Sunteți pe pagina 1din 26

NATIONAL INSTITUTE OF TECHNOLOGY

WARANGAL – 506 004


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING I B.Tech.,I Semester
PSCP Lab Assignment

Roll No. :841923 Name:Duda Ram Beniwal


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 n,r1,r2,sum=0;
cout<<"enter the 4 digit number:"<<endl;
cin>>n;
r1=n%10;
while(n>0)
{r2=n%10;
n=n/10;}

sum=sum+r1+r2;
cout<<"the sum of first and last digits is:"<<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()
{
intyr;
cout<<"Enter a year\n";
cin>>yr;
if((yr%4==0 && yr%100!=0) || yr%400==0)
{
cout<<"Entered year is leap year";
}
else
{
cout<<"Entered year is not a leap year";
}
return 0;
}
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()
{intar[3],large,i;
cout<<"enter the numbers:"<<endl;
for(i=0;i<3;i++)
{
cin>>ar[i];
}
large=ar[0];
for(i=0;i<3;i++)
{
if(ar[i]>large)
{
large=ar[i];
}
}
cout<<"the maximum of numbers is:"<<large<<endl;
if(large%2==0)
{ cout<<"the maximum is even"<<endl;}
else
{cout<<"the maximum is odd"<<endl;}
return 0;
}

4 Find out the sum of squares of first n numbers.


#include <iostream>
using namespace std;
int main()
{intn,i,sum=0,s;
cout<<"enter the limit:"<<endl;
cin>>n;
for(i=1;i<=n;i++)
{ s=i*i;
sum=sum+s;}
cout<<"the sum of squares of first"<<n<<"numbers is:"<<sum<<endl;
return 0;
}
5 Find out the average of n numbers.
#include<iostream>
using namespace std;
int main()
{
inta,n,i;
float Average, sum=0;
cout<<"\nEnter the number of numbers=";
cin>>n;
cout<<"\nEnter the numbers=";
for(i=0;i<n;i++)
{
a=0;
cin>>a;
sum=sum+a;
}
Average=sum/n;
cout<<"\nAverage of the number is="<<Average;
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.

#include<iostream>
using namespace std;
int main()
{float mp,cp,pr,lo,d,sp;
cout<<"enter the mark price:";
cin>>mp;
cout<<"enter the cost price:";
cin>>cp;
cout<<"enterthe discount:";
cin>>d;
sp=mp-d;
if(sp>cp)
{cout<<"profit is made"<<endl;
pr=sp-cp;
cout<<"profit is:"<<pr<<endl;
}
else
{cout<<"loss is incurred"<<endl;
lo=cp-sp;
cout<<"loss is:"<<lo<<endl;
}
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>
using namespace std;
int main()
{
intnumber,r,i,sum=0,a=0;
cout<<"enter the number"<<endl;
cin>>number;
cout<<"the armstrong numbers between 1 and "<<number<<" are:"<<endl;
for(i=1;i<=number;i++)
{
a=i;
sum=0;
while(a>0)
{
r=a%10;
sum=sum + r*r*r;
a=a/10;
}
if(sum==i)
{
cout<<i<<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>
#include <math.h>
using namespace std;
int main()
{
int y=9000,i,sum=0;
cout<<"The present population of town is 9000.\n";
for(i=0;i<20;i++)
{
sum=sum+1;
y=y*115/100;
cout<<sum<<":Population next year is ="<<y<<"\n";
if(y>50000) break;
} cout<<"\nThe numbers of years it take is="<<sum;
return 0;
}

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


#include <iostream>
using namespace std;
int main()
{intn,r,sum=0;
cout<<"enter the number:"<<endl;
cin>>n;
while(n>0)
{ r=n%10;
sum=sum+r;
n=n/10;}
cout<<"the sum of digits:"<<sum<<endl;
return 0;
}

1 Write a program to receive Cartesian coordinates (x,y) of a points and convert them into polar coordinates(r,θ).
#include<iostream>
0 #include<cmath>
using namespace std;
int main()
{
intx,y,a,b;
float r,teta;
cout<<"enter the x co-ordinate:"<<endl;
cin>>x;
cout<<"enter the y co-ordinate:"<<endl;
cin>>y;
a=x*x;
b=y*y;
r=sqrt(a+b);
teta=(atan(y*1.0/x))*180/3.14;
cout<<"polar co-ordinates are:"<<r<<"and"<<teta<<endl;
return 0;
}

1 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.
1

#include <iostream>
#include<cmath>
long double dectorad(long double x)
{
x=x*3.142/180;
return x;
}

using namespace std;

int main()
{
long double l1,l2,g1,g2;
cout<<"enter the co-ordinates of two places:\n";
cout<<"longitude 1:";
cin>>l1;
cout<<"longitude 2:";
cin>>l2;
cout<<"latitude 1:";
cin>>g1;
cout<<"latitude g2:";
cin>>g2;
long double a,c,d,del_lat,del_long;
del_lat=dectorad(l2)-dectorad(l1);
del_long= dectorad(g2)- dectorad(g1);
a= pow(sin(del_lat/2),2)+cos(dectorad(l1))*cos(dectorad(l2))*pow(sin(del_long/2),2);
c=2*asin(sqrt(a));
d=6371*c/1.852;
cout<<"the distance between two places is:"<<d<<" nautical miles"<<endl;

return 0;
}

#include <iostream>
1 #include <cmath>
2
using namespace std;

int main()
{
double t,w,ans;
cout<<"enter the temperature(in farenheit):";
cin>>t;
cout<<"enter the wind speed(mph):";
cin>>w;
ans=35.74 +(0.625*t)-(35.75*pow(w,0.16))+(0.4275*t*pow(w,0.16));
cout<<"wind chill factor is:"<<ans<<endl ;
return 0;
}

1 If the value of an angle is input through the keyboard, write a program to print all its Trigonometric ratios.

3 #include<iostream>
#include<cmath>
using namespace std;
int main()
{
float angle,a;
cout<<"enter the angle in degrees:"<<endl;
cin>>angle;
a=angle*3.14*1.0/180;
sin(a);
cos(a);
tan(a);
cout<<"the trignometric ratios are:"<<endl;
cout<<"sine:"<<sin(a)<<endl;
cout<<"cos:"<<cos(a)<<endl;
cout<<"tan:"<<tan(a)<<endl;
return 0;
}
}

1 Find the gcd and lcm of given two numbers


#include <iostream>
4 using namespace std;
int main()
{
inta,b,x,i=2,y,fact=1,k,m,hcf;
cout<<"\nEnter the numbers=";
cin>>a>>b;
cout<<"\nLCM of the numbers is=";
k=a; m=b;
while(i<=a)
{
x=a%i; y=b%i;
if(x==0 && y==0)
{
a=a/i; b=b/i;
fact=fact*i;
i=1;
}
if(i==a)
{
fact=fact*a*b;
break;
}
i++;
}
cout<<fact;
hcf=k*m/fact;
cout<<"\nHCF of the numbers is="<<hcf;
return 0;
}
1 Write a program to find the sum of first n terms of series:
5 1 + X + (X*X) + (X*X*X) + (X*X*X*X) +…………..
2! 3! 4!

#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float i,j,n,fact,sum=1,x,k;

cout<<”\nEnter the number of numbers=”;


cin>>n;
cout<<”\nEnter the value of x=”;
cin>>x;
cout<<’\The required series is=”;
for(i=1;i<=n-1;i++)
{
fact=1;
for(j=1;j<=i;j++)
{
fact=fact*j;
}
k=pow(x,i)/fact;
sum=sum+k;
}
cout<<sum;
}

ij

1 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.
6
#include<iostream>
using namespace std;
int main()
{
intn,r,i,sum=0;
cout<<"enter the number"<<endl;
cin>>n;
while(10)
{ sum=0;
while(n>0)
{ r=n%10;
sum=sum+r;
n=n/10;}
if(sum<=9)
break;
n=sum;
}
cout<<"repeated sum is:"<<sum<<endl;
return 0;
}

1
7

#include<iostream>
a using namespace std;
int main()
{ inta,b;
cout<<"ABCDEFGFEDCBA"<<endl;
for(int i=1;i<7;i++)
{
for(int j=0;j<13;j++)
{
a=0;
b=0;
if(j<=6-i)
{
a=65+j;
cout<<(char)a;
}
else if(j>=6+i)
{
b=77-j;
cout<<(char)b;
}
else
{
cout<<" ";
}
}
cout<<endl;
}
return 0;
}
#include<iostream>
b using namespace std;
intmain()
{
intn,row,den,num;
cout<<"enter the no of rows:";
cin>>n;
row=(n+1)/2;
for( int i=0;i<row;i++)
{
for( int j=1;j<=row-1-i;j++)
{
cout<<" ";
}
int start=1;
num=i;
den=1;
for( int k=0;k<=i;k++)
{ cout<<start<<" ";
start=start*num/den;
num--;
den++;
}
cout<<endl;
}
for( int i=row-2;i>=0;i--)
{
for(int j=1;j<=row-1-i;j++)
{
cout<<" ";
}
int start=1;
num=i;
den=1;
for(int k=0;k<=i;k++)
{
cout<<start<<" ";
start=start*num/den;
num--;
den++;
}
cout<<endl;
}
return 0;
}

#include<iostream>
c using namespace std;
int main()
{
introw,a;
cout<<"enter the number of rows(odd) :";
cin>>row;
a=(row+1)/2;
for(int i=1;i<=a;i++)
{
for(int k=1;k<=a-i;k++)
{ cout<<" ";
}
for(int j=1;j<2*i;j++)
{ cout<<"*";
}
cout<<endl;
}
for(int m=a-1;m>0;m--)
{
for(int n=m;n<=a-1;n++ )
{
cout<<" ";
}
for(int l=1;l<2*m;l++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
1 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
8
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double x1,x2;
inta,b,c,d;
cout<<"enter 'a' value:"<<endl;
cin>>a;
cout<<"enter 'b' value:"<<endl;
cin>>b;
cout<<"enter 'c' value:"<<endl;
cin>>c;
d=(b*b) - 4*a*c;
if( d>=0)
{
x1=(-b+ sqrt(d))*1.0/2*a;
x2=(-b- sqrt(d))*1.0/2*a;
cout<<"the roots are:"<<endl<<x1<<endl<<x2<<endl;}
if (d<0)
{ cout<<"the roots are imaginary"<<endl;
}
return 0;}

th
1 Write a program to find out the sum of the following series (up-to 30 term):
……………….
9
#include<iostream>
using namespace std;
int main()
{
float sum=0.00;
intx,a,n,sign=-1,p=1,fact=1;
cout<<"enter the value of x :";
cin>>x;
cout<<"enter no of terms :";
cin>>n;
a=2*n;
for(int i=1;i<a;i+=2)
{
for(int j=1;j<=i;j++)
{ fact=fact*j;
p=p*x;
}
sign=-1*sign;
sum+=sign*p*1.0/fact;
}
cout<<"thr sum is :"<<sum<<endl;
return 0;
}

2 Write a menu driven program which has following options


0 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 <math.h>
using namespace std;
int main()
{ int choice, n, f3, fac=1;
int flag=0;
int f1=0;
int f2=1;
do
{ cout<<"The menu:\n"<<"1. Factorial\n2. Prime or not\n3. Even or Odd\n4. nth Fibonacci Series\n5. Exit\n";
cout<<"Select the option you want(1/2/3/4/5):\n";
cin>>choice;
switch(choice)
{
case 1 :
cout<<"Enter the number:\n";
cin>>n;
for(int i=1; i<=n; i++)
fac*=i;
cout<<"The factorial is "<<fac<<"\n\n";
break;
case 2 :
cout<<"Enter the number:\n";
cin>>n;
for(int i=2;i<n; i++)
{if(n%i==0)

flag=1;}
if(flag==1)
cout<<"The no. is not a prime number.\n\n";
else
cout<<"The no. is a prime number.\n\n";
break;
case 3 :
cout<<"Enter the number:\n";
cin>>n;
if(n%2==0)
cout<<"The number is even.\n\n";
else
cout<<"The number is odd.\n\n";
break;
case 4 :
cout<<"Enter the value of n(>2):\n";
cin>>n;
for(int i=3; i<=n;i++)
{f3=f1+f2;
f1=f2;
f2=f3;}
cout<<"The nth Fibonacci series is "<<f3<<"\n\n";
break;
case 5 :
cout<<"You chose to exit the program.\n\n";
break;
default :
cout<<"Wrong number entered. Please enter a number from 1 to 5.\n\n";

}
}
while(choice!=5);
cout<<"You have exited the program.\n";
return 0;
}
2 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
1 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;


void func()
{
intn,maximum=1,freq=0;
cout<<"enter the number (use 0 to stop) :";
cin>>n;
while(n!=0)
{
if(n>=maximum)
{ if(n>maximum)
{
maximum=n;
freq=0;
}
if(n==maximum)
freq++;
}

cin>>n;
}
cout<<" the largest number is: "<<maximum<<endl;
cout<<" its frequency is: "<<freq<<endl;

return ;
}
int main()
{
func();
return 0;
}

2 Write a program that reads an integer -n (decimal number system) and convert this decimal number to Binary, Octal, and
Hexadecimal form.
2
#include<iostream>
using namespace std;
int main()
{
intn,a;
cout<<"enter the decimal number number:";
cin>>n;
a=n;
int r1=0,sum1=0,i=1;
while(n>0)
{ r1=n%2;
n=n/2;
sum1+=r1*i;
i*=10;
}
n=a;
int r2=0,sum2=0,j=1;
if (n>8)
{
while(n>0)
{
r2=n%8;
n=n/8;
sum2+=r2*j;
j*=10;
}
}
else
{ sum2=n;}
n=a;
int r3=0,sum3=0,k=1;
if(n>16)
{
while(n>0)
{
r3=n%16;
n=n/16;
sum3+=r3*k;
k*=10;
}
}
else
{ sum3=n;}
cout<<" octal: "<<sum2<<endl;
cout<<" hexadecimal: "<<sum3<<endl;
cout<<" binary: "<<sum1<<endl;
return 0;
}

2 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 scalen, equilateral, isoceless or right angled triangle.
3
#include<iostream>
using namespace std;
int main()
{
inta,b,c;
cout<<"enter the three angles"<<endl;
cin>>a>>b>>c;
if(a+b+c==180)
{
cout<<"the given angles forms a triangle"<<endl;
if(a==60 && b==60 && c==60)
{
cout<<"its an equilateral triangle"<<endl;
}
if(a==90||b==90||c==90)
{
cout<<"its a rght angled triangle"<<endl;
}
if(a==b || b==c || c==a)
{cout<<"its an isoceles triangle"<<endl;
}
else
{ cout<<"its a scalene triangle"<<endl;
}
}
else
{
cout<<"the given angles do not form a triangle"<<endl;
}

return 0;
}
2 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.
4
#include<iostream>
using namespace std;
int main()
{
intn,a=0,b=1,c;
cout<<"enter the number"<<endl;
cin>>n;
for(int i=0;i<=n;i++)
{ c=a+b;
a=b;
b=c;
}
cout<<"the fibonacci value is :"<<a<<endl;
return 0;
}

2 Write a program to print all the ASCII values and their equivalent characters using a while loop.

5 #include<iostream>
using namespace std;
int main()
{
int n=0;
while(n<129)
{cout<<"character equivalent to ASCII value "<<n<<" is:"<<(char)n<<endl;
n++;
}
return 0;
}
2 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
6 to find π value (up to n terms and display the result by correcting it to three decimal places):
#include<iostream>
using namespace std;
int main()
{intn,sign=-1,a;
float res=0;
cout<<"enter the no of terms :";
cin>>n;
for(int i=0;i<=n;i++)
{
sign=-1*sign;
res+=sign*1.0/(2*i +1);
}
res=res*4;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(3);
cout<<"value of pi is :"<<res<<endl;
return 0;
}

Write a program that accepts a year written as a four-digit numeral and outputs the year written in Roman numerals.
2 Important Roman numerals are V –5 , X-10 , L-50 , C-100, D-500 and M-1,000.

7 #include<iostream>
using namespace std; int main() {
intn,a[4],i; cout<<"enter a four digit year"<<endl;
cin>>n; for (i=0;i<4;i++){
a[i]=n%10;
n=n/10; } for (i=1;i<=a[3];i++)
cout<<"M"; if (a[2]<5){
for (i=1;i<=a[2];i++)
cout<<"C"; }else if (a[2]==5)
cout<<"D"; else if (a[2]>5){
cout<<"D"; for (i=1;i<=(a[2]-5);i++) cout<<"C";} if (a[1]<5){ for (i=1;i<=a[1];i++) cout<<"X"; }else if (a[1]==5)
cout<<"L"; else if (a[1]>5){
cout<<"L"; for (i=1;i<=(a[1]-5);i++)
cout<<"X"; } if (a[0]<4){
for (i=1;i<=a[0];i++)
cout<<"I"; } else if (a[0]==4)
cout<<"IV"; else if (a[0]==5) cout<<"V"; else if (a[0]>5&&a[0]<9){ cout<<"V"; for
(i=1;i<=(a[0]-5);i++) cout<<"I"; }else if (a[0]==9) cout<<"IX";
return 0;
}
2 Write a function power(a,b) , to calculate the value of a raised to b.

#include<iostream>
using namespace std;
intpow(intx,int y)
{ int r=1;
for(int i=1;i<=y;i++)
{ r*=x;
}
return r;
}
int main()
{
inta,b,res;
cout<<"enter the base number:";
cin>>a;
cout<<"enter the power:";
cin>>b;
res=pow(a,b);
cout<<"result is:"<<res<<endl;
return 0;
}

2
9

3
0

S-ar putea să vă placă și