Learn C++ beginer programs

July 8, 2009

write a program to calculate sum of all even numbers upto n

Filed under: cin, cout, else if, for loop, if, if else, loop, numbers — snehilkhanor @ 1:49 pm
A number is taken from user and sum of all even numbers upto that number is calculated.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,sum=0;
  7. cout<<”Please enter a number: “;
  8. cin>>n;
  9. for (int i=1;i<=n;i++)
  10. {
  11. if(i%2==0)
  12. sum=sum+i;
  13. else
  14. continue;
  15. }
  16. cout<<sum;
  17. getch();
  18. }

write a program to calculate sum of all odd numbers upto n

Filed under: cin, cout, else if, for loop, if, if else, loop, numbers — snehilkhanor @ 1:44 pm
A number n is taken from user and sum of all odd numbers upto n is calculated.

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,sum=0;
  7. cout<<”Please enter a number: “;
  8. cin>>n;
  9. for (int i=1;i<=n;i++)
  10. {
  11. if(i%2!=0)
  12. sum=sum+i;
  13. else
  14. continue;
  15. }
  16. cout<<sum;
  17. getch();
  18. }

write a program to displat table of a number

Filed under: cin, cout, for loop, loop — snehilkhanor @ 1:40 pm
A number is taken from user and its table upto n is displayed.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,m;
  7. cout<<”Please enter n: “;
  8. cin>>n;
  9. for (int i=1;i<=10;i++)
  10. {
  11. m=n*i;
  12. cout<<n<<”*”<<i<<”=”<<m<<endl;
  13. }
  14. getch();
  15. }

write a profram to calculate power of a number

Filed under: cin, cout, for loop, loop — snehilkhanor @ 1:34 pm
A number and power to be calculated is taken from user.Power is calculated.

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,x,y=1;
  7. cout<<”Please enter x: “;
  8. cin>>x;
  9. cout<<”Please enter n: “;
  10. cin>>n;
  11. for (int i=1;i<=n;i++)
  12. y=y*x;
  13. cout<<y;
  14. getch();
  15. }

write a program to determine factorial of a number

Filed under: cin, cout, for loop, loop — snehilkhanor @ 1:32 pm
A number is taken from user and its factorial is given.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,fac=1;
  7. cout<<”Please enter a number: “;
  8. cin>>n;
  9. for (int i=1;i<=n;i++)
  10. fac=fac*i;
  11. cout<<fac;
  12. getch();
  13. }

write a program to find sum of n numbers

Filed under: cin, cout, for loop, loop, numbers, sum — snehilkhanor @ 1:27 pm
A number n is taken from user.
Sum of all natural numbers upto n is displayed.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,sum=0;
  7. cout<<”Please enter a number: “;
  8. cin>>n;
  9. for (int i=1;i<=n;i++)
  10. sum=sum+i;
  11. cout<<sum;
  12. getch();
  13. }

write a program to check whether entered number is prime or not

Filed under: cin, cout, else if, for loop, if, if else, loop, numbers — snehilkhanor @ 1:24 pm

Prime number: A natural number which has exactly two distinct natural number divisors: 1 and itself.

A number is taken from user and is checked whether prime number or not.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,m;
  7. cout<<”Please enter a number: “;
  8. cin>>n;
  9. for (int i=2;i<=(n-1);i++)
  10. {
  11. if(n%i==0)
  12. {
  13. cout<<”Not a prime”;
  14. break;
  15. }
  16. else
  17. {
  18. cout<<”Prime”;
  19. break;
  20. }
  21. }
  22. getch();
  23. }

write a program to display fibonacci series upto n terms

Filed under: cin, cout, for loop, numbers — snehilkhanor @ 1:19 pm
Fibonacci series:The first two Fibonacci numbers are 0 and 1, and each remaining number is the sum of the previous two
e.g.: 0 1 1 2 3 5 8 13 21 ….
A number n is taken from user and the series is displayed upto nth term.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a=0,b=1,c=0,n;
  7. cout<<”Enter the number of terms you wanna see: “;
  8. cin>>n;
  9. cout<<a<<” “<<b<<” “;
  10. for(int i=1;i<=n-2;i++)
  11. {
  12. c=a+b;
  13. a=b;
  14. b=c;
  15. cout<<c<<” “;
  16. }
  17. getch();
  18. }

write a program to check whether entered number is palindrome or not

Filed under: cin, cout, else if, if, if else, loop, numbers, while loop — snehilkhanor @ 12:33 pm
Palindrome :A word or phrase that reads the same backward as forward
A number is taken from user and then checks if the entered number is palindrome or not.

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. long int n,rev=0,m,num;
  7. cout<<”please enter a five digit no.: “;
  8. cin>>n;
  9. num=n;
  10. while(n>0)
  11. {
  12. m=n%10;
  13. rev=rev*10+m;
  14. n=n/10;
  15. }
  16. cout<<rev<<endl;
  17. if (num==rev)
  18. cout<<”Number is a palindrome”;
  19. else
  20. cout<<”Not a palindrome”;
  21. getch();
  22. }

write a program to reverse digits of a number

Filed under: cin, cout, loop, while loop — snehilkhanor @ 12:18 pm
A number is taken from the user number with reverse digits is displayed

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. long int n,rev=0,m;
  7. cout<<”please enter a five digit no.: “;
  8. cin>>n;
  9. while(n>0)
  10. {
  11. m=n%10;
  12. rev=rev*10+m;
  13. n=n/10;
  14. }
  15. cout<<rev;
  16. getch();
  17. }

Next Page »

Blog at WordPress.com.