Saturday 20 December 2014

Find Armstrong number in C++

#include<iostream>
  using namespace std;
  int main()
  {
  int armstrong=0,num=0,result=0,check;
  cout<<"Enter Number to find it is an Armstrong number?";
       cin>>num;
       check=num;
       for(int i=1;num!=0;i++){
           armstrong=num%10;
           num=num/10;
           armstrong=armstrong*armstrong*armstrong;
           result=result+armstrong;
       }
       if(result==check){
       cout<<check<<"  is an Armstrong Number";
       }
       else{
       cout<<check<<"  is NOT an Armstrong Number";
       }
       return 0;
    }

Find Palindrome number in c++

  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int palindrome, reverse=0;
  6.     cout<<"Enter number:  ";
  7.     cin>>palindrome;
  8.     int num=0,key=palindrome;
  9. for(int i=1;palindrome!=0;i++){

  10.     num=palindrome%10;
  11.     palindrome=palindrome/10;
  12.     reverse=num+(reverse*10);
  13.               }

  14.    if(reverse==key){
  15.    cout<<key<<" is a Palindrome Number";
  16.             }
  17.             else{
  18.    cout<<key<<"is NOT a Palindrome Number";
  19.             }
  20. return 0;
}

C++ program to find Fibonacci Series

  • #include<iostream>
  •     using namespace std;
  •     int main()
  •     {
  •        int range, first = 0, second = 1, fibonicci=0;
  •        cout << "Enter Range for Terms of Fibonacci Sequence: ";
  •        cin >> range;
  •        cout << "Fibonicci Series upto " << range << " Terms "<< endl;
  •        for ( int c = 0 ; c < range ; c++ )
  •        {
  •           if ( c <= 1 )
  •              fibonicci = c;
  •           else
  •           {
  •              fibonicci = first + second;
  •              first = second;
  •              second = fibonicci;
  •           }
  •           cout << fibonicci <<" ";
  •        }
  •        return 0;

  •     }
  • Find last prime number occur before enter number


    #include<iostream>
    using namespace std;
    int main()
    {
     int num,count=0;
     cout<<"Enter number to find last prime number occurs before it: ";
     cin>>num;
      for( int a=num-1;a>=1;a--)
      {
       for(int b=2;b<a;b++)
          {
           if(a%b==0)
           count++;
           }
           if(count==0)
           {
          if(a==1)
           {
           cout<<"no prime number less than 2";
           break;
         }
          cout<<a<<" is the last prime number before entered number";
           break;
           }
           count=0;
    }
    return 0;
    }

    Friday 19 December 2014

    C++ program to swap the values of two integers

    #include<iostream>

    using namespace std;

    int main()

    {

    int var1, var2, swap;

     cout<<"Enter value for first integer:  ";

     cin>>var1;

     cout<<"Enter value for second integer:  ";

     cin>>var2;

     cout<<" Values Before swapping:  "<<endl;

     cout<<"First Integer ="<<var1<<endl;

     cout<<"Second Interger ="<<var2<<endl;

                  swap=var1;

                  var1=var2;

                  var2=swap;

     cout<<" Values After swapping:  "<<endl;

     cout<<"First Integer ="<<var1<<endl;

     cout<<"Second Interger ="<<var2<<endl;

     return 0;

    }

    Thursday 18 December 2014

    C++ program to find greatest number between three numbers

    1. #include<iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. int num1,num2,num3;
    6. cout<<" Enter value for first number";
    7. cin>>num1;

    8. cout<<" Enter value for second number";
    9. cin>>num2;

    10. cout<<" Enter value for third number";
    11. cin>>num3;
    12. if(num1>num2&&num1>num3)
    13. {
    14. cout<<" First number is greatest:"<<endl<<"whick is= "<<num1;
    15. }
    16. else if(num2>num1&&num2>num3)
    17. {
    18. cout<<" Second number is greatest"<<endl<<"whick is= "<<num2;
    19. }
    20. else
    21. {
    22. cout<<" Third number is greatest"<<endl<<"whick is= "<<num3;
    23. }
    24. return 0;
    25. }

    Program to find factorial of Number in C++

     The factorial of a number 'n' is the product of all number from 1 upto the number 'n'
    it is denoted by n!.  For example n=5 then factorial of 5 will be 1*2*3*4*5= 120. 5!= 120
    Factorial program C++ Logic:
    • First think what is the factorial of a number? How mathematically it can be calculated.
    •  If you got this info then it will be very easier to make a C++ Program logic to find the factorial.
    •  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 variable contains garbage value then we will not be able to get correct result. 
    • It is recommended that to initialize it to one.
    #include<iostream>

    using namespace std;

    int main()

    {

        int num,factorial=1;

        cout<<" Enter Number To Find Its Factorial:  ";

        cin>>num;

        for(int a=1;a<=num;a++)

        {

            factorial=factorial*a;

        }

    cout<<"Factorial of Given Number is ="<<factorial<<endl;

        return 0;

    }

    FIND PRIME NUMBER IN C++

    What is a PRIME NUMBER?

    " A Natural number greater than 1 which has only two divisor 1 and itself is called prime number ".
    For Example:  

    5 is prime, because it has only two divisors 1 and itself.

    1. #include<iostream.h>
    2. #include<conio.h>
    3.         void main()
    4.         {
    5.          //clrscr();
    6.          int number,count=0;
    7. cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
    8.           cin>>number;
    9.            for(int a=1;a<=number;a++)
    10.                {
    11.                 if(number%a==0)
    12.                    {
    13.                   count++;
    14.                    }
    15.                }
    16.        if(count==2)
    17.          {
    18.           cout<<" PRIME NUMBER \n";
    19.          }
    20.        else
    21.          {
    22.           cout<<" NOT A PRIME NUMBER \n";
    23.          }
    24.        //getch();
    25.        }

    FIND THE PERFECT NUMBER IN C++

    What is a perfect number?
    "Perfect number is a positive number which sum of all positive divisors excluding that number.
    For example 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
    1 + 2+ 3 =6
    and  28 is also a Perfect Number
     since 1+ 2 + 4 + 7 + 14= 28

    Other perfect numbers: 496, 8128

    1. #include<iostream.h>
    2. #include<conio.h>
    3. void main()                 //Start of main
    4. {
    5.   clrscr();
    6.    int i=1, u=1, sum=0;
    7.    while(i<=500)
    8.  {                                  // start of first loop.

    9.    while(u<=500)
    10.    {                               //start of second loop.
    11.      if(u<i)
    12.      {
    13.       if(i%u==0 )
    14.       sum=sum+u;
    15.      }                          //End of if statement
    16.     
    17.      u++;
    18.    }                           //End of second loop

    19.    if(sum==i)
    20.    {
    21.     cout<<i<<" is a perfect number."<<"\n";
    22.    }

    23.    i++;
    24.    u=1;  sum=0;
    25.  }                             //End of First loop
    26.    getch();
    27.  }                            //End of main