Sunday, November 18, 2012

Show which numbers is greater amongst 3 in C++

#include <conio.h>
#include <iostream.h>
int main()
{
        int a,b,c;
        cout<<"enter a value of a";
        cin>>a;
         cout<<"enter a value of b";
        cin>>b;
         cout<<"enter a value of c";
        cin>>c;
        if (a>b && a>c)
        cout<<"The value A is Greater than all 2";
        else if (b>c)
        cout<<"The value B is Greater than all 2";
        else
        cout<<"The value C is Greater than all 2";
getch();
return 0;
}

Convert decimal numbers to octal numbers in C++

#include<iostream.h>
#include<conio.h>
int main ()
{
    int num,i=0;
    cout<<"Enter the decimal number:";
    cin>> num;
    while(num>0)
    {
                i=num%8;
                num=num/8;  cout<<i;
                }
                cout<<"(Result in reverse order)";
                getch ();
                return 0;
}

conver decimal to Hexadecimal in C++

#include<iostream.h>
#include<conio.h>
int main ()
{
    int num,i=0;
    cout<<"Enter the decimal number:";
    cin>> num;
    while(num>0)
    {
                i=num%16;
                num=num/16;  cout<<i;
                }
               cout<<"(Result in reverse order)";
                getch ();
                return 0;
}

Convert Decimal number to binary numbers in C++

#include<iostream.h>
#include<conio.h>
int main ()
{
    int num,i=0;
    cout<<"Enter the decimal number:";
    cin>> num;
    while(num>0)
    {
                i=num%2;
                num=num/2;  cout<<i;
                }
                cout<<"(Result in reverse order)";
                getch ();
                return 0;
}

Tuesday, November 13, 2012

Displaying even numbers upto 100 in C++

#include <iostream.h>
#include <conio.h>
int main()
{
          int i;
for(int i=1;i <= 100 ;i++)
{ if(i%2==0)
cout<<i<<endl;
}
getch();
return 0;
}

Build a diamond in C++

#include <string.h>
 #include <iostream.h>
 #include <conio.h>
int main()
{
    int i, j, k;
    int n = 0;

    cout<<"Program for displaying pattern of *.\n";
    cout<<"Enter the maximum number of *: ";
    cin>>n;

  cout<<"\nHere is the Diamond of Stars\n";

    for (i = 1; i <= n; i++)
    {
          for (j = 0; j < (n - i); j++)
                cout<<" ";
          for (j = 1; j <= i; j++)
                cout<<"*";
          for (k = 1; k < i; k++)
                cout<<"*";
          cout<<"\n";
    }

    for (i = n - 1; i >= 1; i--)
    {
          for (j = 0; j < (n - i); j++)
                cout<<" ";
          for (j = 1; j <= i; j++)
                cout<<"*";
          for (k = 1; k < i; k++)
                cout<<"*";
          cout<<"\n";
    }
   
    cout<<"\n";
    getch();
    return 0;
}

comparing character and integer in C++

#include<iostream.h>
#include<conio.h>
main()
{
      int i;
      char c;
      for(i=0;i<256;i++)
      {
                        c=i;
                        cout<<i<<"\t"<<c<<endl;
                        }
                        getch();
                        return 0;

}

Weather program in C++

#include<iostream.h>
#include<conio.h>
#include<stdio.h> 

 #include<windows.h>   
int main()
{
int w;
int a,b;
system("color 5e");


cout<<"   ASIM KHAN BANGASH"<<endl;
cout<<"Enter month numbers between 1 and 12:";
cin>>w;
switch(w)
{
case 1:
case 2:
case 11:
case 12:
cout<<"The weather is winter";
break;
case 9:
case 10:
cout<<"The weather is Autum";
break;
case 3:
case 4:
cout<<"The weather is Spring";
break;
case 5:
case 6:
case 7:
case 8:
cout<<"The weather is Summer";
break;
defualt:
cout<<"You entered an Invalid month";
}
getch();
return 0;
}
   

Table in C++

#include <iostream>
#include <conio.h>

using namespace std;
int main()
{
int i=1;
int n,r;
int v;
cout<<"Enter a table number you want:";
cin>>n;
cout<<"Enter the Table Limit:";
cin>>v;
while(i<=v)
{
r=n*i;
cout<<n<<"*"<<i<<"="<<r<<endl;
i++;
}
getch();
    return 0;
}

Quadratic equation in C++

#include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
        int a,b,c,d,e,i;
        cout<<"Solving the quadratic equation ax^2+bx+c=0"<<endl;
        cout<<"Enter 'a' value:";
        cin>>a;
        cout<<"Enter 'b' value:";
        cin>>b;
        cout<<"Enter 'c' value:";
        cin>>c;
        i=1/2;
        d=(b*b)+4*(a*c);
        e=sqrt(d)/2*a;
        cout<<endl<<"The values of x are"<<-b+e<<" and "<<-b-e;
        getch();
        return 0;
}

power function in C++

#include<iostream.h>
#include<conio.h>
int power(int,int);
int main()
{
int base,pow,ans;
cout<<"Enter base:";
cin>>base;
cout<<"Enter power:";
cin>>pow;
ans=power(base,pow);
cout<<"The answer is:"<<ans;
getch();
return 0;
}
int power(int b,int p)
{
int rp=1;
for(int i=1;i<=p;i++)
rp=rp*b;
return rp;
}

Log program in C++

#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
          int a;
          float r;
          cout<<"Enter a number :";
          cin>>a;
          r=log(a);
        
          cout<<"the log of  "<<a<<"  is :"<<r;
          getch();
          return 0;
          }

Factorial in C++

# include <conio.h>
# include <iostream.h>
int main()
{
long rt=1;
int fact;
cout <<"Enter the number for factorial: ";
cin >>fact;
for(int i=1;i<=fact;i++)
{
rt=rt*i;
}
cout<<"the Factorial of "<<fact<<" is: "<<rt;
getch();
return 0;
}

square diagram in C++

#include <string.h>
 #include <iostream.h>
 #include <conio.h>
int main(){
    int u,v, k;
    int n = 0;
    cout<<"Program for displaying pattern of *.\n";
    cout<<"Enter the width of square: ";
    cin>>n;
 cout<<"Enter the length of square: ";
    cin>>v;
  cout<<"\nHere is the square of Stars\n";

    for (int i = 1; i <= n+3; i++)
    { cout<<"*";
    } cout<<endl;
    for (int i = 1; i <= n+3; i++)
    { cout<<"*";
    } cout<<endl;
    for (int i = 1; i <= v; i++)
    { cout<<"**";
        for (int i = 1; i <= n-1; i++)
    { cout<<" ";
}
   
    cout<<"**"<<endl;
    }
    for (int i = 1; i <= n+3; i++)
    { cout<<"*";
    } cout<<endl;
    for (int i = 1; i <= n+3; i++)
    { cout<<"*";
    }
    getch();
    return 0;
}

square root in C++

#include<iostream.h>
#include<conio.h>
#include<math.h>
int main(){    double num, ans;
    cout<<"Enter number to find square root";
    cin>>num;
    ans=sqrt(num);
    cout<<"The square root of "<<num<< "="<<ans;
    getch();
}

first program in C++

#include<iostream.h>
#include <conio.h>
int main()
{
        int a,b,c;
        cout<<"enter a 1st number:";
        cin>>a;
        cout<<"enter 2nd number:";
        cin>>b;
        c=a+b;
        cout<<"The sum of two number is:"<<c;
        getch();
        return 0;
}