Showing posts with label beginner. Show all posts
Showing posts with label beginner. Show all posts

Monday, 22 April 2013

A programe to calculate factorial of a number

Description:

This program take a number from user as input and find its factorial automatically.

Code:


#include<iostream>
#include<conio.h>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
       float a,b=1,c,i,f;
    cout<<"A programe to calculate factorial of a number"<<endl;
    xy:
    cout<<"Enter a number to calculate its factorial"<<endl;
    cin>>a;
    cout<<endl;
    i=a;
    while(i>=1)
    {
      b=b*i;
      i--;
      }
      cout<<"The factorial of the given number is"<<b<<endl;
      goto xy;
      getche();
      return 0;
      }


Output:


Programm to get multiplication table

Description:

This program take a number as input and make its multiplication table automatically.

Code:


#include<iostream>
#include<conio.h>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
    int i,n=1,p;
    xy:
    cout<<"Enter the number to get its multiplication table"<<endl;
    cin>>n;
    cout<<endl;
    i=1;
    while(i<=10)
    {p=n*i;
     cout<<n<<"*"<<i<<"="<<p<<endl;
     i++;
     }
     goto xy;
     getche();
     return 0;
     }


Output:


Calculating sum of first ten odd numbers using loop

Description:

This program automatically calculate sum of first ten odd numbers using 'for' loop.

Code:


#include<iostream>
#include<conio.h>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
    int i,j,s=0;
    i=1;
    cout<<"The first ten odd numbers are"<<endl;
    while(i<20)
    {
      if(i%2!=0)
     {
      cout<<i<<" ";
      s=s+i;
      }
      i++;
      }
      cout<<endl<<"The sum of first ten odd numbers is"<<endl<<s;
      getche();
      return 0;
      }


Output:


A programe to find the roots of quadratic equation

Description:

This program take three constants of quadratic equation as input and find real and imaginary roots of that quadratic equation automatically.

Code:


#include<iostream>
#include<conio.h>
#include<iomanip>
#include<math.h>
using namespace std;
int main()
{
    float a,b,c,d,f,e,g,h,i,j,k,l;
    cout<<"A programe to find the roots of quadratic equation"<<endl;
    xy:
    cout<<"Enter the values of a,b,c"<<endl;
    cout<<"Enter value of a"<<endl;
    cin>>a;
    cout<<endl<<"Enter the value of b"<<endl;
    cin>>b;
    cout<<endl<<"Enter the value of c"<<endl;
    cin>>c;
    cout<<endl;
    d=b*b-(4*a*c);
    if(d>0||d==0)
    {
      f=(-b+sqrt(d))/(2*a);
      e=(-b-sqrt(d))/(2*a);
      cout<<"The required roots are"<<endl<<"The first root is ="<<f<<endl<<"The second root is ="<<e<<endl;
      }
    if(d<0)
    {
      g=-b/(2*a);//x1
      h= sqrt(-d)/(2*a);//x1
      j=-sqrt(-d)/(2*a);//x2
      k=g+h;
      l=g+j;
      cout<<"the required roots are"<<endl;
      cout<<"first root="<<k<<"i"<<endl;
      cout<<"second root="<<l<<"i"<<endl;
      }
      goto xy;
      getche();
      return 0;
      }


Output:


A programme to check the divisibility of a number by 3

Description:

In C++ if there is conditional logic then "if", "else" statements are used.

Code:


#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
    int a,b,c;
    cout<<"A programe to check the divisibility of a number by 3"<<endl;
    xy:
    cout<<"Enter the number to check its divisibility"<<endl;
    cin>>a;
    cout<<endl;
    if(a%3==0)
    {
              cout<<"The number is divisible by 3"<<endl;
           
              }
           
              goto xy;
              getche();
              return 0;
              }


Output:


Conversion of Fahrenheit Temperature in Celcius

Description:

In C++ 'float' (data type) is used for decimal numbers. And 'int' (data type) is used for integer numbers.

Code:


#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
    float c,f,a;
    xy:
    cout<<"Enter the value of temperature in Fahrenheit"<<endl;
    cin>>f;
    c=(f-32)*5/9;
    cout<<endl<<"The value of temperature in celsius degrees is"<<endl<<c<<endl;
    goto xy;
    getche();
 
    }


Output:


Addition and Multiplication of Two Numbers

Description:

In c++ 'goto' operator is used to repeat program again. In other words it acts as infinite loop.

Code:


#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
    int a,b,c,d;
    xy:
    cout<<"Enter two numbers to get their sum and product"<<endl;
    cout<<"Enter the value of a"<<endl;
    cin>>a;
    cout<<"You have entered a="<<a;
    cout<<endl<<"Enter the value of b"<<endl;
    cin>>b;
    cout<<"You have entered b="<<b<<endl;
    c=a+b;
    cout<<"The sum of a and b is"<<endl<<"a+b="<<c<<endl;
    d=a*b;
    cout<<"The product of a and b is"<<endl<<"a*b="<<d<<endl;
    goto xy;
    getche();
    return 0;
    }


Output:


Taking two inputs in variables and then interchanging their values

Description:

In c++, to take inputs from user 'cin' operator is used and to show output 'cout' operator is used.

Code:


#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
    int a,b,c;
    cout<<"Enter value of A"<<endl;
    cin>>a;
    cout<<endl;
    cout<<"Enter value of B"<<endl;
    cin>>b;
    c=a;
    a=b;
    b=c;
    cout<<"interchanged values are"<<endl;
    cout<<a<<endl<<b<<endl<<"ok";
    getche();
    return 0;
    }


Output:


Creating Columns using "SETW" Operator

Description:

In c++ "Setw(x)" operator is used to separate columns up to 'x' units. It stands for set width. Used in making tables.

Code:


#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;
int main()
{
    cout<<setw(6)<<"62"<<setw(6)<<"8"<<endl;
    cout<<setw(6)<<"100"<<setw(6)<<"77"<<endl;
    cout<<setw(6)<<"5"<<setw(6)<<"162"<<endl;
    getche();
    return 0;
    }


Output: