If you like my posts please leave a comment.

Sunday, August 12, 2012

Some Simple C++ Programs


Dear students, so far while discussing various topics I have include related programs for the better understanding of those concepts. Here by I am writing some more simple C++ programs which will further help you in better understanding and implementation of the programming concepts that so far we have discussed.  
It will be the best practice if you can write some more programs by your own. As we will move on further, more related programs will be discussed.
Prg.No.
Program
1.
//Write a Program to check for a given number as even or odd
#include<iostream.h>
int main()
{
int num;
cout<<”\n Enter a number: “;
cin>>num;
if(num%2 == 0)
cout<<”\n Number “<<num<<” is EVEN”;
else
cout<<”\n Number “<<num<<” is ODD”;
return 0;
}
2.
/*Write a program to print all the EVEN numbers present in a given range of numbers*/
#include<iostream.h>
int main()
{
int lower,upper;
cout<<”\n Enter Lower limit:”;
cin>>lower;
cout<<”\n Enter Upper limit:”;
cin>>upper;
cout<<”\n EVEN numbers in the range: \n\n”;
  for(int i=lower;i<=upper;i++)
  {
    if(i%2 == 0)
    cout<<i<<endl;
  }
Return 0;
}

3.
//Find the greatest of three numbers
#include <iostream.h>
int main()
{
    int a,b,c;
    cout<<"\nEnter Three Numbers: ";
    cin>>a>>b>>c;
    cout<<endl;
    if(a>b)
    {
        if(a>c)
        {
         cout<<a<<" IS GREATEST";
        }
    }
        else if(b>c)
        cout<<b<<" IS GREATEST";
        else
        cout<<c<<" IS GREATEST";
    return 0;
}
4.
//Convert the total time entered in seconds as – Hrs. Mins. Sec.
#include <iostream.h>
int main()
{
    int val,hrs,min,sec,temp1;
    cout << "Enetr time in seconds" << endl;
    cin>>val;
    hrs=val/3600;
    temp1=val-(hrs*3600);
    min=temp1/60;
    sec=temp1-(min*60);
    cout<<"Time = "<<"Hrs: "<<hrs<<" Mins: "<<min<<" Sec: "<<sec;
    return 0;
}
5.
//Calculate LCM of two numbers
#include <iostream.h>
int main()
{
  int i,m,n,v1,lcm;
  lcm=1;i=2;
  cout<<"\n Enter two numbers: ";
  cin>>m>>n;

  v1=(m>n)?m:n;
  while(i<=v1)
  {
     if(m%i ==0 && n%i==0)
     {
          lcm*=i;
          m/=i;

          n/=i;

          i=2;
     }
     else
     i++; v1=(m>n)?m:n;
  }
  cout<<"\n LCM = "<<lcm*m*n;
    return 0;
}
6.
//Program to find HCF of two numbers
#include <iostream.h>
int main()
{
    int a,b,max,min,r;
r=0;
cout<<"\n enter two numbers: ";
cin>>max>>min;
(max > min) ? max:min;
     while(max % min != 0)
     {
          r=max%min;
          int temp=min;
          min=r;
          max=temp;
     }
       cout<<"\n HCF:"<<min;
    return 0;
}
7.
//Program to print the Factorial of a number
#include <iostream.h>
int main()
{
    int num,factorial=1,i;
    cout << "Enter a number" << endl;
    cin>>num;
    i=num;
    while(i>=1)
    {
        factorial*=i;
        i--;
    }
    cout<<"\nFactorial of "<<num<<" = "<<factorial;
    return 0;
}
8.
//Program to check for an Armstrong Number
// Example: 371
#include <iostream.h>
#include<math.h>
int main()
{
    int num,temp,R,sum=0;
    cout << "Enter a number: ";
    cin>>num;
    temp=num;
    while(temp)
    {
        R=temp%10;
        sum=sum+R*R*R;
        temp/=10;
    }
    if(sum == num)
    cout<<num<<" is Armstrong Number";
    else
    cout<<num<<" is not Armstrong Number";
    return 0;}
9.
//To check for a given as Palindrome number
#include <iostream.h>
int main()
{
    int num,num2,R,rev=0;
    cout << "Enter Your Number" << endl;
    cin>>num;
    num2=num;
    while (num)
    {
        R=num%10;
        rev=rev*10+R;
        num/=10;
    }
    cout<<"Reversed form of number = "<<rev;
    if(rev == num2)
    cout <<" and the number is PALINDROME";
    else
    cout <<" and the number is NOT PALINDROME";
    return 0;
}
10.
//Program for printing Fibonacci Series for given number or terms
#include <iostream.h>
int main()
{
    int a=-1,b=1,sum=0,num;
    cout<<"\nEter for number of terms:";
    cin>>num;
    for(int i=0;i<num;i++)
    {
        sum=a+b;
        cout<<sum<<' ';
        a=b;
        b=sum;
    }
    return 0;
}
11.
//Write the program to print the following output:
 
#include <iostream.h>
int main()
{
    for(int i=0;i<10;i++)
    {
        for(int j=0;j<i;j++)
        {
            if((i+j)%2==0)
            cout<<0<<' ';
            else
            cout<<1<<' ';
        }
        cout<<endl<<endl ;
    }
    return 0;
}
12.
//Program to check a year as a LEAP year or not
#include <iostream.h>
int main()
{
    int year;
cout<<"\n Enter Year: ";
cin>>year;
     if(year%4 == 0 || year % 400 == 0)
     cout<<"\n LEAP YEAR";
     else
     cout<<"\n NOT A LEAP YEAR";
    return 0;
}
13.
//Program to print all the PRIME NUMBERS from the range of numbers
#include <iostream.h>
int main()
{
    int lower,upper,flag;
cout<<"\n Enter Lower limit: ";
cin>>lower;
cout<<"\n Enter Upper limit: ";
cin>>upper;
cout<<"\n PRIME NUMBERS ARE: \n\n";
     for(int i=lower;i<=upper;i++)
     {            flag = 0;
          for(int j=2;j<=i-1;j++)
          {
              if(i%j == 0)
              {
              flag = 1;
              break;
              }
          }
          if(!flag)
          cout<<i<<'\n';
     }
    return 0;
}
14.
/*Convert an entered character into upper case to lower case and vise versa*/
#include <iostream.h>
int main()
{
char ch;
     cout<<"\n Enter your character: ";
     cin>>ch;
     if(int(ch)>=65 && int(ch)<=90)
     ch+=32;
     else if(int(ch)>=97 && int(ch)<=122)
     ch-=32;
     else
     {
     cout<<"\n You haven't entered an alphabet";
     }
cout<<ch;
    return 0;
}
15.
//Program to print the following output
#include <iostream.h>
int main()
{
    int a,b,c,d,k=1;
     for(a = 6;a>=1;a--)
     {
          for(b = 1;b<k;b++)
          cout<<" ";
          for(c=a;c>=1;c--)
          cout<<c;
          for(d=2;d<=a;d++)
          cout<<d;
     cout<<'\n';
     k++;
     }
    return 0;
}
16.
//Write a program to print the following output
#include <iostream.h>

int main()
{
   int a,b,c,d,k=1;
     for(a = 70;a>=65;a--)
     {
          for(b = 65;b<=a;b++)
          cout<<char(b);
          if(a == 70)
          {
              for(c=69;c>=65;c--)
              cout<<char(c);
              cout<<'\n';
          }
          else
          {
              for(d = 1;d<=k;d++)
              cout<<" ";
              for(c=a;c>=65;c--)
              cout<<char(c);
              cout<<'\n';
              k+=2;
          }
     }
    return 0;
}
17.
//Write a program to print the following output
#include<iostream.h>
int main()
{
int i,j,p;
     for(i=5;i>=1;i--)
     {
          for(j=i;j>=1;j--)
          cout<<j<<' ';
     cout<<endl;
     }

return 0;
}
18
//Write a program to print the following output
#include<iostream.h>
int main()
{
int i,j,p;
            for(i=1;i<=5;i++)
            {
                        for(j=1;j<=i;j++)
                        cout<<' ';
                        for(j=1;j<=i;j++)
                        cout<<i<<' ';
            cout<<endl;
            }

return 0;
}

No comments:

Post a Comment