Monday 6 April 2015

Programs in C++ With Syntax

QUE 1- WAP to find square of a number.
     #include<iostream.h>    #include<conio.h>   
     void main()   
   { clrscr();    
      int num, square;    
      cout<<"enter the num:";    
      cin>>num;    
      square=num*num;    
      cout<<"the square of given num:";    
      cout<<square;    getch();    }


QUE 2- WAP to  find percentage of marks in 3 subjects.

      #include<iostream.h>
      #include<conio.h>
      int main()
     { clrscr();
       float sub1, sub2, sub3, marks, perc;
       cout<<"Enter marks obtained in 3 subjects:";cin>>sub1 >>sub2 >>sub3;
       marks =sub1+sub2+sub3;perc=(marks/300)*100;
       cout<<"\n"<<"the percentage marks are:"<<perc<<"%";
       getch();return 0;
      }

QUE 3- WAP to add, subtract,multiply and divide 2 numbers.
        #include<iostream.h> 
        #include<conio.h>
        int main()
       {  clrscr();   
          char ch;   
          float a, b, result;   
          cout<<"Enter two numbers:";   
          cin>>a>>b;  
          cout<<"\n"<<"Enter the operator(+,-,*,/):";  
          cin>>ch;   
          cout<<"\n"; 
          if(ch=='+')   result=a+b;   else   if(ch=='-')   result=a-b;   else   if(ch=='*')   result=a*b;   
          else   if(ch=='/')   result=a/b;  
          else   cout<<"wrong operator \n";   
          cout<<"\n"<<"The calculated result is:"<<result<<"\n";
          getch();   
         return 0;   
      }




QUE 4- WAP to find area of circle & circumference.
#include<iostream.h>
#include<conio.h>
int main()
{clrscr();
int choice,r, Area;
cout<<"Area menu"<<"\n";
cout<<"1.Area of_circle"<<"\n";
cout<<"2.Area of_circumference"<<"\n";
cout<<"Enter your choice (1-2):";
cin>>choice;if(choice==1){cout<<"\n"<<"Enter the value of r:";
cin>>r;Area = 3.14*r*r;cout<<"\n"<<"The Area of_circle is"<<Area<<"\n";}
else{cout<<"\n"<<"Enter the value of r:";cin>>r;Area = 2*3.14*r;
cout<<"\n"<<"The Area of_circumference is"<<Area<<"\n";}
getch();
return 0;}


QUE 5- WAP to find cube of a number.  
 #include<iostream.h>  
 #include<conio.h>   
void main()   
{clrscr();   
int num, cube;   
cout<<"enter the num:";  
 cin>>num;   
cube=num*num*num;   
cout<<"the cube of given num:";  
cout<<cube;
getch();   }


QUE 6- WAP to find simple interest.

#include<iostream.h>
 #include<conio.h>
 int main()
 {clrscr(); 
int p, r, t, SI; 
cout<<"Enter the value of p:";
 cin>>p; 
cout<<"Enter the value of r:"; 
cin>>r; 
cout<<"Enter the value of t:"; 
cin>>t;
 SI=p*r*t/100; 
cout<<"The SI of given values is:"; 
cout<<SI; 
getch();
return 0;}
 
QUE 7- WAP to check a no. is even or odd.
#include<iostream.h>
 #include<conio.h> 
int main()
 {clrscr();
 char ch;
 int num, i; 
cout<<"Enter the num:";
 cin>>num; 
if(num%2==0)cout<<"even num";elsecout<<"odd num"; 
getch(); 
return 0;}


QUE 8- WAP to check a given year is leap or not
#include<iostream.h> 
#include<conio.h> 
int main() 
{clrscr();
 int year;
 cout<<"Enter year(in 4-digits)\n";
 cin>>year; 
if(year%100==0){if(year%400==0)  cout<<"leap year\n";
 }else if(year%4==0)cout<<"leapyear\n";
 elsecout<<"not a leap year\n"; 
getch(); 
return 0;}



QUE 9- WAP to find greatest no. between two nos
#include<iostream.h> 
#include<conio.h> 
int main() 
{clrscr();
 int a, b;
 cout<<"Enter the value of a:";
 cin>>a; 
cout<<"Enter the value of b:"; 
cin>>b; 
if(a>b)cout<<"a is greater than b:";
 elsecout<<"b is greater than a:"; 
getch(); 
return 0;}

QUE -10 WAP to find greatest no. among three nos.    
#include<iostream.h> 
#include<conio.h>    
int main()   
 {     int x, y, z, max; 
 cout<<"Enter three numbers :";   
  cin>> x>> y>> z;     max =x;   
  if(y>max)     max =y;     if(z>max)     max =z;  
  cout<<"\n"<<"the largest of"<<x<<","<<y<<"&"<<z<<"is"<<max;  
  getch();   
 return 0;    }

QUE 11- WAP to check a no. is negative , positive, or zero.
#include<iostream.h>
#include<conio.h>
int main()
{ clrscr();
int no;
cout<<"Enter the value of no:";
cin>>no;
if(no==0)cout<<"zero no";
elseif(no>0)cout<<"positive no";
elseif(no<0)cout<<"negative no";
getch();
return 0;}



QUE 12- WAP which accept percentage marks of student and print his result as given criteria:
pm>35                                        failpm>=35 and pm<50                  3rd division
pm>=50 and pm <60                 2nd divisionpm>=60 and pm<95                  1st  division
otherwise student passed with  distinction

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{clrscr();
int pm;
cout<<"Enter the value of pm:";
cin>>pm;
if(pm<35)cout<<"/n fail";
if(pm>=35 && pm <50)cout<<"/n third division";
if(pm>=50 && pm <60)cout<<"/n second division";
if (pm>=60 && pm <95)cout<<"/n first division";
if(pm>95)cout<<"/n distinction";
elsecout<<"";
getch();}

QUE 13- WAP to calculate sum of each row and each column of a 2d array.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
char ch(); 
 int i,a[10][20],j,sum; 
 for(i=0;i<3;i++)  
{  for(j=0;j<2;j++) 
 { cout<<"enter the no for row="<<i+1<<"and col="<<j+1<<"\n";
  cin>>a[i][j];  }  }  cout<<a; 
    for(i=0;i<3;i++)  {  for(j=0;j<2;j++)  
  {  cout<<a[i][j]<<"\t"; 
   }getch();  cout<<"\n";
  }  for(j=0;j<2;j++)  
   { sum=0;  for(i=0;i<3;i++)  
   {  cout<<"sum of"<<j+1<<" column"<<"\n"<<sum;
  getch(); 


QUE 14- WAP to convert temperature given in celsius into fahrenheit and vice versa.
#include<iostream.h>
#include<conio.h>
int main()
{clrscr();
int choice;double temp, conv_temp;
cout<<"Temperature conversion menu"<<"\n";
cout<<"1.Fahrenheit to ceisius"<<"\n";
cout<<"2.celsius to fahrenheit"<<"\n";
cout<<"Enter your choice (1-2):";
cin>>choice;
if(choice==1){cout<<"\n"<<"Enter temperature in fahrenheit:";
cin>>temp;conv_temp=(temp-32)/1.8;
cout<<"the temperature in celcius is "<< conv_temp<<"\n";}
else{cout<<"\n"<<"Enter temperature in celcius:";
cin>>temp;
conv_temp=(1.8*temp)+32;
cout<<"The temperature in fahrenheit is"<<conv_temp<<"\n";
}
getch();
return 0;

QUE 15- WAP to make a calculator which has all the arithmetic operations e.g. +,-,*,/,% and remainder through switch case statement.

#include<iostream.h>
#include<conio.h>
int main()
{  clrscr();   
float op1, op2,  op3, res; 
 char ch;   
cout<<"Enter two numbers:"; 
  cin>>op1>>op2; 
  cout<<"\n"<<"Enter the operator(+,-,*,/,%):";
  cin>>ch;  
 cout<< "\n";  
 switch(ch)   {  case+'+':res=op1+op2;                 
                                   break ; 
                          case'-':res=op1-op2;           
                                   break ;      
                          case'*': res=op1*op2;      
                                   break ;    
                          case'/':if(op2==0)    
       
             cout<<"divide by zero error!!!";  
                    else                              
               res=op2/op1;                 
                                    break ;       
                  case'%':if(op2==0)            
                        cout<<"divide by zero error!!!";                   
                             else                     
                  {  int r, q;                   
                         q= op2/op1;                    
                        r= op2-(q*op1);     
                        res=r;                 
                       }                   
                break ;  
               default:cout<<"\n"<<"wrong operator!!!";   }
              cout<<"The calculated result is:"<<res<<"\n"; 
             getch(); 
             return 0;   } 
OUE 16- WAP to calculate the table of any no. 
#include<iostream.h>
 #include<conio.h>
 int main()
 {clrscr();
 int num; 
cout<<"\n enter number:";
 cin>>num; 
for(int i=1; i<11; ++i)cout<<"\n"<<num<<"*"<<i    <<"="<<num*i;
 getch();
 return 0;}


QUE 17- WAP to find factorial of any no. 
#include<iostream.h>
#include<conio.h> 
int main() 
{clrscr();
 unsigned long i , num, fact=1;cout<<"Enter integer:";cin>>num;i= num;while(num){fact=fact* num; --num;}cout <<"the factorial of"<<i<<"is"<<fact<<"\n";getch();return 0;}

QUE 18- WAP TO PRINT THE FIBONACCI SERIES UP TO NTERM.
 #include<iostream.h>
 #include<conio.h> 
int main() 
{clrscr();
 unsigned long first, second, third, n;first=0; 
second =1; 
cout<<"How many element(>5)?\n"; 
cin>>n; 
cout<<"Fibonacci series\n"; 
cout<<first<< " "<<second;for(int i=2; i<n; ++i){third= first+second;cout<<" "<<third;first =second;second=third; 
}
 getch(); 
return 0;}

QUE 19- WAP to find sum of the digit of a no
.#include <iostream.h>
#include<conio.h> 
int main() 
{clrscr(); 
int num;int sum = 0; 
cout << " Enter a number : ";
 cin >> num; 
while ( num > 0 ) {sum += num % 10;num /= 10; 
}cout << "Sum = " << sum; 
getch(); 
return 0; 
}
                                      OR 
USING FUNCTIONS 
#include<iostream.h> 
#include<conio.h>
 int main() 
{clrscr(); 
int sum(int); 
int s,i; 
cout<<"Enter the digit whose sum be calculated:"; 
cin>>s; 
i=sum(s); 
cout<<"the sum of"<<"s is:"<<i; 
getch(); 
return 0;
 }int sum(int s)
 {int res, sum=0; 
    res=s%10;s=s/10; 
    sum=res+sum; 
    return sum;
 }

QUE 20- WAP to check whether a given no. is prime or not.
#include<iostream.h>
 #include<conio.h>
 #include<process.h>
 int main() 
{clrscr(); 
 int num, i;
 cout<<"\n Enter the number:";
 cin>>num; 
for(i=2; i<=num/2; ++i)   
 if(num%i==0)  
  {    cout<<"\not a prime number!!!";   
       exit(0);  
  }cout<<"\n it is prime number:"; 
getch();
 return 0;}

QUE 21- WAP to find out the electricity bill as given in following criteria when unit consumed input by user (monthly rent is Rs120 to charged from every consumer).
Unit consumed                                     Rate per unitFirst 100                                   Rs 1.50Next 200                                                      RS 2.00Next500                                             Rs4.00Beyond850                                          Rs 5.00

 #include<iostream.h>
 #include<conio.h>
 int main() 
{clrscr(); 
 float a; 
cout<<"Enter the unit consumed :";
 cin>>a; 
cout<<"The electricity bill is :"<<"Rs"<<120+(a*1.50);
 if (a>10&&a<=350)cout<<"The electricity bill is :"<<"Rs"<<120+150+500+((a-350)*4.00); 
if(a>850);
cout<<"The electricity bill is :"<<"Rs"<<120+150+500+2000+((a-850)*5.00); 
getch(); 
return 0;}



 Que 22:-GCD of a number.
 #include<iostream.h>
 #include<conio.h>
 int main()
 { clrscr(); 
 int a,b,temp;  
cout<<"Enter the 1 integer:"; 
 cin>>a; 
 cout<<"Enter the 2 integer:"; 
 cin>>b; 
 while(b!=0){  temp=a%b;  a=b;  b=temp;  
}  cout<<a<<endl; 
 getch(); 
 return 0; 
 }


QUE 23- WAP to find the sum of following series:1+(1+2)+(1+2+3)+...........N.
 #include<iostream.h> 
#include<conio.h> 
int main()
{clrscr();
 int n; 
cout<<"Enter the num:"; 
cin>>n; 
int i,j, sum=0; 
for(i=1; i<=n; i++){for(j=1; j<=i; j++){sum+=j;
 }}cout<<sum<<"\n"; 
getch(); 
return 0; 
}

No comments:

Post a Comment