Anda di halaman 1dari 18

Programs on c++

WAP that takes input of a radius of a circle and


displays its circumference and area
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float r,ra,cir;
cout<<"\nenter radius of circle";
cin>>r;
ra=3.14*r*r;
cout<<"\nradius"<<ra;
r=2*3.14*r;
cout<<"\ncircumference"<<r;
getch();
}

WAP that takes input as height and


base-length of a triangle and displays
its area
#include<iostream.h>

#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c,s,area;
cin>>a;
cin>>b;
cin>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"\narea="<<area;
getch();
}

WAP that interchanges the values


of two numbers
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
scanf("%d%d",&a,&b);

a=a+b;
b=a-b;
a=a-b;
printf("a=%d b=%d",a,b);
getch();
}

WAP that computes simple


interest for a particular year.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float p,r,t,si;
cin>>p;
cin>>r;
cin>>t;
si=(p*r*t)/100;
cout<<si;
getch();
}

conditional
operator

WAP to calculate simple interest if time is

morer than 10 take rate =10 otherwise 8


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float p,t,si;
cout<<"\nenter principal amount";
cin>>p;
cout<<"\nenter time";
cin>>t;
si=(t>=10)?((p*8*t)/100):((p*12*t)/100);
cout<<"\nsimple interest is"<<si;
getch();
}
if time is more than 10 years

If time is less than 10 years

Write a C++ program to input a


student type (A or B). If the
student type is A initialize the
college account with Rs 200/otherwise initialize the hostel
account with Rs. 200/-.
#include<iostream.h>
#include<conio.h>
void main()

{
clrscr();
int n;
cout<<"\nenter age";
cin>>n;
(n>=18)?cout<<"\neligible
vote":cout<<"\nnot eligible to vote";
getch();
}
if it of student A

to

if is of student B

WAP to accept a number and if it


is even print its square otherwise
cube
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a,r;
cout<<"\nenter your number";
cin>>a;

r=((a/2)%==0)?(a*a):(a*a*a);
cout<<"\nresult is"<<r;
getch();
}

If number is even

If number is odd

WAP to display the maximum of


two numbers.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cin>>a;
cin>>b;
(a>b)?cout<<a:cout<<b;
getch();
}

WAP to input radius of the circle


according to the choice of user if user
enters 1 print area otherwise
circumference
#include<iostream.h>
#include<conio.h>
void main()
{

clrscr();
float n,r,out;
cout<<"\nenter radius of the circle";
cin>>n;
cout<<"\nenter 1 for area and 2 for perimeter";
cin>>r;
out=(r==1)?(3.14*n*n):(2*3.14*n);
cout<<"\nyour output is"<<out;
getch();
}
If user enter 1

If user enter 2

WAP to check whether a person is eligible


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

{
clrscr();
int n;
cout<<"\nenter age";
cin>>n;
(n>=18)?cout<<"\neligible to vote":cout<<"\nnot eligible to
vote";
getch();
}
if age is less than 18

if age is more than 18

WAP to check whether the input


number is even or not?
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<"\nenter no";
cin>>n;
n%2==0?cout<<"\neven":cout<<"\nodd";
getch();
}
if no is even

if no is odd

structure

Write a program that inputs a number between 1 and


7 and display the corresponding day of the week ?
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{clrscr();
int d;
cout<<"enter a number between 1-7";
cin>>d;
if (d==1)
cout<<"\n monday";
else if (d==2)
cout<<"\n tuesday";
else if (d==3)
cout<<"\n wednesday";
else if (d==4)
cout<<"\n thursday";
else if (d==5)
cout<<"\n friday";
else if (d==6)
cout<<"\n saturday";
else if (d==7)
cout<<"\n sunday";

else
cout<<"Enter a number between 1-7";
getch ();
}

WAP to input a no from user from


1 to 12 show the corresponding
month and days of the month
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int m;
cout<<"enter a number between 1 to 12";
cin>>m;
if(m==1)
cout<<"jaunary=31";
cin>>m;
if(m==2)
cout<<"FEUBARY=28";
if(m==3)
cout<<"MARCH=31";
if(m==4)
cout<<"APRIL=30";
if(m==5)
cout<<"MAY=31";

if(m==6)
cout<<"JUNE=30";
if(m==7)
cout<<"JULY=31";
if(m==8)
cout<<"AUGUST=31";
if(m==9)
cout<<"SEPTEMBER=30";
if(m==10)
cout<<"OCTOBER=31";
if(m==11)
cout<<"NOVEMBER=30";
if(m==12)
cout<<"DECEMBER=31";
getch();
}

WAP to calculate 5 basic


opearations like a calculator
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
char ch;

clrscr();
cout<<"\nenter first number";
cin>>a;
cout<<"\nenter second number";
cin>>b;
cout<<"\nenter the operator";
cin>>ch;
if (ch=='+')
cout<<a+b;
else if(ch=='-')
cout<<a-b;
else if(ch=='*')
cout<<a*b;
else if(ch=='/')
cout<<a/b;
else if
(ch=='%')
cout<<a%b;
getch();
}

Write a programme to input a


character and display a vowel
otherwise display constant.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ char A;

cin>>A;
if((A=='a')||(A=='e')||(A=='i')||(A=='o')||(A=='u'))
cout<<"vowel";
else
cout<<"consonant";
getch();
}

WAP to input tha amount of money from


user and find how many notes of
500,200,100,50,20,10,5,2,1,are needed.
#include <iostream.h>
#include<conio.h>

//using namespace std;

int main()
{
clrscr();
int val;
int Note500=0;
int Note200=0;
int Note100=0;
int Note50=0;
int Note20=0;
int Note10=0;
int Note5=0;
int Note2=0;

int Note1=0;
cin>> val;
while(val!=0)
{
if(val>=500)
{
Note500++;
val=val-500;
}
else

if(val>=200)

{
Note200++;
val=val-200;
}
else if( val>=100)
{
Note100++;
val=val-100;
}
else if(val>=50)
{
Note50++;
val=val-50;
}
else if(val>=20)
{
Note20++;

val=val-20;
}
else if(val>=10)
{
Note10++;
val=val-10;
}
else if(val>=5)
{
Note5++;
val=val-5;
}
else if(val>=2)
{
Note2++;
val=val-2;
}
else if(val>=1)
{
Note1++;
val=val-1;
}

cout<<"\nNote500"<<note500<<"\nNote200"<<note200<<"\nNote100"<<
note100<<"\nNote50"<<note50<<"\nNote20"<<note20<<"\nNote10"<<<

note10<"\nNote5"<<<note5<"\nNote2"<<"\\nNote1";
getch();
return 0;

WAP to display the first 10


natural number?
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=10;++i)
cout<<i<<endl;
getch();
}

WAP to display even numbers


from 1 to 100 ?
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=0;i<=100;i=i+2)
{cout<<i<<endl;
}
getch();
}

WAP to display even numbers


from 1 to 100 ?
#include<iostream.h>
#include<conio.h>
void main()

{
clrscr();
int i,sum;
for(i=0;i<=100;i=i+2)
{sum=sum+i;
}
cout<<sum;
getch();
}

WAP to generate the divisors of


an integer?
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cin>>n;
for(i=1;i<=n;++i)
{if(n%i==0)
cout<<i<<endl;
}
getch();
}

WAP to find out Armstrong


numbers 3
Eg. 153=1 + 53 + 33
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,rem,ans;

int b;
cout<<"Enter the number";
cin>>a;
b=a;
ans=0;
while(a!=0)
{ rem=a%10;
ans=ans+(rem*rem*rem);
a=a/10;
}
if(ans==b)
cout<<"\nAmstrong number";
else
cout<<"Not a Armstrong number";
getch();
}

Anda mungkin juga menyukai