Programming Assignment
Programming Assignment
Programming Assignment
1 Enter 4-digit number through keyboard. Write a program to obtain the sum of 1st and last digits
of this number.
include<iostream>
int main()
{ int num;
cin>>num;
int dig1,dig2;
dig2=num%10;
dig1=num/1000;
int sum=dig1+dig2;
cout<<"The sum of the first and last digits of the number are "<<sum<<endl;
return 0;
}
2 Enter a year through keyboard. Write a program to determine whether the year is leap year or
not. Use logical operators && and ||.
#include<iostream>
int main()
int year;
cin>>year;
else
3 Write a program that will take three numbers from keyboard and find the maximum of these
numbers. Then check whether the maximum number is even or odd.
#include <iostream>
int main()
{
int num1,num2,num3,max;
cin>>num1>>num2>>num3;
if(num1>num2)
max=num1;
else
max=num2;
if(max>num3)
cout<<"Maximum :"<<max<<endl;
else
cout<<"Maximum :"<<num3<<endl;
max=num3;
if(max%2==0)
else
return 0;
}
4 Find out the sum of squares of first n numbers.
#include <iostream>
int main()
int n,i,sq;
cin>>n;
for(i=1;i<=n;i++)
sq+=(i*i);
return 0;
}
5 Find out the average of n numbers.
#include<iostream>
int main()
int i=0,s=0,n=0;
float average;
cin>>n;
for(i=1;i<=n;i++)
s+=i;
average=s/n;
return 0;
}
6 The mark price and discount are entered through keyboard. Sometimes seller gets profit of x % or
some time loss of y % depends on discount. Write a program to determine whether the seller has
made profit or incurred loss. Also determine how much profit he made or loss incurred. Enter the
cost price also through key board.
//calculate the prfoit or loss %
#include<iostream>
int main()
float mp,cp,d,x,y=0.0;
cin>>mp>>cp>>d;
float sp=mp*(1.0-d);
if(sp>cp)
x=(sp-cp)/100.0;
else
y=(cp-sp)/100.0;
return 0;
7 Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each
digit of the number is equal to the number itself, then the number is called an Armstrong
number. For example 153= (1*1*1) + (5*5*5) + (3*3*3).
#include<iostream>
#include<cmath>
int main()
int i,copy,digit,sum=0;
for(i=1;i<=500;i++)
{
sum=0;
copy=i;
while(copy!=0)
digit=copy%10;
sum+=pow(digit,3);
copy=copy/10;
if(sum==i)
return 0;
8 There are 9000 people in a town whose population increases by 15% each year. Write a program
that displays the annual population and determines the number of years it will take for the
population to surpass 50000.
#include<iostream>
int main()
int pop_i,pop_tar,year;
float rate=0.0;
cout<<"Enter the initial population and annual increment as a fraction of hunderd"<<endl;
cin>>pop_i>>rate;
cin>>pop_tar;
year=0;
while(pop_i<pop_tar)
pop_i=pop_i+(rate*pop_i);
year++;
return 0;
int main()
int num,copy,dig,sum;
copy=num;
while(copy!=0)
dig=copy%10;
sum+=dig;
copy/=10;
return 0;
10 Write a program to receive Cartesian coordinates (x,y) of a points and convert them into polar co-
ordinates(r,θ).
include<iostream>
#include<cmath>
float x,y,r,angle;
cin>>x>>y;
r=sqrt((x*x)+(y*y));
angle=atan(y/x);
angle=(22/7)-atan(-y/x);
angle=(22/7)+atan(-y/x);
else
angle=(-atan(y/x));
return 0;
}
11 Write a program to receive values of latitude(L1,L2) and longitude (G1,G2), in degrees, of two
places on the earth and output the distance(D) between then in nautical miles.
#include<iostream>
#include<cmath>
int main()
float lat1,long1,lat2,long2,dist;
cin>>lat1>>long1;
cin>>lat2>>long2;
lat1=con*lat1;
lat2=con*lat2;
long1=con*long1;
long2=con*long2;
float d = 6373*0.54*c;
return 0;
12 Wind chill factor is the felt air temperature on exposed skin due to wind. The wind chill
temperature is always lower than the air temperature. Write a program to calculate the wind chill
factor.
#include<iostream>
#include<cmath>
int main()
float v,t,c;
cout<<"enter the wind speed( in kmph) and temperature (in celcius) \n";
cin>>v>>t;
c=13.12+0.6215*t-11.37*pow(v,0.16)+0.3965*t*pow(v,0.16);
return 0;
13 If the value of an angle is input through the keyboard, write a program to print all its
Trigonometric ratios.
#include<iostream>
#include<cmath>
int main()
float a,r;
cin>>a;
r=a*3.14159265/180;
cout<<"sin"<<a<<"= "<<sin(r)<<"\n";
cout<<"cos"<<a<<"= "<<cos(r)<<"\n";
cout<<"tan"<<a<<"= "<<tan(r)<<"\n";
cout<<"cot"<<a<<"= "<<1/tan(r)<<"\n";
cout<<"sec"<<a<<"= "<<1/cos(r)<<"\n";
cout<<"cosec"<<a<<"= "<<1/sin(r)<<"\n";
return 0;
}
14 Find the gcd and lcm of given two numbers
#include<iostream>
int main()
int num1,num2,i,hcf,lcm,min;
cin>>num1>>num2;
if(num1<=num2)
min=num1;
else
min=num2;
for(i=1;i<=min;i++)
if(num1%i==0&&num2%i==0)
hcf=i;
lcm=(num1*num2)/hcf;
cout<<"The hcf is "<<hcf<<" and the lcm is "<<lcm<<endl;
return 0;
#include<iostream>
#include<cmath>
int main()
float x=0.0,fact=1.0,power=0.0;
int i=0,n=0;
float sum=0.0;
cin>>x>>n;
for(i=1;i<=n;i++)
power=pow(x,i);
for(int j=1;j<=i;j++)
fact=fact*j;
sum=power/fact;
16 Write a program to accept a number and find sum of its individual digits repeatedly till the result
is a single digit. For example, if the given number is 4687 the output should be 7.
#include<iostream>
int main()
int num,copy,dig,sum=10;
cin>>num;
copy=num;
while(sum>=10)
{ sum=0;
while(copy!=0)
dig=copy%10;
sum+=dig;
copy/=10;
copy=sum;
return 0;
int main()
char a='A';
int i,j,k,n;
cin>>n;
for (i=1;i<=n;i++)
cout<<a;
a++;
a=a-1;
for (i=n-1;i>0;i--)
a--;
cout<<a;
cout<<endl;
int c=1;
while (n!=0)
for (i=1;i<=n-1;i++)
{
cout<<a;
a++;
for (j=1;j<=2*c-1;j++)
cout<<" ";
c++;
for(k=1;k<=n-1;k++)
a--;
cout<<a;
cout<<endl;
n--;
return 0;
#include <iostream>
int factorial(int);
int main()
int n,i=0,j=0,k=0,l=0,fact=0,space;
for(i=0;i<n;i++)
for(j=n;j>=i;j--)
cout<<" ";
for(k=0;k<=i;k++)
cout<<'1';
else
fact=factorial(i)/(factorial(i-k)*factorial(k));
cout<<fact<<" ";
cout<<" "<<endl;
for(i=n;i>=0;i--)
for(j=n;j>=i;j--)
cout<<" ";
for(k=0;k<=i;k++)
cout<<'1';
else
fact=factorial(i)/(factorial(i-k)*factorial(k));
cout<<fact<<" ";
cout<<" "<<endl;
for(i=1;i<=n;i++)
for(j=n;j>=i;j--)
cout<<" ";
for(k=1;k<=(2*i-1);k++)
cout<<"*"<<" ";
cout<<endl;
for(i=n-1;i>=1;i--)
for(j=n;j>=i;j--)
cout<<" ";
for(k=1;k<=(2*i-1);k++)
cout<<"*"<<" ";
cout<<endl;
return 0;
}
int factorial(int n)
int fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
return fact;
18 An equation of the form ax^2+bx+c=0 is known as quadratic equation. The values of x that satisfy
the equation are known as the roots of the equation. Write a program to find out the roots of the
quadratic equation
#include<iostream>
#include<cmath>
int main()
float a,b,c,x1,x2,d;
cin>>a>>b>>c;
if(a==0)
cout<<"Invalid Input";
else
d=b*b-4*a*c;
if(d<0)
cout<<"Imaginary roots"<<endl;
else
d=sqrt(d);
x1=(-b+d)/(2*a);
x2=(-b-d)/(2*a);
cout<<x1<<","<<x2<<endl;
}
return 0;
19 Write a program to find out the sum of the following series (up-to 30th term):
……………….
#include<iostream>
#include<cmath>
int main()
double x,sum;
int i=0,count=1;
cin>>x;
for(i=1;i<=30;i++)
if(i%2!=0)
sum=sum+(pow(x,count)/fact(count));
else
sum=sum-(pow(x,count)/fact(count));
count=count+2;
cout<<sum<<endl;
int fact(int n)
int fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
return fact;
1-Factorial of a number
2-Prime or not
3-Odd or even
4. Nth Fibonacci number
5-Exit
Once a menu item is selected the appropriate option should be taken and once this option is
finished, the menu should reappear .Unless the user selects the Exit option the program should
continue work.
#include<iostream>
#include<cmath>
int main()
int choice,num,i,fact,count=0,flag=1,a=0,b=1,c=0;
while(flag==1)
cin>>choice;
switch(choice)
cin>>num;
for(i=1;i<=num;i++)
fact=fact*i;
break;
cin>>num;
for(i=1;i<=num;i++)
if(num%i==0)
count++;
}
if(count==2)
else
break;
cin>>num;
if(num%2==0)
else
break;
case 4:a=0,b=1,c=0;
cin>>num;
if(num==1)
cout<<'0'<<endl;
else if(num==2)
cout<<'1'<<endl;
else
for(i=1;i<=num-2;i++)
c=a+b;
a=b;
b=c;
cout<<c<<endl;
break;
case 5:flag=0;
}
21 A user enters integers until end of input and wants to find the largest number in the list of
integers entered and the number of times it was entered. For example, if the input is 5, 2, 15, 3,
7, 15, 8, 9, 5, 2, 15, 3, and 7, the largest is 15 and is repeated 3 times. Write an algorithm to
compute frequency of the largest of the integers entered without storing them. Convert the
algorithm to a function that displays the integers read and returns the largest number and its
frequency.
#include<iostream>
int main()
int flag=1,num,max=1,count=0;
do{
cout<<"Enter a number"<<endl;
cin>>num;
if(num>max)
max=num;
count=1;
else if(num==max)
{
count++;
cin>>flag;
}while(flag==1);
return 0;
22 Write a program that reads an integer -n (decimal number system) and convert this decimal
number to Binary, Octal, and Hexadecimal form.
#include<iostream>
int main()
int num,i,b,copy,digit,count=0;
char arr[10];
cin>>num;
copy=num;
char bin[2]={'0','1'};
char oct[8]={'0','1','2','3','4','5','6','7'};
char hex[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(copy!=0)
digit=copy%2;
arr[count]=bin[digit];
count++;
copy/=2;
cout<<"Binary: ";
for(i=count-1;i>=0;i--)
cout<<arr[i];
count=0;
copy=num;
cout<<endl;
while(copy!=0)
digit=copy%8;
arr[count]=oct[digit];
count++;
copy/=8;
cout<<"Octal: ";
for(i=count-1;i>=0;i--)
cout<<arr[i];
count=0;
copy=num;
cout<<endl;
for(i=0;i<10;i++)
arr[i]=' ';
while(copy!=0)
digit=copy%16;
//cout<<digit<<endl;
arr[count]=hex[digit];
//cout<<arr[count]<<endl;
count++;
copy/=16;
cout<<"Hexadecimal: ";
for(i=count-1;i>=0;i--)
cout<<arr[i];
count=0;
cout<<endl;
return 1;
23 Given 3-angles. write a program to check whether they form a triangle or not (A+B+C =180). If yes
check whether triangle is scalene, equilateral, isosceles or right angled triangle.
#include<iostream>
int main()
int A,B,C,i;
cin>>A>>B>>C;
if(A+B+C!=180)
else
{
if(A==B && B==C && C==A)
else
if(A==90||B==90||C==90)
return 0;
24 The Fibonacci numbers Fn are defined as follows. F0 is 1, F1 is 1 and Fi+2=Fi+Fi+1 i=0,1,….n Write a
program to find the Fibonacci value of the given number.
#include<iostream>
int main()
int n,a=1,b=1,c;
cout<<"Enter the value of number"<<endl;
cin>>n;
if(n==0||n==1)
cout<<'1'<<endl;
else
for(int i=0;i<n-2;i++)
c=a+b;
a=b;
b=c;
cout<<c<<endl;
return 0;
25 Write a program to print all the ASCII values and their equivalent characters using a while loop.
#include<iostream>
int main()
int i=0;
while(i<256)
cout<<i<<" - "<<char(i)<<”\t”;
i++;
return 0;
26 A way to calculate the value of π is based on the use of a series defines as follows(N- number of
terms). Write a program to find π value (up to n terms and display the result by correcting it to
three decimal places):
#include<iostream>
#include<cmath>
#include<iomanip>
int main()
float n,sum_pi=0.0;
cin>>n;
for(int i=0;i<n;i++)
sum_pi=sum_pi+(pow((-1),i)/((2*i)+1));
sum_pi=4.0*sum_pi;
cout<<setprecision(4)<<sum_pi<<endl;
return 0;
27 Write a program that accepts a year written as a four-digit numeral and outputs the year written
in Roman numerals. Important Roman numerals are V –5 , X-10 , L-50 , C-100, D-500 and M-1,000.
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"enter a 4 digit number ";cin>>n;
int x=n,p,h=1000;
while(x>1000)
{
p=x/1000;
for(int i=0;i<p;i++)
cout<<"M";
x=x%1000;
}
while(x>500)
{
p=x/500;
for(int i=0;i<p;i++)
cout<<"D";
x=x%500;
}
while(x>100)
{
p=x/100;
for(int i=0;i<p;i++)
cout<<"C";
x=x%100;
}
while(x>50)
{
p=x/50;
for(int i=0;i<p;i++)
cout<<"L";
x=x%50;
}
while(x>10)
{
p=x/10;
for(int i=0;i<p;i++)
cout<<"X";
x=x%10;
}
while(x>5)
{
p=x/5;
for(int i=0;i<p;i++)
cout<<"V";
x=x%5;
}
if(x>=3)
{
if(x==3)
cout<<"IIV";
if(x==4)
cout<<"IV";
}
else
{
while(x>0)
{
cout<<"I";
x--;
}
}
return 0;
}
int power(int,int);
int main()
int num,pow;
cin>>num>>pow;
int result=power(num,pow);
int res=1;
for(int i=1;i<=b;i++)
res=res*a;
return res;
29 A positive integer is entered through the keyboard, write a function to find the binary equivalent
of this number:
(i) without using recursion
(ii) using recursion
#include<iostream>
using namespace std;
int i=0;
int recursion(int x,int y[100])
{
for(int s=v-1;s>=0;s--)
cout<<m[s];
return 0;
}
if(n==1)
{
return;
TOH(n-1,Sour,Des,Aux);
TOH(n-1,Aux,Sour,Des);
int main()
int n;
cin>>n;
TOH(n,'A','B','C');
return 0;