Programming Assignment

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

NATIONAL INSTITUTE OF TECHNOLOGY

WARANGAL – 506 004


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
I B.Tech., II Semester
PSCP Lab Assignment-1, January 2019

1 Enter 4-digit number through keyboard. Write a program to obtain the sum of 1st and last digits
of this number.
include<iostream>

using namespace std;

int main()

{ int num;

cout<<"Enter the four digit number"<<endl;

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>

using namespace std;

int main()

int year;

cout<<"Enter the year"<<endl;

cin>>year;

if((year%4==0 && year%100!=0) || year%400==0)

cout<<"The year is a leap year"<<endl;

else

cout<<"The year is not a leap year"<<endl;

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>

using namespace std;

int main()
{

int num1,num2,num3,max;

cout<<"Enter the three numbers to be compared"<<endl;

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)

cout<<"The maximum number is even "<<endl;

else

cout<<"The maximum number is odd"<<endl;

return 0;

}
4 Find out the sum of squares of first n numbers.
#include <iostream>

using namespace std;

int main()

int n,i,sq;

cout<<"Enter the limit: "<<endl;

cin>>n;

for(i=1;i<=n;i++)

sq+=(i*i);

cout<<"The sum of the squares is "<<sq<<endl;

return 0;

}
5 Find out the average of n numbers.
#include<iostream>

using namespace std;

int main()

int i=0,s=0,n=0;

float average;

cout<<"Enter the value upto which average is to be found"<<endl;

cin>>n;

for(i=1;i<=n;i++)

s+=i;

average=s/n;

cout<<"The sum is: "<<s<<endl;

cout<<"The average is: "<<average<<endl;

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>

using namespace std;

int main()

float mp,cp,d,x,y=0.0;

cout<<"Enter the marked price,cost price and discount"<<endl;

cin>>mp>>cp>>d;

float sp=mp*(1.0-d);

if(sp>cp)

x=(sp-cp)/100.0;

cout<<"The profit percentage is "<<x;


}

else

y=(cp-sp)/100.0;

cout<<"The loss percentage is "<<y;

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>

using namespace std;

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)

cout<<"The number "<< i <<" is an armstrong number"<<endl;

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>

using namespace std;

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;

cout<<"Enter the target population "<<endl;

cin>>pop_tar;

year=0;

while(pop_i<pop_tar)

pop_i=pop_i+(rate*pop_i);

year++;

cout<<"The required time is "<<year<<endl;

return 0;

9 Write a program to print the sum of digits of any positive number


#include<iostream>

using namespace std;

int main()

int num,copy,dig,sum;

cout<<"Enter the number "<<endl;


cin>>num;

copy=num;

while(copy!=0)

dig=copy%10;

sum+=dig;

copy/=10;

cout<<"The sum of digits is "<<sum<<endl;

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>

using namespace std;


int main()

float x,y,r,angle;

cout<<"Enter the x and y coordinates"<<endl;

cin>>x>>y;

r=sqrt((x*x)+(y*y));

if(x>=0 && y>=0)

angle=atan(y/x);

else if(x<0 && y>0)

angle=(22/7)-atan(-y/x);

else if(x<0 && y<0)

angle=(22/7)+atan(-y/x);

else

angle=(-atan(y/x));

cout<<"The value of r is "<<r<<" and the value of the angle is "<<angle<<endl;

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>

using namespace std;

int main()

float lat1,long1,lat2,long2,dist;

cout<<" Enter the coordinates for the first place "<<endl;

cin>>lat1>>long1;

cout<<" Enter the coordinates for the second place "<<endl;

cin>>lat2>>long2;

float con =(M_PI/180);

lat1=con*lat1;

lat2=con*lat2;

long1=con*long1;

long2=con*long2;

float dlon = long2 - long1;


float dlat = lat2 - lat1;

float a = pow(sin(dlat/2),2) + cos(lat1) * cos(lat2) * pow(sin(dlon/2),2);

float c = 2 * atan2( sqrt(a), sqrt(1-a) );

float d = 6373*0.54*c;

cout<<"The distance in nautical miles is "<<d<<endl;

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>

using namespace std;

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);

cout<<"wind chill factor is = "<<c<<"\n";

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>

using namespace std;

int main()

float a,r;

cout<<"enter the angle \n";

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>

using namespace std;

int main()

int num1,num2,i,hcf,lcm,min;

cout<<"Enter the two numbers"<<endl;

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;

15 Write a program to find the sum of first n terms of series:

1 + X + (X*X) + (X*X*X) + (X*X*X*X) +…………..


2! 3! 4!

#include<iostream>

#include<cmath>

using namespace std;

int main()

float x=0.0,fact=1.0,power=0.0;

int i=0,n=0;

float sum=0.0;

cout<<"Enter the value of x and n"<<endl;

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;

cout<<"The sum is "<<sum<<endl;

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>

using namespace std;

int main()

int num,copy,dig,sum=10;

cout<<"Enter the number "<<endl;

cin>>num;

copy=num;

while(sum>=10)

{ sum=0;
while(copy!=0)

dig=copy%10;

sum+=dig;

copy/=10;

copy=sum;

cout<<"The sum of digits is "<<sum<<endl;

return 0;

17 Write a program to get following output


ABCDEFGFEDCBA 1
ABCDEF FEDCBA 1 1 *
ABCDE EDCBA 1 2 1 * * *
ABCD DCBA 1 3 3 1 * * * * *
1 4 6 4 1 * * * * * * *
ABC CBA
1 3 3 1 * * * * *
AB BA 1 2 1 * * *
A A 1 1 *
1
#include<iostream>

using namespace std;

int main()

char a='A';

int i,j,k,n;

cout<<"Enter a number "<<endl;

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>

using namespace std;

int factorial(int);

int main()

int n,i=0,j=0,k=0,l=0,fact=0,space;

cout<<"Enter the limit "<<endl;


cin>>n;

for(i=0;i<n;i++)

for(j=n;j>=i;j--)

cout<<" ";

for(k=0;k<=i;k++)

if(k==0 && i==0)

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++)

if(k==0 && i==0)

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>

using namespace std;

int main()

float a,b,c,x1,x2,d;

cout<<"Enter the value of a,b and c"<<endl;

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>

using namespace std;

int fact(int n);

int main()

double x,sum;

int i=0,count=1;

cout<<"Enter the value of x"<<endl;

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;

cout<<"The sum is "<<sum;

int fact(int n)

int fact=1;

for(int i=1;i<=n;i++)

fact=fact*i;

return fact;

20 Write a menu driven program which has following options

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>

using namespace std;

int main()

int choice,num,i,fact,count=0,flag=1,a=0,b=1,c=0;

while(flag==1)

cout<<"Enter your choice\n 1.Factorial of a number\n 2.Prime or not\n 3.Even or


Odd\n 4.Nth Fibonacci number\n 5.Exit"<<endl;

cin>>choice;

switch(choice)

case 1: cout<<"Enter the number"<<endl;

cin>>num;

for(i=1;i<=num;i++)

fact=fact*i;

cout<<"The factorial "<<fact<<endl;

break;

case 2:cout<<"Enter the number"<<endl;

cin>>num;

for(i=1;i<=num;i++)

if(num%i==0)

count++;
}

if(count==2)

cout<<"The number is prime"<<endl;

else

cout<<"The number is not prime"<<endl;

break;

case 3:cout<<"Enter the number"<<endl;

cin>>num;

if(num%2==0)

cout<<"The number is even"<<endl;

else

cout<<"The number is odd"<<endl;

break;

case 4:a=0,b=1,c=0;

cout<<"Enter the value of n"<<endl;

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>

using namespace std;

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++;

cout<<"Enter 1 to continue, 0 to end"<<endl;

cin>>flag;

}while(flag==1);

cout<<"The maximum number is "<<max<<" and the frequency is "<<count<<endl;;

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>

using namespace std;

int main()

int num,i,b,copy,digit,count=0;
char arr[10];

cout<<"Enter the decimal number"<<endl;

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>

using namespace std;

int main()

int A,B,C,i;

cout<<"Enter the value of A,B,C respectively "<<endl;

cin>>A>>B>>C;

if(A+B+C!=180)

cout<<"The input is not valid"<<endl;

else

{
if(A==B && B==C && C==A)

cout<<"The triangle is equilateral"<<endl;

else if(A==B || B==C || C==A)

cout<<"The triangle is isosceles"<<endl;

else

cout<<"The triangle is scalene"<<endl;

if(A==90||B==90||C==90)

cout<<"The triangle is right angled"<<endl;

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>

using namespace std;

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>

using namespace std;

int main()

int i=0;

cout<<"The ASCII characters are as follows"<<endl;

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>

using namespace std;

int main()

float n,sum_pi=0.0;

cout<<"Enter the value of n"<<endl;

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;
}

28 Write a function power(a,b) , to calculate the value of a raised to b.


#include<iostream>

using namespace std;

int power(int,int);

int main()

int num,pow;

cout<<"Enter the number and the power to which it is to be raised"<<endl;

cin>>num>>pow;

int result=power(num,pow);

cout<<"The result is: "<<result<<endl;


return 0;

int power(int a, int b)

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])
{

//static int i=0;


if(x==0)
return i;
else
{
int d=x%2;
y[i++]=d;
x=x/2;
recursion(x,y);
}
}
int main()
{
int n,m[100];
cout<<"enter a number ";cin>>n;
cout<<"binary equivalent is "<<endl;
int v=recursion(n,m);

for(int s=v-1;s>=0;s--)
cout<<m[s];

return 0;
}

30 Write a program for towers of Hanoi, using recursive function


#include<iostream>

using namespace std;

void TOH(int n,char Sour, char Aux,char Des)

if(n==1)
{

cout<<"Move Disk "<<n<<" from "<<Sour<<" to "<<Des<<endl;

return;

TOH(n-1,Sour,Des,Aux);

cout<<"Move Disk "<<n<<" from "<<Sour<<" to "<<Des<<endl;

TOH(n-1,Aux,Sour,Des);

int main()

int n;

cout<<"Enter no. of disks:";

cin>>n;

//calling the TOH

TOH(n,'A','B','C');

return 0;

You might also like