"Enter The Number of Terms of Fibonacci Series You Want": 0 1 Cout Cin Cout 0 1
"Enter The Number of Terms of Fibonacci Series You Want": 0 1 Cout Cin Cout 0 1
#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;
}
User enters a number and we have to multiply all numbers upto entered number.
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;
}