Friday, November 6, 2015

Sample Programming

1)   Write program that takes two values as input and print Sum, Product, Difference, Quotient and Remainder.
#include<iostream.h>
#include<conio.h>
void main()
{
int no1,no2,sum,product,difference,quotient,rem;
clrscr();
cout<<"Enter 1st Number ";
cin>>no1;
cout<<"Enter 2nd Number ";
cin>>no2;
sum=no1+no2;
product=no1*no2;
difference=no1-no2;
quotient=no1/no2;
rem=no1%no2;
cout<<"sum is = "<<sum<<"\n";
cout<<"Product is = "<<product<<"\n";
cout<<"Difference between two value = "<<difference<<"\n";
cout<<"Quotient is =  "<<quotient<<"\n";
cout<<"Remainder is = "<<rem<<"\n";
getch();
}




2)   Write program that inputs a 4-digit integer and display the number in reverse order. Compute and print Sum, Product and average of its digits.
#include<iostream.h>
#include<conio.h>
void main()
{
int no,r1,r2,r3,c1,c2,c3,sum,product;
float avg;
clrscr();
sum=0;
cout<<"Enter 4digit numbers ";
cin>>no;
c1=no/10;
r1=no%10;
c2=c1/10;
r2=c1%10;
c3=c2/10;
r3=c2%10;
sum=r1+r2+r3+c3;
product=r1*r2*r3*c3;
avg=sum/4;
cout<<"Reverse order is = "<<r1<<r2<<r3<<c3<<"\n";
cout<<"Sum is = "<<sum<<"\n";
cout<<"Product is = "<<product<<"\n";
cout<<"Average is = "<<avg<<"\n";
getch();
}


3)   Write program that inputs a 3-digit integer number and display its first and last digits and their sum.

#include<iostream.h>
#include<conio.h>
void main()
{
int no,c,r,sum;
clrscr();
sum=0;
cout<<"Enter 3digit Number ";
cin>>no;
c=no/100;
r=no%10;
sum=c+r;
cout<<"First Digit is ="<<c<<"\n"<<"Last Digit is = "<<r<<"\n";
cout<<"Sum is = "<<sum<<"\n";
getch();
}

4)   Write program that read a 3-digited integer and display ASCII    character of its middle digit.

#include<iostream.h>
#include<conio.h>
void main()
{
int no,r1,c1,r2,c2;
char ch;
clrscr();

cout<<"Enter 3-digit Number ";
cin>>no;
c1=no/10;
r1=no%10;
c2=c1/10;
r2=c1%10;
ch=r2;
cout<<ch;
getch();
}

5)   Write a program that inputs an integer and display its Square , Cube , and Square root.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int no,square,cube,square_root;
clrscr();
cout<<"Enter a number ";
cin>>no;
square=no*no;
cube=no*no*no;
square_root=sqrt(no);
cout<<"Square is = "<<square<<"\n";
cout<<"Cube is = "<<cube<<"\n";
cout<<"Square root is =  "<<square_root<<"\n";
getch();

}

6)             Write program to input two numbers x and y and compute xy

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,y,ans;
clrscr();
cout<<"Enter a value of x ";
cin>>x;
cout<<"Enter a value of y ";
cin>>y;
ans=pow(x,y);
cout<<ans;
getch();
}

7)   Write program to input three sides a,  b   and c of triangle and compute its area
          Formula:  s = (a+b+c)/2
         
           area =
 
#include<math.h>
#include<iostream.h>
#include<conio.h>
void main()

{
int a,b,c,s,area;
clrscr();
cout<<"Enter 1st side ";
cin>>a;
cout<<"Enter 2nd side ";
cin>>b;
cout<<"Enter 3rd side ";
cin>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area is = "<<area;
getch();
}

8)    Write program that calculate area of triangle when base and height are entered from keyboard.
          Formula: area=   ½*base*height

#include<iostream.h>
#include<conio.h>
void main()
{
float base,height,area;
clrscr();
cout<<"Enter base value";
cin>>base;
cout<<"Enter Height value";
cin>>height;
area=0.5*base*height;

cout<<"Area value is = "<<area;
getch();
}

9)   WAP that convert temperature from Centigrade to Fahrenheit.
          Formula:  F=9/5*c+32;

#include<iostream.h>
#include<conio.h>
void main()
{
float celsius,fahrenheit;
clrscr();
cout<<"Enter the temperature in celcius";
cin>>celsius;
fahrenheit=9.0/5.0*celsius+32;
cout<<"Temperature in fahrenheit is = "<<fahrenheit;
getch();
}

10)                      WAP that convert temperature from  Fahrenheit to       Centigrade.
      Formula:  c=5/9*(f-32);

#include<iostream.h>
#include<conio.h>
void main()
{
float celsius,faren;
clrscr();
cout<<"Enter the temperature in farenheit";

cin>>faren;
celsius=5.0/9.0*(faren-32);
cout<<"Temperature is celsius is = "<<celsius;
getch();
}

11)                      Write a program that Swap values of two variables without using third variable.

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<"Enter 1st Number ";
cin>>a;
cout<<"Enter 2nd Number ";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"1st Number is = "<<a<<"\n";
cout<<"2nd Number is = "<<b<<"\n";
getch();
}

12)                      Write program that read the distance between two cities in kilometers and change it into Meters, Feet, Inches, Centimeters & Millimeters.

#include<iostream.h>
#include<conio.h>
void main()
{
float km,m,inch,cm,feet,mm;
clrscr();
cout<<"Enter Distance between two cities ";
cin>>km;
m=km*1000;
feet=km*3280.84;
inch=km*39370.1;
cm=km*100000;
mm=km*1000000
cout<<"Distance in meter is = "<<m<<"\n";
cout<<"Distance in feet is = "<<feet<<"\n";
cout<<"Distance in inches is = "<<inch<<"\n";
cout<<"Distance in centimeter is = "<<cm<<"\n";
cout<<”Distance in millimeter is = ”<<mm<<”\n”;
getch();
}

13)                      Write program that read age in Year and convert it into Month, Weeks, Days and Hours.

#include<iostream.h>
#include<conio.h>
void main()
{
int year,month,weeks,days,hours;
clrscr();
cout<<"Enter a year ";

cin>>year;
month=year*12;
weeks=month*4;
days=year*365;
hours=days*24;
cout<<"Age in month is = "<<month<<"\n";
cout<<"Age in weeks is = "<<weeks<<"\n";
cout<<"Age in days is = "<<days<<"\n";
cout<<"Age in hours is = "<<hours<<"\n";
getch();
}

14)                      Write program that inputs a character from Keyboard and display its ASCII code.

#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
int a;
clrscr();
cout<<"Enter any character ";
cin>>ch;
a=ch;
cout<<"ASCII Code is = "<<a;
getch();
}



15)                      Write program that inputs ASCII Code from Keyboard and display its ASCII character.

#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
int a;
clrscr();
cout<<"Enter any ASCII code ";
cin>>a;
ch=a;
cout<<"Character is = "<<ch;
getch();
}

16)                      Write program to find out the right most digit of an input integer.

#include<iostream.h>
#include<conio.h>
void main()
{
int no,a,b;
clrscr();
cout<<"Enter a 4digit number ";
cin>>no;
a=no/1000;
b=no%10;
cout<<"Right most digit is = "<<b;

getch();
}

17)                      Write program that inputs a lowercase letter and display corresponding uppercase letter and vice versa.

#include<iostream.h>
#include<conio.h>
void main()
{
char ch1,ch2;
clrscr();
cout<<"Enter a character ";
cin>>ch1;
ch2=ch1-32;
cout<<"Upper case letter is = "<<ch2;
getch();
}

18)                      Write program that inputs amount in Rupees and convert it into Dollars
             (1 Dollar = 105 Rupees)

#include<iostream.h>
#include<conio.h>
void main()
{
int rupees,dollar;
clrscr();
cout<<"Enter amount in Rupees ";

cin>>rupees;
dollar=rupees/105;
cout<<"Dollar amount is = "<<dollar;
getch();
}

19)                      Write program that inputs marks of a student in four subjects and compute Total Marks and Average.

#include<iostream.h>
#include<conio.h>
void main()
{
int sub1,sub2,sub3,sub4,total_marks;
float avg;
clrscr();
cout<<"Enter 1st subject marks ";
cin>>sub1;
cout<<"Enter 2nd subject marks ";
cin>>sub2;
cout<<"Enter 3rd subject marks ";
cin>>sub3;
cout<<"Enter 4th subject marks ";
cin>>sub4;
total_marks=sub1+sub2+sub3+sub4;
avg=total_marks/4;
cout<<"Total Marks is = "<<total_marks<<"\n";
cout<<"Average is = "<<avg<<"\n";
getch();
}

20)                      Write program that inputs a floating point number from user. Then separate and display its integer and fractional parts on screen.
          e.g. if user enters 65.375 then display
           integer part = 65
           Fractional part = 0.375

#include<iostream.h>
#include<conio.h>
void main()
{
float no,f;
int i;
clrscr();
cout<<"Enter floating number ";
cin>>no;
i=no;
f=no-i;
cout<<"integer part is = "<<i<<"\n";
cout<<"folating part is = "<<f<<"\n";
getch();
}

21)                      Write program that takes 5-digit integer number from user. Then make its three middle digits 0. E.g. if input number is 42935, then output should be 40005. Store and print the result as a single variable.

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

{
long int no,r1,c1,r2,c2;
clrscr();
cout<<"Enter 5 digits number ";
cin>>no;
c1=no/10000;
r1=no%10;
r2=c1*10000;
c2=r2+r1;
cout<<c2;
getch();
}

22)                      Write program that takes 5-digit integer and print its each digit on a separate line. E.g. if we input number 34567 output should be
3                 4                 5                 6                 7

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main()
{
long int no,c1,c2,c3,c4,r1,r2,r3,r4;
clrscr();
cout<<"Enter 5digit Number ";
cin>>no;
c1=no/10;
r1=no%10;
c2=c1/10;
r2=c1%10;


c3=c2/10;
r3=c2%10;
c4=c3/10;
r4=c3%10;
cout<<c4<<setw(10)<<r4<<setw(10)<<r3<<setw(10)<<r2<<setw(10)<<r1;
getch();
}

23)                      Write program that inputs radius of circle and compute and print area and circumference.

#include<iostream.h>
#include<conio.h>
void main()
{
float radius,area,circumference;
clrscr();
cout<<"Enter radius value ";
cin>>radius;
area=3.14*radius*radius;
circumference=2*3.14*radius;
cout<<"Area value is = "<<area<<"\n";
cout<<"Circumference value is = "<<circumference<<"\n";
getch();
}

24)                      Write program that inputs coordinates of two points P(x1,y1) and Q(x2,y2) and print distance between two points.
Formula:  Distance =


#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int x1,y1,x2,y2,distance;
clrscr();
cout<<"Enter 1st point x1 ";
cin>>x1;
cout<<"Enter 2nd point y1 ";
cin>>y1;
cout<<"Enter 2st point x2 ";
cin>>x2;
cout<<"Enter 2nd point y2 ";
cin>>y2;
distance=pow((x1-x2)*(x1-x2)*(y1-y2)*(y1-y2),0.5);
cout<<""<<distance;
getch();
}

25)                      Write program that input Basic salary of an employee from keyboard and compute 25% House rent, 15% Medical Allowance, 35% Dearness Allowance and print Gross Salary.

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


long int basic_salary,house_rent,medical_allowance,dearness_allowance,gross_profit;
clrscr();
cout<<"Enter your basic salary ";
cin>>basic_salary;
house_rent=basic_salary*25/100;
medical_allowance=basic_salary*15/100;
dearness_allowance=basic_salary*35/100;
gross_profit=house_rent+medical_allowance+dearness_allowance;
cout<<"Gross Profit is = "<<gross_profit;
getch();
}

26)                      Write program that prompts the user to enter number of hours. It calculates and print number of weeks , Days, and Hours within the centered number of hours.

#include<iostream.h>
#include<conio.h>
void main()
{
int hours,weeks,days;
clrscr();
cout<<"Enter number of hours ";
cin>>hours;
weeks=hours/168;
hours=hours%168;
days=hours/24;
hours=hours%24;
cout<<"Weeks is = "<<weeks<<"\n";


cout<<"Days is  = "<<days<<"\n";
cout<<"Hours is = "<<hours<<"\n";
getch();
}

27)                      Write program that input time in second and converts into hours minutes and seconds.

#include<iostream.h>
#include<conio.h>
void main()
{
int sec,s,m,h;
clrscr();
cout<<"Enter time in second ";
cin>>sec;
h=sec/3600;
sec=sec%3600;
m=sec/60;
s=sec%60;
cout<<"HH-MM-SS="<<h<<":"<<m<<":"<<s;
getch();
}

28)                      Write a program that inputs two floating point numbers, divide them and print the results rounded to two digits after decimal point.

#include<iostream.h>


#include<conio.h>
#include<iomanip.h>
void main()
{
float no1,no2,ans;
clrscr();
cout<<"Enter 1st number ";
cin>>no1;
cout<<"Enter 2nd number ";
cin>>no2;
ans=no1/no2;
cout<<"Answer is = "<<setprecision(2)<<ans;
getch();
}

29)                      Write program to input multi-word strings name , address and city from user and print them on three separate line.

#include<iostream.h>
#include<conio.h>
void main()
{
char name[30],address[30],city[30];
clrscr();
cout<<"Enter your Name ";
cin>>name;
cout<<"Enter your Address ";
cin>>address;
cout<<"Enter your city Name ";


cin>>city;
cout<<name<<"\n"<<address<<"\n"<<city;
getch();
}

30)                      Write program that input two letters from user and print next two letters.

#include<iostream.h>
#include<conio.h>
void main()
{
char ch1;
clrscr();
cout<<"Enter Two litter ";
cin>>ch1;
cout<<(char)(ch1+2)<<"\n";
cout<<(char)(ch1+3)<<"\n";
getch();
}