Write a program to print Fibonacci series without using recursion and using recursion.
Fibonacci Series in C++: In case of fibonacci series, next number is the sum of previous two numbers for
example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.
1. #include <iostream>
2. using namespace std;
3. int main() {
4. int n1=0,n2=1,n3,i,number;
5. cout<<"Enter the number of elements: ";
6. cin>>number;
7. cout<<n1<<" "<<n2<<" "; //printing 0 and 1
8. for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed
9. {
10. n3=n1+n2;
11. cout<<n3<<" ";
12. n1=n2;
13. n2=n3;
14. }
15. return 0;
16. }
Write a program to check prime number.
Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers
can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17, 19, 23.... are the prime
numbers.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n, i, m=0, flag=0;
6. cout << "Enter the Number to check Prime: ";
7. cin >> n;
8. m=n/2;
9. for(i = 2; i <= m; i++)
10. {
11. if(n % i == 0)
12. {
13. cout<<"Number is not Prime."<<endl;
14. flag=1;
15. break;
16. }
17. }
18. if (flag==0)
19. cout << "Number is Prime."<<endl;
20. return 0;
21. }
Write a program to check palindrome number.
A palindrome number is a number that is same after reverse. For example 121, 34543, 343, 131, 48984 are
the palindrome numbers.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n,r,sum=0,temp;
6. cout<<"Enter the Number=";
7. cin>>n;
8. temp=n;
9. while(n>0)
10. {
11. r=n%10;
12. sum=(sum*10)+r;
13. n=n/10;
14. }
15. if(temp==sum)
16. cout<<"Number is Palindrome.";
17. else
18. cout<<"Number is not Palindrome.";
19. return 0;
20. }
Write a program to print factorial of a number.
Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int i,fact=1,number;
6. cout<<"Enter any Number: ";
7. cin>>number;
8. for(i=1;i<=number;i++){
9. fact=fact*i;
10. }
11. cout<<"Factorial of " <<number<<" is: "<<fact<<endl;
12. return 0;
13. }
Write a c++ program to check armstrong number.
Armstrong number is a number that is equal to the sum of cubes of its digits. For example 0, 1, 153,
370, 371 and 407 are the Armstrong numbers.
Let's try to understand why 371 is an Armstrong number.
1. 371 = (3*3*3)+(7*7*7)+(1*1*1)
2. where:
3. (3*3*3)=27
4. (7*7*7)=343
5. (1*1*1)=1
6. So:
7. 27+343+1=371
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n,r,sum=0,temp;
6. cout<<"Enter the Number= ";
7. cin>>n;
8. temp=n;
9. while(n>0)
10. {
11. r=n%10;
12. sum=sum+(r*r*r);
13. n=n/10;
14. }
15. if(temp==sum)
16. cout<<"Armstrong Number."<<endl;
17. else
18. cout<<"Not Armstrong Number."<<endl;
19. return 0;
20. }
Write a program to print sum of digits.
FOR EXAMPLE
Input: 23
Output: 5
Input: 624
Output: 12
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n,sum=0,m;
6. cout<<"Enter a number: ";
7. cin>>n;
8. while(n>0)
9. {
10. m=n%10;
11. sum=sum+m;
12. n=n/10;
13. }
14. cout<<"Sum is= "<<sum<<endl;
15. return 0;
16. }
Write a program to reverse given number.
Input: 234
Output: 432
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int n, reverse=0, rem;
6. cout<<"Enter a number: ";
7. cin>>n;
8. while(n!=0)
9. {
10. rem=n%10;
11. reverse=reverse*10+rem;
12. n/=10;
13. }
14. cout<<"Reversed Number: "<<reverse<<endl;
15. return 0;
16. }
Write a program to swap two numbers without using third variable
Input: a=5 b=10
Output: a=10 b=5
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a=5, b=10;
6. cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
7. a=a+b; //a=15 (5+10)
8. b=a-b; //b=5 (15-10)
9. a=a-b; //a=10 (15-5)
10. cout<<"After swap a= "<<a<<" b= "<<b<<endl;
11. return 0;
12. }
Write a c++ program to convert number in characters.
Input: 74254
Output: Seven Four Two Five Four
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. long int n,sum=0,r;
6. cout<<"Enter the Number= ";
7. cin>>n;
8. while(n>0)
9. {
10. r=n%10;
11. sum=sum*10+r;
12. n=n/10;
13. }
14. n=sum;
15. while(n>0)
16. {
17. r=n%10;
18. switch(r)
19. {
20. case 1:
21. cout<<"one ";
22. break;
23. case 2:
24. cout<<"two ";
25. break;
26. case 3:
27. cout<<"three ";
28. break;
29. case 4:
30. cout<<"four ";
31. break;
32. case 5:
33. cout<<"five ";
34. break;
35. case 6:
36. cout<<"six ";
37. break;
38. case 7:
39. cout<<"seven ";
40. break;
41. case 8:
42. cout<<"eight ";
43. break;
44. case 9:
45. cout<<"nine ";
46. break;
47. case 0:
48. cout<<"zero ";
49. break;
50. default:
51. cout<<"tttt ";
52. break;
53. }
54. n=n/10;
55. }
56. }
Write a program to print multiplication of 2 matrices.
Input:
First matrix elements:
1 2 3
1 2 3
1 2 3
Second matrix elements
1 1 1
2 1 2
3 2 1
Output: Multiplication of the matrix:
14 9 8
14 9 8
14 9 8
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
6. cout<<"enter the number of row=";
7. cin>>r;
8. cout<<"enter the number of column=";
9. cin>>c;
10. cout<<"enter the first matrix element=\n";
11. for(i=0;i<r;i++)
12. {
13. for(j=0;j<c;j++)
14. {
15. cin>>a[i][j];
16. }
17. }
18. cout<<"enter the second matrix element=\n";
19. for(i=0;i<r;i++)
20. {
21. for(j=0;j<c;j++)
22. {
23. cin>>b[i][j];
24. }
25. }
26. cout<<"multiply of the matrix=\n";
27. for(i=0;i<r;i++)
28. {
29. for(j=0;j<c;j++)
30. {
31. mul[i][j]=0;
32. for(k=0;k<c;k++)
33. {
34. mul[i][j]+=a[i][k]*b[k][j];
35. }
36. }
37. }
38. //for printing result
39. for(i=0;i<r;i++)
40. {
41. for(j=0;j<c;j++)
42. {
43. cout<<mul[i][j]<<" ";
44. }
45. cout<<"\n";
46. }
47. return 0;
48. }
Write a program to generate Fibonacci triangle.
Input: 5
Output:
1
1 1
1 1 2
1 1 2 3
1 1 2 3 5
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a=0,b=1,i,c,n,j;
6. cout<<"Enter the limit: ";
7. cin>>n;
8. for(i=1; i<=n; i++)
9. {
10. a=0;
11. b=1;
12. cout<<b<<"\t";
13. for(j=1; j<i; j++)
14. {
15. c=a+b;
16. cout<<c<<"\t";
17. a=b;
18. b=c;
19. }
20. cout<<"\n";
21. }
22. return 0;
23. }