0% found this document useful (0 votes)
83 views4 pages

"Enter The Number of Terms of Fibonacci Series You Want": 0 1 Cout Cin Cout 0 1

The document contains code for calculating the Fibonacci series and factorial of a number in C++. For the Fibonacci series, the code uses a for loop to calculate the next term as the sum of the previous two terms, starting from 0 and 1. For factorial, it uses a for loop to multiply all integers from 1 to the given number, initializing the variable to store the factorial as 1. The code also contains a program to check if a number is a palindrome by reversing it and comparing to the original number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views4 pages

"Enter The Number of Terms of Fibonacci Series You Want": 0 1 Cout Cin Cout 0 1

The document contains code for calculating the Fibonacci series and factorial of a number in C++. For the Fibonacci series, the code uses a for loop to calculate the next term as the sum of the previous two terms, starting from 0 and 1. For factorial, it uses a for loop to multiply all integers from 1 to the given number, initializing the variable to store the factorial as 1. The code also contains a program to check if a number is a palindrome by reversing it and comparing to the original number.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Fibonacci Series

#include<iostream>
using namespace std;
main()
{
int n, c, first = 0, second = 1, next;
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
cout << "First " << n << " terms of Fibonacci series are :- " << endl;
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
}
}

return 0;

Or
#include<stdio.h>
#include<conio.h>
void main()
{
int first=0,second=1,third,i,n;
clrscr(); //to clear the screen
printf(Enter how many elements?);
scanf(%d,&n);
printf(n%d %d,first,second);
for(i=2;i<n;++i)
{
third=first+second;
printf( %d,third);
first=second;
second=third;
}

getch(); //to stop the screen


}

C++ code to find factorial using for loop


o

User enters a number and we have to multiply all numbers upto entered number.

Like if user enters 6 then Factorial should be equal to factorial= 1*2*3*4*5*6.

In this case a for Loop will be very helpful. It will start from one and multiply all numbers upto entered number
after it loop will be terminated.

Take a variable and initialized it to 1 and in loop store multiplication result into it like in below program a variable

Factorial is used for this purpose.what is we does not initialized it to 1 and initialized it to zero or remain it
uninitialized. In case of 0 our result will be zero in case of any number entered

In case of not initializing it our answer will correct mostly but if variablecontains garbage value then we will not be
able to get correct result. It is recommended that to initialize it to one.

Or

#include <stdio.h>
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);
for (c = 1; c <= n; c++)
fact = fact * c;
printf("Factorial of %d = %d\n", n, fact);
}

return 0;

Pallindrome no.-

Or
#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
cout << "Enter a positive number: ";
cin >> num;
n = num;
do
{
digit = num%10;
rev = (rev*10) + digit;
num = num/10;
}while (num!=0);
cout << " The reverse of the number is: " << rev << endl;
if (n==rev)
cout << " The number is a palindrome";
else
cout << " The number is not a palindrome";
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy