100 program
100 program
Input: #include<iostream.h>
int main()
{
cout<<"Program = 20"<<endl<<"Documentation = 23"<<endl
<<"Logic = 21"<<endl<<"Flow chart = 18"<<endl;
return 0;
}
Output:
PROGRAM 5:
Aim: Write a program that reads temperature in Celsius and displays it in Fahrenheit.
Input: #include<iostream.h>
int main()
{
double C,F;
cout<<"Enter temperature in Celsius:";
cin>>C;
F=(9*C)/5+32;
cout<<"The temperature in Fahrenheit is "<<F<<"F"<<endl;
return 0;
}
Output:
PROGRAM 5:
Aim: Write a program to read values of w, x, y and z and display the value of P, where
P=w+x/y-z
Input: #include<iostream.h>
int main()
{
int w,x,y,z,p;
cout<<"Enter the value w:";
cin>>w;
cout<<"Enter the value x:";
cin>>x;
cout<<"Enter the value y:";
cin>>y;
cout<<"Enter the value z:";
cin>>z;
p=(w+x)/(y-z);
cout<<"The values of P is "<<p<<endl;
return 0;
}
Output:
PROGRAM 5:
Aim: Write a program that displays on screen
a. the character ‘z’ b. the name ‘Mohan’
c. the number 1977
Using (i) single cout statement (ii) multiple cout statements.
Input(i): #include<iostream.h>
int main()
{
cout<<"z\t"<<"Mohan\t"<<"1977"<<endl;
return 0;
}
Input(ii): #include<iostream.h>
int main()
{
cout<<"z\t";
cout<<"Mohan\t";
cout<<"1977\t"<<endl;
return 0;
}
Output:
PROGRAM 6:
Aim: Assuming that there are 7.481 gallons in a cubic foot, write a program that asks the user
to enter the user to enter the number of gallons, and then display the equivalent in cubic feet.
Input: #include<iostream.h>
int main()
{
float gallons,cufeet;
cout<<"Enter quantity in gallons:";
cin>>gallons;
cufeet=gallons/7.481;
cout<<"Equivalent in cublic feet is"<<cufeet<<endl;
return 0;
}
Output:
PROGRAM 7:
Aim: Write a program to generate the following table:
1992 17421
1993 29210
1994 100523
1995 106802
1996 127000
Use a single cout statement for output.
Input: #include<iostream.h>
int main()
{
cout<<"1992\t17421\n"<<"1993\t29210\n"<<"1994\t100523\n"
<<"1995\t106802\n"<<"1996\t127000\n";
return 0;
}
Output:
PROGRAM 8:
Aim: Write a program that generates the following output :
5
10
9
Assign value 5 to a variable using = Multiply it with 2 to generate 10 and subtract 1 to
generate 9.
Input: #include<iostream.h>
int main()
{
int a=5;
cout<<a<<endl;
a*= 2;
cout<<a<<endl;
a-= 1;
cout<<a<<endl;
return 0;
}
Output:
PROGRAM 9:
Aim: Write a C++ program that accepts radius of a circle and prints its area.
Input: #include<iostream.h>
int main()
{
int radius,area;
cout<<"Enter radius of circle:";
cin>>radius;
area=(22/7)*radius*radius;
cout<<"The area of the circle is:"<<area<<endl;
return 0;
}
Output:
PROGRAM 10:
Aim: Write a C++ program that accepts marks in 5 subjects and outputs average marks.
Input: #include<iostream.h>
int main()
{
int s1,s2,s3,s4,s5,sum,avg;
cout<<"Enter the marks in first subject:";
cin>>s1;
cout<<"Enter the marks in second subject:";
cin>>s2;
cout<<"Enter the marks in third subject:";
cin>>s3;
cout<<"Enter the marks in forth subject:";
cin>>s4;
cout<<"Enter the marks in fifth subject:";
cin>>s5;
sum=s1+s2+s3+s4+s5;
avg=sum/5;
cout<<"Average marks:"<<avg<<endl;
return 0;
}
Output:
PROGRAM 11:
Aim: Write a program to input an integer and display its first three multiples.
Input: #include<iostream.h>
int main()
{
int number,multiple1,multiple2,multiple3;
cout<<"Enter a number:";
cin>>number;
multiple1=number*1;
multiple2=number*2;
multiple3=number*3;
cout<<"First three multiples of given number are:";
cout<<multiple1<<","<<multiple2<<","<<multiple3<<endl;
return 0;
}
Output 1:
Output 2:
PROGRAM 12:
Aim: Write a program to display ASCII code of a character and vice versa.
Input: #include<iostream.h>
int main()
{
char ch='A';
int num=ch;
cout<<"The ASCII code for "<<ch<<" is "<<num<<"\n";
cout<<"Adding 1 to the character code\n";
ch=ch+1;
num=ch;
cout<<"The ASCII code for "<<ch<<" is "<<num<<"\n";
return 0;
}
Output:
PROGRAM 13:
Aim: Write a program to calculate and display area of circle with radius 6 cm.
Input: #include<iostream.h>
int main()
{
float radius=6.0;
float area;
area=3.14159*radius*radius;
cout<<"Area of the circle is:"<<area<<endl;
return 0;
}
Output:
PROGRAM 14:
Aim: Write a program (using a function) to accept a number and print its cube.
Input: #include<iostream.h>
#include<stdlib.h>
float cube(float);
int main()
{
system("cls");
float num;
cout<<"Enter a number:";
cin>>num;
cout<<"The cube of "<<num<<" is "
<<cube(num)<<"\n";
return 0;
}
float cube(float a)
{
return a*a*a;
}
Output 1:
Output 2:
PROGRAM 15:
Aim: Write a program to compute the area of a square. Make assumptions on your own.
Input: #include<iostream.h>
int main()
{
float side,area=float();
cout<<"Enter side of the square:";
cin>>side;
area=side*side;
cout<<"\nThe area of the square is:";
<<area<<"square-units"<<endl;
return 0;
}
Output 1:
Output 2:
PROGRAM 16:
Aim: Write a program that accepts a character between a to j and print next 4 characters.
Input: #include<iostream.h>
int main()
{
char ch,ch1,ch2,ch3,ch4;
cout<<"Enter a character between A and V:";
cin>>ch;
int num=ch;
ch1=ch+1;
ch2=ch+2;
ch3=ch+3;
ch4=ch+4;
cout<<"\n Next Four characters are:";
cout<<"\n"<<ch1<<"\n"
<<ch2<<"\n"
<<ch3<<"\n"
<<ch4<<"\n";
return 0;
}
Output 1:
Output 2:
PROGRAM 17:
Aim: Write a program that inputs experience and age of a person. The salary of the person is
6000 if the person is experienced and his age is more than 35, otherwise if the person is
experienced and his age is more than 28 but less 35 than the salary should be 4800 otherwise
for experienced person the salary should be 3000 and for inexperienced person the salary
should be 2000.
Input: #include<iostram.h>
int main()
{
int exper,age,salary;
cout<<"The person is experienced?
<<"Enter 1 for yes, 0 for no:";
cin>>exper;
cout<<"\nEnter age of the person :";
cin>>age;
salary=(exper)?((age>35)?6000:(age>28)?4800:3000):2000;
cout<<"\nThe salary of the person is"<<salary<<"\n";
return 0;
}
Output 1:
Output 2:
Output 3:
Output 4:
PROGRAM 18:
Aim: A computer programming contest requires terms of 5 members each. Write a program
that asks for the number of players, and then give the number of terms and number of players
left over.
Input: #include<iostream.h>
int main()
{
int no_of_players, no_of_terms,leftovers;
cout<<"Enter the number of players:";
cin>>no_of_players;
no_of_terms=no_of_players/5;
leftovers=no_of_players/5;
cout<<"There will be"<<no_of_terms
<<"teams with"<<leftovers<<"left over\n";
return 0;
}
Output:
PROGRAM 19:
Aim: The value of e is known to be 2.71828…. Using this value, write a program to
determine the value of the expression :2-ye2y+4y. Obtain value of y from user.
Input: #include<iostream.h>
#include<math.h>
int main()
{
const double e=2.71828;
double result, y;
cout<<"Enter value of y:";
cin>>y;
result=2-y*exp(2*y)+pow(4,y);
cout<<"The result of given expression is: "<<result<<endl;
return 0;
}
Output 1:
Output 2:
PROGRAM 20:
Aim: Write a program to accepts three integers and print the largest of the three. Make use of
only if statement.
Input: #include<iostream.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<<" and "<<z<<" is "<<max<<endl;
return 0;
}
Output:
PROGRAM 21:
Aim: Temperature-conversion program that gives the user the option of converting
Fahrenheit to Celsius or Celsius to Fahrenheit and depending upon user’s choice carries out
the conversion.
Input: #include<iostream.h>
int main()
{
int choice;
double temp,conv_temp;
cout<<"Temperature Conversion Menu"<<"\n";
cout<<"1. Fahrenheit to Celsius"<<"\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 Celsius is "<<conv_temp<<"\n";
}
else
{
cout<<"\n"<<"Enter temperature in Celsius:";
cin>>temp;
conv_temp=(1.8*temp)+32;
cout<<"The temperature in Fahrenheit is "<<conv_temp<<"\n";
}
return 0;
}
Output 1:
Output 2:
PROGRAM 22:
Aim: Program to create the equivalent of a four-function calculator. The program requires the
user to enter two numbers and an operator. It then carries out the specified arithmetical
operation: addition, subtraction, multiplication or division of the two numbers. Finally, it
displays the result.
Input: #include<iostream.h>
int main()
{
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";
}
return 0;
}
Output 1:
Output 2:
Output 3:
Output 4:
PROGRAM 23:
Aim: Write a program to input a character and to print whether a given character is an
alphabet, digit or any other character.
Input: #include<iostream.h>
int main()
{
char ch;
cout<<"Enter a character:";
cin>>ch;
if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z')))
{
cout<<"You entered an alphabet"<<"\n";
}
else if(ch>='0'&&ch<='9')
{
cout<<"You entered a digit"<<"\n";
}
else
{
cout<<"You entered a character other than"
<<"alphabets and digits"<<"\n";
}
return 0;
}
Output 1:
Output 2:
Output 3:
Sales Commission Rate
30001 onwards 15%
22001-30000 10%
12001-22000 7%
5001-12000 3%
0-5000 0%
PROGRAM 24:
Aim: Write a program to calculate commission for the salesmen. The commission is
calculated according to following rates:
Input: #include<iostream.h>
int main()
{
float sales, comm;
cout<<"Enter sales made by the saleman:";
cin>>sales;
if(sales>5000)
{
if(sales>12000)
{
if(sales>22000)
{
if(sales>30000)
{
comm=sales*0.15;
}
else
{
comm=sales*0.10;
}
}
else
{
comm=sales*0.07;
}
}
P.T.O
else
{
comm=sales*0.03;
}
}
else
{
comm=0;
}
cout<<"\n"<<"The commission is:"<<comm<<"\n";
return 0;
}
Output 1:
Output 2:
Output 3:
PROGRAM 25:
Aim: Write a program to input number of week’s day(1-7) and translate to its equivalent
name of the day of the week.
Input: #include<iostream.h>
int main()
{
int dow;
cout<<"Enter number of week's day(1-7):";
cin>>dow;
switch(dow)
{
case 1:cout<<"\n"<<"Sunday \n";
break;
case 2:cout<<"\n"<<"Monday \n";
break;
case 3:cout<<"\n"<<"Thusday \n";
break;
case 4:cout<<"\n"<<"Wednesday \n";
break;
case 5:cout<<"\n"<<"Thursday \n";
break;
case 6:cout<<"\n"<<"Friday \n";
break;
case 7:cout<<"\n"<<"Saturday \n";
break;
default:cout<<"\n"<<"Wrong number of day \n";
break;
}
return 0;
}
Output 1:
Output 2:
PROGRAM 26:
Aim: Write a program to print whether a given character is an uppercase or a lowercase
character or a digit or any other character. Use ASCII codes for it. The ASCII codes are as
given below:
Characters ASCII Range
‘0’ – ‘9’ 48 – 57
‘A’ – ‘Z’ 65 – 90
‘a’ – ‘z’ 97 – 122
other characters 0 – 255 excliding the
above mentained code.
Input: #include<iostream.h>
int main()
{
char ch;
cout<<"Enter a character:";
cin>>ch;
if(ch>=48&&ch<=57)
{
cout<<"\n"<<"You entered a digit\n";
}
else if(ch>=65&&ch<=90)
{
cout<<"\n"<<"You entered a uppercase character\n";
}
else if(ch>=97&&ch<=122)
{
cout<<"\n"<<"You entered a lowercase character\n";
}
else
{
cout<<"\n"<<"You entered a special character\n";
}
return 0;
}
Output 1:
Output 2:
Output 3:
Output 4:
PROGRAM 27:
Aim: Write a program to calculate and print roots of a quadratic equation
ax2+bx +c =0 ( a ≠ 0 )
Input: #include<iostream.h>
#include<math.h>
int main()
{
float a,b,c,root1,root2,delta;
cout<<"Enter three numbers a, b & c of "
<<"ax^2+bx+c:\n";
cin>>a>>b>>c;
if(!a)
{
cout<<"Value of\'a/'should not be zero"
<<"\n Aborting !!!!!!"<<"\n";
}
else
{
delta=b*b-4*a*c;
if(delta>0)
{
root1=(-b+sqrt(delta))/(2*a);
root2=(-b-sqrt(delta))/(2*a);
cout<<"Roots are REAL and UNEQUAL"<<"\n";
cout<<"Root1="<<root1
<<"Root2="<<root2<<"\n";
}
else if(delta==0)
{
root1=-b/(2*a);
cout<<"Roots are REAL and EQUAL"<<"\n";
cout<<"Root1="<<root1;
cout<<"Root2="<<root2<<"\n";
}
else
cout<<"Roots are COMPLEX and IMAGINARY"<<"\n";
}
return 0;
}
Output 1:
Output 2:
PROGRAM 28:
Aim: Write a program to calculate area of a circle, a rectangle or a triangle depending upon
user’s choice.
Input: #include<iostream.h>
#include<math.h>
int main()
{
float area,rad,len,bre,a,b,c,s;
int ch;
cout<<"Area Menu"<<"\n";
cout<<"1. Circle"<<"\n";
cout<<"2. Rectangle"<<"\n";
cout<<"3. Triangle"<<"\n";
cout<<"Enter your choice:";
cin>>ch;
cout<<"\n";
switch(ch)
{
case 1: cout<<"Enter radius of the circle:";
cin>>rad;
area=3.14*rad*rad;
cout<<"\n"<<"The area of the circle is "<<area<<"\n";
break;
case 2: cout<<"Enter length and breadth of rectangle:";
cin>>len>>bre;
area=len*bre;
cout<<"\n"<<"The area of rectangle is "<<area<<"\n";
break;
case 3: cout<<"Enter three sides of a triangle:";
cin>>a>>b>>c;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"\n"<<"The area of the triangle is "<<area<<"\n";
break;
default:cout<<"Wrong choice!!!!!";
break;
}
return 0;
}
Output 1:
Output 2:
Output 3:
PROGRAM 29:
Aim: Program to illustration the working of switch in the absence of break statement.
Input: #include<iostream.h>
int main()
{
int i=0,ua=0,ub=0,uc=0,fail=0;
while(i<=5)
{
switch(i++)
{
case 1:
case 2: ++ua;
case 3:
case 4: ++ub;
case 5: ++uc;
default:++fail;
}
}
cout<<"ua="<<ua<<"\t"<<"ub="<<ub<<endl;
cout<<"uc="<<uc<<"\t"<<"fail="<<fail<<endl;
}
Output:
PROGRAM 30:
Aim: Program to illustration the working of switch in the presence of break statement.
Input: #include<iostream.h>
int main()
{
int i=0,ua=0,ub=0,uc=0,fail=0;
while(i<=5)
{
switch(i++)
{
case 1:
break;
case 2: ++ua;
break;
case 3:
break;
case 4: ++ub;
break;
case 5: ++uc;
break;
default:++fail;
}
}
cout<<"ua="<<ua<<"\t"<<"ub="<<ub<<endl;
cout<<"uc="<<uc<<"\t"<<"fail="<<fail<<endl;
}
Output:
PROGRAM 31:
Aim: Program to perform arithmetic calculation using switch. This program inputs two
operands and an operator and then displays the calculated result.
Input: #include<iostream.h>
int main()
{
float op1,op2,res;
char ch;
cout<<"Enter two numbers:";
cin>>op1>>op2;
cout<<"\n"<<"Enter an 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);
}
break;
default:cout<<"\n"<<"Wrong operator!!!";
}
cout<<"The calculated result is:"<<res<<"\n";
}
Output 1:
Output 2:
Output 3:
Output 4:
PROGRAM 32:
Aim: Program using for loop to print number from 1 to 10.
Input: #include<iostream.h>
int main()
{
int i;
for(i=1;i<=10;++i)
cout<<i<<endl;
return 0;
}
Output:
PROGRAM 33:
Aim: Program to print first n natural numbers and their sum.
Input: #include<iostream.h>
int main()
{
int i,sum,n;
cout<<"How many natural numbers? ";
cin>>n;
for(i=1,sum=0;i<=n;++i)
{
cout<<"\n"<<i;
sum=sum+i;
}
cout<<"\n"<<"The sum of first"<<n
<<"natural number is:"<<sum
<<"\n";
return 0;
}
Output:
PROGRAM 34:
Aim: Program to tabulate the function
2
f ( x )=( x + 1.5 x +5)/( x −3)
For x=−10 to 10 , xshould take values −10 ,−8 ,−6 , … … , 6 , 8 , 10
Input: #include<iostream.h>
int main()
{
int x;
float fx;
for(x=-10;x<=10;x=x+2)
{
fx=(x*x+1.5*x+5)/(x-3);
cout<<"For x= "<<x<<"\t f(x) is"<<fx<<"\n";
}
return 0;
}
Output:
PROGRAM 35:
Aim: Program to calculate the factorial of an integer.
Input: #include<iostream.h>
#include<stdlib.h>
int main()
{
system("cls");
int i,num,fact=1;
cout<<"Enter intger:";
cin>>num;
i=num;
while(num)
{
fact*=num;
--num;
}
cout<<"The factorial of "<<i
<<" is "<<fact<<"\n";
return 0;
}
Output:
PROGRAM 36:
Aim: Program to calculate and print the sums of even and odd integers of the first n natural
numbers.
Input: #include<iostream.h>
int main()
{
int n,sum_even=0,sum_odd=0,ctr=1;
cout<<"Upto which natural number ? ";
cin>>n;
while(ctr<=n)
{
if(ctr%2==0)
sum_even+=ctr;
else
sum_odd+=ctr;
ctr++;
}
cout<<"\n"<<"The sum of even integers is "<<sum_even;
cout<<"\n"<<"The sum of odd integers is "<<sum_odd<<endl;
return 0;
}
Output:
PROGRAM 37:
Aim: Program to display a menu regarding rectangle operations and perform according to
user’s response.
Input: #include<iostream.h>
#include<math.h>
#include<process.h>
int main()
{
char ch,ch1;
float l,b,peri,area,diag;
cout<<"Rectangle Menu";
cout<<"\n 1. Area";
cout<<"\n 2. Perimeter";
cout<<"\n 3. Diagonal";
cout<<"\n 4. Exit"<<endl;
cout<<"Enter your choice:";
do
{
cin>>ch;
if(ch=='1'||ch=='2'||ch=='3')
{
cout<<"Enter length & breadth:";
cin>>l>>b;
}
switch(ch)
{
case'1':area=l*b;
cout<<"Area="<<area;
break;
case'2':peri=2*(l+b);
cout<<"Perimete="<<peri;
break;
case'3':diag=sqrt((l*l)+(b*b));
cout<<"Diagonal="<<diag;
break;
P.T.O.
case'4':cout<<"Breaking";
exit(0);
default:cout<<"Wrong choice !!!!";
cout<<"Enter a valid one";
break;
}
cout<<"\nWant to enter more(y/n)? ";
cin>>ch1;
if(ch1=='y'||ch1=='Y')
cout<<"Again enter choice(1-4):";
}
while(ch1=='y'||ch1=='Y');
return 0;
}
Output:
PROGRAM 38:
Aim: Program to illustrate the difference between the break and continue statements.
Input: #include<iostream.h>
int main()
{
int i;
cout<<"The loop with\'break\'produces output as :\n";
for(i=1;i<=10;++i)
{
if((i%3)==0)
break;
else
cout<<i<<endl;
}
cout<<"The loop with\'continue\'produces output as:\n";
for(i=1;i<=10;++i)
{
if((i%3)==0)
continue;
else
cout<<i<<endl;
}
}
Output:
PROGRAM 39:
Aim: Program to check whether a number is prime or not.
Input: #include<iostream.h>
#include<process.h>
int main()
{
int num,i;
cout<<"\nEnter the number:";
cin>>num;
for(i=2;i<=num/2;++i)
if(num%i==0)
{
cout<<"\n Not a prime number!!!"<<endl;
exit(0);
}
cout<<"\n It is a prime number."<<endl;
return 0;
}
Output 1:
Output 2:
PROGRAM 40:
Aim: Write a program that prints 1 2 4 8 16 32 64 128.
Input: #include<iostream.h>
int main()
{
for(int i=1;;)
{
cout<<i<<" ";
if(i==128)
break;
i*=2;
}
return 0;
}
Output:
PROGRAM 40:
Aim: Write a C++ program to check whether the given number is palindrome or not.
Input: #include<iostream.h>
int main()
{
int n,num,digit,rev=0;
cout<<"\nInput the number(max.32767):";
cin>>num;
n=num;
do
{
digit=num%10;
rev=(rev*10)+digit;
num=num/10;
}
while(num !=0);
cout<<"The reverse of the number is:"<<rev<<"\n";
if(n==rev)
cout<<"The number is palindrome"<<endl;
else
cout<<"\nThe number is not a palindrome"<<endl;
return 0;
}
Output 1:
Output 2:
PROGRAM 41:
Aim: Write a C++ program to generate divisors of an integer.
Input: #include<iostream.h>
int main()
{
int i,num;
cout<<"Enter an integer :";
cin>>num;
for(i=1;i<=num/2;++i)
{
if(num%i==0)
cout<<"\n"<<i<<endl;
}
return 0;
}
Output 1:
Output 2:
PROGRAM 42:
Aim: Write a C++ program to find whether a given number is odd or even or prime. The
program should continue as the user wants.
Input: #include<iostream.h>
int main()
{
char ch;
int num,i;
do
{
cout<<"Enter a number\n";
cin>>num;
if(num%2==0)
cout<<"Even\n";
else
cout<<"Odd\n";
if(num==1)
cout<<"Prime\n";
else
{
for(i=2;i<=num/2;++i)
if(num%i==0)
{
cout<<"Not prime\n";
goto Ib;
}
cout<<"Prime\n";
}
Ib:
cout<<"\nWant to continue(y/n)?";
cin>>ch;
}
while(ch=='y'||ch=='Y');
return 0;
}
Output:
PROGRAM 43:
2 4 6 n
x x x x
Aim: Write a C++ program to compute cosine series i.e., cos ( x )=1− + − +… .
2! 4 ! 6 ! n!
Input: #include<iostream.h>
#include<math.h>
int main()
{
float x,t,sum;
int i,n=20;
cout<<"Input X:\n";
cin>>x;
x=x*3.1412/180;
t=t+1;
sum=1;
for(i=1;i<=n;++i)
{
t=t*pow((double)(-1),(double)(2*i-1))*x*x/(2*i*(2*i-1));
sum+=t;
}
cout<<"cos(x)="<<sum<<"\n";
return 0;
}
Output:
PROGRAM 44:
Aim: Write a C++ program to print table of a given number.
Input: #include<iostream.h>
int main()
{
int num;
cout<<"\nEnter number :";
cin>>num;
for(int i=1;i<11;++i)
cout<<"\n"<<num<<"*"<<i
<<"="<<num*i;
return 0;
}
Output:
PROGRAM 44:
Aim: Write a C++ program to print sum of negative, sum of positive even numbers, sum of
positive odd numbers from a list of numbers entered by the user. The list terminates when the
num entered is zero.
Input: #include<iostream.h>
int main()
{
int num,sumneg,sumeven,sumodd;
sumneg=sumeven=sumodd=0;
do
{
cout<<"\nEnter a number(0 to terminate):\n";
cin>>num;
if(num<0)
sumneg+=num;
else if(num%2==0)
sumeven+=num;
else
sumodd+=num;
}
while(num!=0);
cout<<"\nSum of negative numbers="<<sumneg;
cout<<"\nSum of positive even numbers="<<sumeven;
cout<<"\nSum of positive odd numbers="<<sumodd;
return 0;
}
Output:
PROGRAM 45:
Aim: Write a C++ program to find out whether a year (entered in 4-digits numbers
representing it) is a leap year.
Input: #include<iostream.h>
int main()
{
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<<"Leap Year\n";
else
cout<<"Not a leap year\n";
return 0;
}
Output 1:
Output 2:
PROGRAM 46:
Aim: Given three numbers A, B and C, write a program to write their values in descending
order. For example, if A=7, B=4 and C=10, your program should print out : 10, 7, 4.
Input: #include<iostream.h>
int main()
{
float a,b,c,big,big2,big3;
cout<<"Enter three numbers\n";
cin>>a>>b>>c;
big=a;
if(b>big)
big=b;
if(c>big)
big=c;
if(a==big)
{
if(b>c)
{
big2=b;
big3=c;
}
else
{
big2=c;
big3=b;
}
}
else if(b==big)
{
if(a>c)
{
big2=a;
big3=c;
}
else
P.T.O.
{
big2=c;
big3=a;
}
}
else if(c==big)
{
if(a>b)
{
big2=a;
big3=b;
}
else
{
big2=b;
big3=a;
}
}
cout<<big<<","<<big2<<","<<big3<<"\n";
return 0;
}
Output:
PROGRAM 47:
Aim: Write a C++ program to print Fibonacci series i.e., 0 1 1 2 3 5 8….
Input: #include<iostream.h>
int main()
{
unsigned long first,second,third,n;
first=0;
second=1;
cout<<"How many elements(>5)?\n";
cin>>n;
cout<<"Fibonacci series\n";
cout<<first<<""<<second<<endl;
for(int i=2;i<n;++i)
{
third=first+second;
cout<<""<<third<<endl;
first=second;
second=third;
}
return 0;
}
Output:
PROGRAM 48:
Aim: Write a C++ program to print largest even and largest odd numbers from a list of
numbers entered through keywords. The list terminates as soon as one enters 0 (zero).
Input: #include<iostream.h>
int main()
{
int num, leven=0, lodd=1;
cout<<"Enter number(0 to terminate)\n";
do
{
cin>>num;
if(num%2==0)
{
if(num>leven)
leven=num;
}
else
if(num>lodd)
lodd=num;
}
while(num!=0);
cout<<"\nLargest even number="<<leven;
cout<<"\nLargest odd number="<<lodd<<endl;
return 0;
}
Output:
PROGRAM 49:
Aim: Program to print cube of a given number using a function.
Input: #include<iostream.h>
int main()
{
float cube(float);
float x,y;
cout<<"\nEnter number whose cube is to be calculated:\n";
cin>>x;
y=cube(x);
cout<<"\nThe cube of "<<x<<" is "<<y<<"\n";
return 0;
}
float cube(float a)
{
float n;
n=a*a*a;
return (n);
}
Output:
PROGRAM 50:
Aim: Program to illustrate the call by value method of function invoking.
Input: #include<iostream.h>
int main()
{
int change(int);
int orig=10;
cout<<"\nThe original value is "<<orig<<"\n";
cout<<"\nReturn values of function change() is "<<change(orig)<<"\n";
cout<<"\nThe value after function change() is over "<<orig<<"\n";
return 0;
}
int change(int a)
{
a=20;
return a;
}
Output: