Pages

Showing posts with label cpp. Show all posts
Showing posts with label cpp. Show all posts

Friday, 4 July 2014

Write a C++ program to find greatest number between three numbers.

Program description
This program shows the greatest number between three numbers using if-else-if statement. It takes three numbers as input from user and output the greatest number.

code-

    #include<iostream>
    using namespace std;
    int main()
    {
    int num1,num2,num3;
    cout<<" Enter value for first number";
    cin>>num1;

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

    cout<<" Enter value for third number";
    cin>>num3;
    if(num1>num2&&num1>num3)
    {
    cout<<" First number is greatest:"<<endl<<"whick is= "<<num1;
    }
    else if(num2>num1&&num2>num3)
    {
    cout<<" Second number is greatest"<<endl<<"whick is= "<<num2;
    }
    else
    {
    cout<<" Third number is greatest"<<endl<<"whick is= "<<num3;
    }
    return 0;

    }

Write a C++ Program take hours, minutes, seconds and print it in 24 hours & standard format

C++ program to display hours minutes and seconds in both  12 and 24 hours format:
24 Hours format : 23:30:12
Standard format : 11:30:12 pm
C++ CODE using Turbo C++ IDE.

code-
#include<iostream.h>
#include<conio.h>
void main()
{

   clrscr();

   int hours,mins,seconds,x;                         

   cout<<"Enter hours=";

   cin>>hours;

   cout<<"\nEnter minutes=";

   cin>>mins;

   cout<<"\nEnter seconds=";

   cin>>seconds;

   if(hours > 24)

    {

     cout<<"Invalid Entery";

   }

   else

   {

       cout<<"\n24 Hours Format\n";

cout<<"Hours  :  Mins  :  Seconds\n"<<"  "<<hours<<"  :     "<<mins<<"   :     "<<seconds<<"\n";

  
  if(hours > 12)

{

  hours=hours-12;

  cout<<"12 Hours Format\n";

cout<<"Hours  :  Mins  :  Seconds\n"<<"  "<<hours<<"  :     "<<mins<<"   :     "<<seconds;

  }

       else

  {

 cout<<"12 Hours Format\n";

 cout<<"Hours  :  Mins  :  Seconds\n"<<" "<<hours<<": "<<mins<<"   :        "<<seconds;

  }

   }


}                            // end of main

Write a C++ Program To Find Prime Numbers.

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.


code-

    #include<iostream.h>
    #include<conio.h>
            void main()
            {
             //clrscr();
             int number,count=0;
    cout<<"ENTER NUMBER TO CHECK IT IS PRIME OR NOT ";
              cin>>number;
               for(int a=1;a<=number;a++)
                   {
                    if(number%a==0)
                       {
                      count++;
                       }
                   }
           if(count==2)
             {
              cout<<" PRIME NUMBER \n";
             }
           else
             {
              cout<<" NOT A PRIME NUMBER \n";
             }
           //getch();
           }

Thursday, 3 July 2014

Write a C++ Program to find the Perfect Number.

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

CODE :-

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

   while(u<=500)

   {                               //start of second loop.
     if(u<i)
     {
      if(i%u==0 )
      sum=sum+u;
     }                          //End of if statement
    
     u++;
   }                           //End of second loop

   if(sum==i)

   {
    cout<<i<<" is a perfect number."<<"\n";
   }

   i++;

   u=1;  sum=0;
 }                             //End of First loop
   getch();

 }