PSCP Assignment: Name: Kotana Gopi Chand ROLL NO.: 841936
PSCP Assignment: Name: Kotana Gopi Chand ROLL NO.: 841936
this number.
CODE:
#include<iostream>
using namespace std;
int main()
{
int a,b,c,sum;
cout<<"enter any four digit number"<<endl;
cin>>a;
b=a%10;
c=a/1000;
sum=b+c;
cout<<"required sum="<<sum;
return 0;
}
=====================================================================================
2) Enter a year through keyboard. Write a program to determine whether the year is leap year or not.
CODE:
#include<iostream>
using namespace std;
int main()
{
int year,a,b,c;
cout<<"enter any year"<<endl;
cin>>year;
a=year%4;
b=year%100;
c=year%400;
if(c==0||b!=0&&a==0)
cout<<"leap year";
else
cout<<"ordinary year";
return 0;
}
=====================================================================================
3) Write a program that will take three numbers from keyboard and find the maximum of these
CODE:
#include<iostream>
using namespace std;
int main()
{
int a,b,c,max,d;
cout<<"enter any three numbers"<<endl;
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
{
max=a;
cout<<"MAX="<<max;
}
else
{
max=c;
cout<<"MAX="<<max;
}
}
else
if(b>c)
{
max=b;
cout<<"MAX="<<max;
}
else
{
max=c;
cout<<"MAX="<<max;
}
d=max%2;
if(d==0)
cout<<endl<<"even number";
else
cout<<endl<<"odd number";
return 0;
}
=================================================================================
CODE:
#include<iostream>
using namespace std;
int main()
{
int n,sum=0,i;
float average;
cout<<"enter the value of n:";
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
cout<<"SUM="<<sum;
return 0;
}
=====================================================================================
5) Find out the average of n numbers.
CODE:
#include<iostream>
using namespace std;
int main()
{
int n,sum=0,i;
float average;
cout<<"enter the value of n:"<<endl;
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum+i;
}
cout<<endl<<"SUM="<<sum;
average=(float)sum/n;
cout<<endl<<"AVERAG="<<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
#include<iostream>
using namespace std;
int main()
{
int mp,d,sp,a,b,c;
cout<<"enter the marked price:";
cin>>mp;
cout<<endl<<"enter discount percentage:";
cin>>d;
cout<<endl<<"enter selling price:";
cin>>sp;
a=(d*sp)/100;
b=sp-a;
c=b-mp;
if(c>0)
cout<<"PROFIT="<<c;
else if(c<0)
cout<<"LOSS="<<-c;
else
cout<<"NEUTRAL";
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).
CODE:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int sum,num;
for(int i=0;i<=5;i++)
{
for(int j=0;j<=10;j++)
{
for(int k=0;k<=10;k++)
{
num=i*100+j*10+k;
sum=((i*i*i)+(j*j*j)+(k*k*k));
if(num==sum)
cout<<"numbers"<<num<<endl;
}
}
}
return 0;
}
====================================================================================
8) There are 9000 people in a town whose population increases by 15% each year. Write a program
displays the annual population and determines the number of years it will take for the population to
surpass 50000.
CODE:
#include<iostream>
using namespace std;
int main()
{
float n=0,a=9000,b;
b=a+(15*a)/100;
cout<<"annual population ="<<b<<endl;
while(a<=50000)
{
a=a+((a*15)/100);
n++;
cout<<"population value="<<a<<endl;
}
cout<<endl<<"number of years taken to surpass 50000 is "<<n;
return 0;
}
===============================================================================
CODE:
#include<iostream>
using namespace std;
int main()
{
int n,sum=0;
cout<<"enter any number"<<endl;
cin>>n;
while(n>0)
{
sum=sum+n%10;
n=n/10;
}
cout<<"sum of digits ="<<sum;
return 0;
}
========================================================================
10) Write a program to receive Cartesian coordinates (x,y) of a points and convert them
CODE:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
float x,y,r;
double angle;
double A,p=3.1415;
cout<<"enter coordinates in cartesian system "<<endl;
cin>>x>>y;
r=sqrt(x*x+y*y);
A=(180/p)*atan(y/x);
if(x>=0&&y>=0)
angle=A;
else if(x<=0&&y>=0)
angle=180-A;
else if(x<=0&&y<=0)
angle=180+A;
else
angle=360-A;
cout<<"polar coordinates are"<<"("<<r<<","<<angle<<")";
return 0;
}
========================================================================
11) Write a program to receive values of latitude(L1,L2) and longitude (G1,G2), in
degrees, oftwo places on the earth and output the distance(D) between then in nautical
miles.
CODE:
#include<iostream>
#include<cmath>
#define pi 3.14159265359
double convertDtR(double);
int main(){
//formulae
//a=sin(dellon/2)^2+cos(lat1)*cos(lat2)*sin(dellon/2)^2;
//c=2*atan2(sqrt(a),sqrt(1-a))=sininv
//d=R*c
double lat[2],lon[2];
cout<<"\n=>";cin >>lat[0]>>lon[0];
cout<<"\n=>";cin >>lat[1]>>lon[1];
for(int i=0;i<=1;i++){
lat[i]=convertDtR(lat[i]);
lon[i]=convertDtR(lon[i]);
double dellat=lat[1]-lat[0];
double dellon=lon[1]-lon[2];
double D=sin(dellat/2)*sin(dellat/2)+cos(lat[0])*cos(lat[1])*sin(dellon/2)*sin(dellon/2);
D=2*asin(sqrt(D));
D=D*r;
return 0;
return ((degree/180)*pi);
=============================================================================
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.
CODE:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double t,v,w;
cout<<"enter temperature in farenheit:"<<endl;
cin>>t;
cout<<"enter velocity of air in miles/hour:"<<endl;
cin>>v;
w=33-(((10*sqrt(v)-v+10.5)*(33-t))/23.1);
cout<<"windchill factor is="<<w;
return 0;
}
=====================================================================
13) If the value of an angle is input through the keyboard, write a program to print all its
Trigonometric ratios.
CODE:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double angle,A,p=3.1416;
cout<<"enter angle in degrees"<<endl;
cin>>angle;
A=(p/180)*angle;
cout<<"sin"<<A<<"="<<sin(A)<<endl;
cout<<"cos"<<A<<"="<<cos(A)<<endl;
cout<<"tan"<<A<<"="<<tan(A)<<endl;
cout<<"cosec"<<A<<"="<<1/sin(A)<<endl;
cout<<"sec"<<A<<"="<<1/cos(A)<<endl;
cout<<"cot"<<A<<"="<<1/tan(A);
return 0;
}
========================================================================
#include<iostream>
using namespace std;
int main()
{
int lcm,a,b;
cout <<"Enter any two positive numbers :";
cin >>a >> b;
int temp1=a,temp2=b;
if(a<=0||b<=0)
{
cout <<"Invalid Input";
return 0;
}
while(a!=b)
{
if(a>b)
{
a=a-b;
}
else
{
b=b-a;
}
}
lcm=(temp1*temp2)/a;
cout <<"The GCD is "<< a<<endl;
cout <<"The LCM is "<<lcm<<endl;
return 0;
}
=====================================================================================
15) Write a program to find the sum of first n terms of series:
CODE:
#include<iostream>
using namespace std;
int main()
{
float x,sum;
int n;
cout<<"enter value of x"<<endl;
cin>>x;
cout<<"enter no. of terms n"<<endl;
cin>>n;
float mul=1,f=1,i;
for(i=1;i<=n;i++)
{
mul=mul*x;
f=f*i;
sum=1+(mul/f);
}
cout<<"required sum ="<<sum;
return 0;
}
=====================================================================================
16) Write a program to accept a number and find sum of its individual digits repeatedly till the result
is asingle digit. For example, if the given number is 4687 the output should be 7.
CODE:
#include<iostream>
using namespace std;
int main()
{
long a;
int s,c,n,b,sum=0;
cout<<"enter any number"<<endl;
cin>>a;
while(a>0)
{
b=a%10;
sum=sum+b;
a=a/10;
if(sum>=10)
{
while(sum>0)
{
c=sum%10;
s=s+c;
sum=sum/10;
}
cout<<"required sum="<<s;
}
else
cout<<"required sum="<<sum;
}
return 0;
}
=====================================================================================
ABCDEF FEDCBA
ABCDE EDCBA
ABCD DCBA
ABC CBA
AB BA
A A
CODE:
#include<iostream>
using namespace std;
int main(){
int row=7;
for(int i=1;i<=(2*row-1);i++)
cout <<(char)('A'+i-1);
cout <<endl;
for(int i=2;i<=row;i++){
//left part
for(int j=1;j<=(row-i+1);j++){
cout <<(char)('A'+j-1);
}
//space
for(int spc=1;spc<=(2*(i-1)-1);spc++){
cout <<" ";
}
//right part
for(int j=row-i+1;j>=1;j--){
cout <<(char)('A'+j-1);
}
cout <<endl;
}
return 0;
}
------------------------------
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 3 3 1
1 2 1
1 1
CODE:
#include<iostream>
using namespace std;
//pascals triangle
int main()
{
int row=9,temp=1;
for(int i=0;i<(row+1)/2;i++){
for(int j=1;j<=(row+1)/2-i;j++)
cout<<" ";
cout<<temp<<" ";
for(int k=0;k<i;k++){
temp=(temp*(i-k))/(k+1);
cout<<temp<<" ";
}
cout<<endl;
}
for(int i=(row-3)/2;i>=0;i--){
for(int j=0;j<=(row-1)/2-i;j++)
cout<<" ";
cout<<temp<<" ";
for(int k=0;k<i;k++){
temp=(temp*(i-k))/(k+1);
cout<<temp<<" ";
}
cout<<endl;
}
return 0;
}
-------------------------------------------------
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
CODE:
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"enter number of rows:"<<endl;
cin>>n;
for(int i=1;i<=n;i++){
for(int spc=i;spc<n;spc++){
cout <<" ";
}
for(int j=1;j<=(2*i-1);j++){
cout<<"*";
}
cout <<endl;
}
for(int i=(n-1);i>=1;i--){
for(int spc=i;spc<n;spc++){
cout <<" ";
}
for(int j=1;j<=(2*i-1);j++){
cout<<"*";
}
cout <<endl;
}
return 0;
}
=====================================================================================
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
CODE:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int a,b,c;
cout <<"Enter the coefficient of x^2, x and x^0 : ";
cin >>a>>b>>c;
float dis= b*b-4*a*c;
if(dis <0){
cout <<endl;
cout <<"The roots of equation "<<a<<"x^2 + "<<b<<"x + "<<c<<" are: ";
cout <<endl;
cout << (b*-1)/(2*a)<<" + "<<(sqrt(dis*-1)/(2*a))<<"i"<<endl;
cout << (b*-1)/(2*a)<<" - "<<(sqrt(dis*-1)/(2*a))<<"i"<<endl;
}else{
cout <<endl;
cout <<"The roots of equation "<<a<<"x^2 + "<<b<<"x + "<<c<<" are: ";
cout <<endl;
cout << (b*-1+sqrt(dis))/(2*a)<<endl;
cout << (b*-1-sqrt(dis))/(2*a)<<endl;
}
return 0;
}
=====================================================================================
19) Write a program to find out the sum of the following series (up-to 30th term):
CODE:
#include<iostream>
using namespace std;
int main(){
int x;
cout <<"Enter value of x : ";
cin >>x;
if(x==0){
cout <<"The required sum is 0";
return 0;
}
float sum=0.0;
int sign =1,pro=x,fac=1;
for(int i=1;i<=30;i=i+2){
sum=sum+sign*((pro+0.0)/fac);
pro=pro*x*x;
sign*=-1;
fac=fac*(i+1)*(i+2);
}
cout <<"The required sum is "<<sum<<endl;
return 0;
}
=====================================================================================
1-Factorial of a number
2-Prime or not
3-Odd or even
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.
CODE:
#include<iostream>
using namespace std;
int main(){
while(true){
cout<<endl;
cout<<"-------------------------------------"<<endl;
cout<<"Please Enter your option :"<<endl;
cout<<"1- Factorial of a number \n";
cout<<"2- Prime or not \n";
cout<<"3- Odd or even \n";
cout<<"4- Nth Fibonacci number \n";
cout<<"5- Exit\n==> ";
int opt;
cin >>opt;
if(opt==5){
return 0;
}
if(opt<1 || opt>5){
continue;
}
switch(opt){
case 1:{
int num;
}
case 3:{
int num;
cout<<"Enter any non negative number: ";
cin >>num;
if(num<0){
cout<<"Invalid Input";
continue;
}
if(num%2==0){
cout<<"An even number";
}else{
cout<<"An odd number";
}
break;
}
case 4:{
int n;
cout<<"Enter value of n: ";
cin >>n;
if(n<=0){
cout<<"Not A Valid Input";
continue;
}
int a=0,b=1;
for(int i=1;i<n;i++){
b=a+b;
a=b-a;
}
cout<<"The "<<n<<"th fibonacci number is "<<a;
break;
}
default:
break;
}
}
=====================================================================================
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.
CODE:
#include<iostream>
int main(){
int num;
char chk;
int frequency=0,largest;
largest=num;frequency=1;
cin >>chk;
if(chk=='N'||chk=='n'){
return 0;
do{
check(frequency,largest,num);
cin >>chk;
if(chk=='N'||chk=='n'){
break;
}while(true);
cout<<endl;
cout<"----------------------------------------";
return 0;
}
if(num==largest){
frequency+=1;
largest=num;
frequency=1;
=====================================================================================
22) Write a program that reads an integer -n (decimal number system) and convert this decimal
CODE:
#include<iostream>
void binary(int);
void octal(int);
void hexa(int);
int main(){
int dec;
cin >>dec;
binary(dec);
octal(dec);
hexa(dec);
return 0;
int temp=0;
while(n){
rev=rev*10+n%2;
n=n/2;
temp++;
while(temp){
bin=bin*10+rev%10;
rev=rev/10;
temp--;
cout<<endl;
return;
int temp=0;
while(n){
rev=rev*10+n%8;
n=n/8;
temp++;
while(temp){
oct=oct*10+rev%10;
rev=rev/10;
temp--;
cout<<endl;
return;
char arr[100];
int index=0;
while(n){
if(dig<=9){
arr[index]=(char)('1'+dig-1);
index++;
}else{
switch(dig){
case 10:
arr[index]='A';
index++;
break;
case 11:
arr[index]='B';
index++;
break;
case 12:
arr[index]='C';
index++;
break;
case 13:
arr[index]='D';
index++;
break;
case 14:
arr[index]='E';
index++;
break;
case 15:
arr[index]='F';
index++;
break;
default:
break;
n=n/16;
for(int i=index-1;i>=0;i--){
cout<<arr[i];
cout<<endl;
return;
=====================================================================================
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 scalen, equilateral, isoceless or right angled triangle.
CODE:
#include<iostream>
int main(){
int a,b,c;
cin >>a>>b>>c;
if(a+b+c==180){
if(a==b&&a==c){
}else{
}else{
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
#include<iostream>
int main(){
int n;
cin >>n;
int a=0,b=1;
int index=0;
while(a<n){
b=a+b;
a=b-a;
index++;
if(a==n){
}else{
return 0;
=====================================================================================
25) Write a program to print all the ASCII values and their equivalent characters using a while loop.
CODE:
#include<iostream>
int main(){
int i=0;
cout<<"----------------------------------------------------------";
cout<<endl;
for(int i=32;i<=126;i++){
cout<<i<<"\t\t "<<(char)i;
cout<<endl;
i++;
=====================================================================================
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
CODE:
#include<iostream>
#include <iomanip>
int main(){
int n;
float pi=0;
int sign=1;
for(int i=0;i<=n;i++){
pi+=(sign+0.0)/(2*i+1.0);
sign*=-1;
pi=pi*4;
cout<<setprecision(3)<<fixed;
cout<<pi<<endl;
=====================================================================================
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.
CODE:
#include<iostream>
int main()
int n,a[4],i;
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;
=====================================================================================
CODE:
#include<iostream>
int main(){
int base,exponent;
cin>>exponent;
cout<<endl;
cout<<base<<"^"<<exponent<<" = "<<result;
for(int i=1;i<=e;i++){
pro*=b;
return pro;
=====================================================================================
29) A positive integer is entered through the keyboard, write a function to find the binary equivalent
this number:
CODE:
#include<iostream>
int main(){
int dec;
cin >>dec;
return 0;
int dig=0;
while(dec){
rev=rev*10+dec%2;
dec/=2;
dig++;
while(dig){
org=org*10+rev%10;
rev/=10;
dig--;
return org;
=====================================================================================
CODE:
#include<iostream>
using namespace std;
void tower(int,char,char,char);
int main(){
int n;
cin>>n;
cin>>from;
cin>>to;
cin>> left;
cout<<endl;
cout<<"----------------------------------------"<<endl;
tower(n,from,to,left);
if(n==1){
cout<<endl;
return;
tower(n-1,a,b,c);
tower(n-1,b,c,a);