Академический Документы
Профессиональный Документы
Культура Документы
PROGRAMS IN C++
NAME: ALOK
KUMAR
ROLL NO: 05
{
cout <<"Welcome in C++ Programming";
}
OUTPUT
Welcome in C++ Programming.
OUTPUT
Enter Number 1: 5
Enter Number 2: 6
Sum of numbers: 11
OUTPUT
Enter number:
else
cout<<"Num1 is smaller than num 2";
}
OUTPUT
Enter value for num 1: 5
Enter value for num 2: 10
Num1 is smaller than num 2
OUTPUT
Enter marks of Subject ENGLISH, MATHS & SCIENCE:
90
98
95
Percentage = 94.33333333
";
cin>>Celsius;
Fahrenheit=9*Celsius/5+32;
cout<<"Temperature in degree FAHRENHEIT:
}
OUTPUT
Enter temperature in degree CELCIUS:
40
104
"<<Fahrenheit;
"<<area;
OUTPUT
Enter Radius of circle: 14
Area of Circle:
615.44
#include<conio.h>
void main()
{
int x,y,z;
cout<<"Enter x: ";
cin>>x;
y=2*x;
z=2*x-1;
cout<<" Y = "<<y<<"\n"<<" Z = "<<z;
}
OUTPUT
Enter x: 5
Y = 10
Z= 9
int totaldays,years,weeks,days,rem1,rem2;
cout<<"Enter total number of days : ";
cin>>totaldays;
years=totaldays/365;
rem1=totaldays%365;
weeks=rem1/7;
rem2=rem1%7;
days=rem2;
cout<<"Years = "<<years<<"\tweeks = "<<weeks<<"\tdays = " <<days;
}
OUTPUT
Enter total number of days : 956
Years = 2
weeks = 32
days = 2
a=b;
b=c;
cout<<"\na= "<<a<<"\n"<<"b= "<<b;
}
OUTPUT
Enter a: 5
Enter b: 7
a= 7
b= 5
OUTPUT
Enter a: 4
Enter b: 9
Values after swapping:
a= 9
b= 4
12. Program to input three numbers and print the largest of three.
#include<iostream>
void main()
{
int x,y,z;
cout<<"Enter three variables : "<<"\n";
cin>>x>>y>>z;
if(x>y&&x>z)
cout<<"\n"<<x<<" is greater";
if(y>x&&y>z)
cout<<"\n"<<y<<" is greater";
if(z>x&&z>y)
cout<<"\n"<<z<<" is greater";
}
OUTPUT
Enter three variables :
5
9
3
9 is greatest
OUTPUT
Enter any number: 5
Number is odd
Enter any number: 18
Number is even
#include<iostream>
#include<conio.h>
void main()
{
int P,R,T;
float SI,CI;
cout<<"Enter values for P R T"<<"\n";
cin>>P>>R>>T;
SI=P*R*T/100;
CI=P*(1+R/100)^T;
cout<<"Simple interest= "<<SI<<"\n"<<"Compound Interest= "<<CI;
}
OUTPUT
Enter values for P R T
1000
5
3
Simple interest = 150
Compound Interest = 1003
int b, h, area;
cout<<"Enter values of Base and Height of triangle"<<"\n";
cin>>b>>h;
area=b*h/2;
cout<<"Area of triangle: "<<area;
}
OUTPUT
Enter values of Base and Height of triangle
5
4
Area of triangle: 10
16. Program that seeds the name of user and number of unit and
displays the electricity charge with name.
The electricity board charges according to following data:
For first 100 units
40p/unit
50p/unit
60p/unit
All the users are charged motor charge also which is Rs 50.
#include<iostream>
#include<stdio.h>
#include<conio.h>
void main()
{
char name[25];
int unit, charge;
cout<<"Enter your name: ";
gets(name);
cout<<"Enter total units: "<<\n;
cin>>unit;
if(unit<=100)
charge=100*40/100+50;
if(unit>100&&unit<=300)
charge=100*40/100+(unit-100)*50/100+50;
if(unit>300)
charge=100*40/100+200*50/100+(unit-300)*60/100+50;
puts(name);
cout<<"Total Electricity Charge: "<<charge;
}
OUTPUT
Enter your name: Alok Kumar
Enter total units: 285
Alok Kumar
Total Electricity Charge: 182
Grade
>90
<= 90 and>81
<= 80 and>71
<= 70 and>61
<= 60 and>51
<= 50 and>41
<=40
Fail
#include<iostream>
#include<stdio.h>
#include<conio.h>
void main()
{
char name[67], grade;
int eng, maths, cs, phy, chem, sum, percentage;
cout<<"Enter your name: ";
gets(name);
cout<<"Enter marks in 5 subjects"<<"\n";
cin>>eng>>maths>>cs>>phy>>chem;
sum=eng+maths+cs+phy+chem;
percentage=sum/5;
cout<<"\n"<<"\n";
puts(name);
cout<<"Percentage: "<< percentage <<'%';
if(percentage >90)
grade='A';
if(percentage <=90&& percentage >81)
grade='B';
if(percentage <=80&& percentage >71)
grade='C';
if(percentage <=70&& percentage >61)
grade='D';
if(percentage <=60&& percentage >51)
grade='E';
if(percentage <=50&& percentage >41)
grade='F';
if(percentage <=40)
grade='G';
cout<<"\n"<<"Grade: "<<grade;
}
OUTPUT
Enter your name: Alok Kumar
Enter marks in 5 subjects
93
97
95
90
99
Alok Kumar
Percentage: 94%
Grade: A
18. Program that accepts a character between A and J and prints next
4 characters.
#include<iostream>
#include<conio.h>
void main()
{
char ch;
cout<<"Enter character between A to J: ";
cin>>ch;
int num =ch;
cout<<char(num+1);
cout<<" "<<char(num+2);
cout<<" "<<char(num+3);
cout<<" "<<char(num+4);
}
OUTPUT
Enter character between A to J: F
GHIJ
{
char stu_type;
int coll_acc,hostel_acc;
cout<<"Enter student type: ";
cin>>stu_type;
if(stu_type=='A')
coll_acc=200;
cout<<\nCollage account:<<coll_acc;
if(stu_type=='B')
hostel_acc=200;
cout<<\nHostel account:<<hostel_acc;
}
OUTPUT
Enter student type: A
Collage account: 200
20. Program that print area for choice 1 and perimeter for choice 2 of
a circle
#include<iostream>
void main()
{
char choice;
int radius, area, peri;
cout<<"Enter radius of circle: ";
cin>>radius;
OUTPUT
Enter radius of circle: 7
Enter 1 for area or 2 for perimeter: 1
Area: 154
"<<P;
OUTPUT
Enter numbers w, x, y, z
5
7
6
3
P = (w + x)/(y-z) = 4
OUTPUT
Enter any number x: 5
Enter value of n: 4
x raised to the power n is: 625
Commission rate
30001 onwards
15%
22000 30000
10%
12001 22000
7%
5001 12000
3%
0 5000
0%
#include<iostream>
#include<conio.h>
void main()
{
float sales;
cout<<"Enter sales made by salesman: ";
cin>>sales;
if(sales>5000)
if(sales<12000)
if(sales<22000)
if(sales<30000)
cout<<"Commission = "<<sales*0.15;
else
cout<<"Commission = "<<sales*0.10;
else
cout<<"Commission = "<<sales*0.07;
else
cout<<"Commission = "<<sales*0.03;
else
cout<<"Commission = "<<sales*0;
}
OUTPUT
Enter sales made by salesman: 36548
Commission = 5482.2
ASCII Range
0 9
48 57
A Z
65 90
a z
97 - 122
#include<iostream>
#include<conio.h>
void main()
{
char ch;
int num;
cout<<"Enter any Character: ";
cin>>ch;
num=ch;
if(num>=48)
if(num>=65)
if(num>=97&&num<123)
cout<<"\nThe entered character is a lower case alphabet";
else
cout<<"\nThe entered character is a upper case alphabet";
else
OUTPUT
Enter any Character: j
The entered character is a lower case alphabet
OUTPUT
Enter any number: 2
2
10
12
14
16
18
20
if(a==0)
cout<<"The value of 'a' should not be zero \n Abording!!!!! \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";
cout<<"\n root1= "<<root1;
cout<<"\n root2= "<<root2;
}
else if(delta==0)
{
root1=root2=-b/(2*a);
cout<<"Roots are real and equal";
cout<<"\n Root1= "<<root1;
cout<<"\n Root2= "<<root2;
}
else
cout<<"roots are complex \n";
}
}
OUTPUT
Enter the values of a, b & c of the quadratic equation of the form ax2+bx+c
1
-5
4
Roots are real and unequal
Root1= 1
Root2= 4
OUTPUT
Enter any number: 17
Entered number is a PRIME NUMBER
64
128
#include<iostream>
#include<math.h>
#include<conio.h>
void main()
{
int j,i;
for(i=0;i<=7;++i)
{
j=pow(2,i);
cout<<j<<' ';
}
}
OUTPUT
1
16
32
16
32 64 128
29. Program that prints first n natural numbers and prints their sum.
#include<iostream>
#include<conio.h>
void main()
{
int i, n, sum=0;
cout<<"Enter value of n: ";
cin>>n;
for(i=1;i<=n;i++)
{cout<<i<<' ';
sum+=i;
}
cout<<"\nsum = "<<sum;
}
OUTPUT
Enter value of n: 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Sum = 210
30. Program to print sum of even and odd natural numbers in first n
natural numbers.
#include<iostream>
void main()
{
int n,i,sum1=0,sum2=0;
cout<<"Enter the number of numbers : ";
cin>>n;
for(i=1;i<=n;i++)
{if(i%2==0)
sum1+=i;
else
sum2+=i;
}
cout<<"\nSum of even numbers: "<<sum1;
cout<<"\nSum of odd numbers: "<<sum2;
}
OUTPUT
Enter the number of numbers: 10
Sum of even numbers: 30
Sum of odd numbers: 25
int i,j;
for(i=1;i<=5;i++)
{
cout<<"\n";
for(j=1;j<=i;j++)
cout<<'*';
}
}
OUTPUT
*
**
***
****
*****
for(j=1;j<=2*i-1;j++)
cout<<'*';
}
}
OUTPUT
*
***
*****
*******
OUTPUT
1
22
333
4444
55555
OUTPUT
Enter number of lines: 3
*
**
***
OUTPUT
Enter any number: 5
5 factorial = 120
OUTPUT
Enter number of lines: 4
A
AB
ABC
ABCD
int i, j, k, n;
cout<<"Enter number of lines: ";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\n";
for(j=1;j<=i-1;j++)
cout<< ;
for(k=n;k>=i;k--)
cout<<"& ";
}
}
OUTPUT
Enter number of lines: 4
&&&&
&&&
&&
&
while(n)
{n1=n%10;
rev=rev*10+n1;
n=n/10;
}
if(n2==rev)
cout<<"\nNumber is palindrome";
else
cout<<"\nNumber is not palindrome";
}
OUTPUT
Enter any number: 121
Number is palindrome
else
cout<<"It is not an alphabet.";
}
OUTPUT
Enter character: a
It is an alphabet.
Enter character: %
It is not an alphabet.
char ch;
cout<<"Enter character:";
cin>>ch;
if(isalpha(ch))
{
cout<<"It is an alphabet.\n";
if(isupper(ch))
cout<<"It is an upper case.";
else
cout<<"It is a lower case.";
}
else
OUTPUT
Enter character: r
It is an alphabet.
It is a lower case.
OUTPUT
Enter string 1: GOOD
Enter string 2: LUCK
GOODLUCK
Enter string 1: WEL
OUTPUT
Enter line: My name is Alok Kumar.
Number of words = 5
Enter line: Program to find out number of words present in a line.
Number of words = 11
#include<iostream>
#include<string.h>
void main()
{
char str[25];
cout<<"Enter string:";
cin.getline(str,25);
int len=strlen(str);
for(int i=len; i<=0; i--)
{
}
}
OUTPUT
Enter string: HELLO
HELLO
HELL
HEL
HE
H
char str[20];
if(str[i]!=str[len])
f=0;
break;
}
if(f==1)
cout<<"Entered string is palindrome.";
else
cout<<"Entered string is not a palindrome.";
}
OUTPUT
Enter string: RADAR
Entered string is palindrome.
int i, f=0;
cout<<"Enter string:";
cin.getline(str,20);
cout<<"Enter character:";
cin>>ch;
for(i=1; i<=len; i++)
{
if(str[i]==ch)
{
f=1;
break;
}
}
if(f==1)
cout<<The given character is present in the
string.<<\n The position of the character
else
cout<<The given character is not present in the
string.;
}
OUTPUT
Enter string: My name is Khan and I am not a terrorist.
Enter character: K
The given character is present in the string
The position of the character is: 12
is:<< i;
Enter character: t
The given character is present in the string
The position of the character is: 32
OUTPUT
Enter number: 5
Cube of the number: 125
47. Program to print largest even and odd number from a list of
numbers entered through keyboard. The list terminates as soon as one
enters zero (using function).
#include<iostream>
#include<conio.h>
void even_odd(int);
void main()
{
int n;
even_odd(n);
}
void even_odd(int n)
{
int maxeven=0, maxodd=0;
while(n)
{
cout<<"Enter number:";
cin>>n;
if(n%2==0)
{
if(n>maxeven)
maxeven=n;
}
else if(n%2==1)
{
if(n>maxodd)
maxodd=n;
}
else if(n==0)
break;
}
cout<<"Largest odd number:"<<maxodd<<"\n Largest even
number:"<<maxeven;
}
OUTPUT
Enter number:5
6
8
9
7
15
18
int n;
cout<<"Enter number:";
cin>>n;
factorial(n);
}
void factorial(int n)
{
int fact=1;
int i;
for(i=1; i<=n; i++)
fact=fact*i;
cout<< \nFactorial of <<n<< is <<fact;
OUTPUT
Enter number: 6
Factorial of 6 is 720
Enter number: 5
Factorial of 6 is 120
largest();
}
void largest()
cout<<"Enter number:";
cin>>n;
if(n>large)
large=n;
cout<<"You want to enter number?(y or n):";
cin>>ans;
}
cout<< \n Largest number entered: <<large;
}
OUTPUT
Enter number: 5 6 8 45 68 26 65 35 79 65 -6 64 25 64 3 9 10
Largest number entered: 79
50. Program that inputs three numbers and find the greatest
among them (using functions).
#include<iostream>
void greatest(int, int, int);
void main()
{
int max, d, e, f;
cout<<"Enter 3 number:";
cin>>d>>e>>f;
greatest(d, e, f);
}
void greatest(int d, int e, int f)
{
OUTPUT
Enter 3 number: 5
9
2
Second number is greatest
if(v[i]>large)
large=v[i];
else if(v[i]<small)
small=v[i];
}
cout<<"\n Largest element ="<<large;
OUTPUT
Enter the value in vector
5
Largest element = 9
Smallest element= 1
int a,b;
cout<<"Enter num1: \nEnter num2:";
cin>>a>>b;
swap (a,b);
}
void swap(int c, int d)
{
int temp;
temp =c;
c=d;
d=temp;
cout<< \nValues after swapping numbers.
cout<< \nNum1<<c<<"\nNum2"<<d;
OUTPUT
Enter num1: 9
Enter num2: 15
int n,s;
cout<<"Enter number:";
cin>>n;
s=sum(n);
cout<< \n Sum of digits :<<s;
}
int sum(int n1)
{
int p=0;
while(n1)
{
p=p+(n1%10);
n1=n1/10;
}
return p;
}
OUTPUT
Enter number:356
Sum of digits : 14
54. Program to print all those elements of a matrix that are on and are on
the right side of main diagonal.
#include<iostream>
void main()
{int arr[3][3];
cout<<"Enter matrix:";
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cin>>arr[i][j];
for(i=0;i<3;i++)
{
cout<<"\n";
for(int j=0;j<3;j++)
{
if(i<=j)
cout<<arr[i][j];
else
cout<<" ";
}}
}
OUTPUT
Enter matrix:
}
void negative(int arr1[], int size1)
{
if(arr1[i]<0)
arr1[i]=arr1[i]*-1;
}
for(i=0;i<size1; i++)
cout<<arr1[i]<<\t;
}
OUTPUT
Enter elements of an array:
1
-6
-8
-9
-9
int arr1[5][5];
int arr[]={1,2,3,4,5};
for(int i=0;i<5;i++)
for(int j=0;j<5;j++)
arr1[i][j]=arr[j];
if(i+j>4)
arr1[i][j]=0; }
for(i=0;i<5;i++)
{
cout<<"\n";
for(int j=0;j<5;j++)
cout<<arr1[i][j]<<" ";
}
}
OUTPUT
12345
12340
12300
12000
10000
cout<<"\n";
cout<<A[i][j]<<" ";}
for(i=0; i<3; i++)
for(j=0; j<3; j++)
{
B[k]=A[i][j];
k++;}
OUTPUT
Enter Array A:
123
123
123
Array B:
123123123
int i, j, k;
int A[5], B[5], C[10];
cout<<"Enter array A:";
for(i=0; i<5; i++)
cin>>A[i];
cout<<"Enter array B:";
for(j=0;j<5;j++)
cin>>B[j];
for(i=0,k=0; i<5;k++, i++)
C[k]=A[i];
for(k=5, i=0; i<5; k++, i++)
C[k]=B[i];
cout<<"\nArray C:";
for(k=0;k<10;k++)
cout<<C[i]<<" ";
}
OUTPUT
Enter array A:
01234
Enter array B:
56789
Array C:
0123456789
59. Program to replace a number from array with 0 and take all
0s of the array to the left.
#include<iostream>
void main()
{
int arr[9], n, t, k;
cout<<"Enter array:";
for(int i=0; i<9; i++)
cin>>arr[i];
cout<<"Enter number:";
cin>>n;
for(i=0; i<9; i++)
{
if(arr[i]==n)
arr[i]=0;
}
OUTPUT
Enter array:
Enter number: 5
}
for(i=0; i<3; i++)
{
sum=0;
for(int j=0; j<3; j++)
sum=sum+A[i][j];
cout<< \nSum of row<<i+1<< is <<sum<<"\n";
}
}
OUTPUT
Enter matrix A:
Sum of row 1 is 6
Sum of row 2 is 15
Sum of row 3 is 24
#include<iostream>
#include<conio.h>
void main()
{
int A[3][3], B[3][3], C[3][3];
cout<<"Enter matrix A:";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
cin>>A[i][j];
}
cout<<"Enter matrix B:";
for(i=0; i<3; i++)
{
for(int j=0; j<3; j++)
cin>>B[i][j];
}
cout<< \nMatrix A matrix B :
for(i=0; i<3; i++)
{
cout<<"\n";
for(int j=0; j<3; j++)
{
int sum=0;
for(int k=0; k<3; k++)
{
sum+=A[i][j]*B[k][j];
}
cout<<sum<<" ";
}
}
}
OUTPUT
Enter matrix A: 1
Enter matrix B: 2
10
10
Matrix A matrix B:
int A[10];
cout<<"Enter array:";
for(int i=0; i<10; i++)
cin>>A[i];
if(A[i]==A[j])
{
}}
for(i=0; A[i]!='\0'; i++)
{
if(A[i]==0)
A[i]='\0';
OUTPUT
Enter array: 5
New array: 5
int cday;
int cmonth;
int cyear;
int irate;
int totalamount;
int tot_years;
};
clients c[50];
void main()
{
float ci[50];
for(int i=0; i<50; i++)
{
gets(c[i].cname);
cin>>c[i].ccode;
cin>>c[i].cday;
cin>>c[i].cmonth;
cin>>c[i].cyear;
cin>>c[i].tot_years;
cin>>c[i].irate;
cin>>c[i].totalamount;
}
for (i=0; i<50; i++)
{
float t,s;
t[i]=(1+c[i].irate*0,01);
s[i]=pow(t[i],c1[i].year;
ci[i]=c1[i].totalamount*s[i];
cout<<ci[i];
}
}
{
cin>>e[i].eno;
gets(e[i].ename);
gets(e[i].eaddress);
do{
cout<<"enter employee no";
cin>>n;
if(n==e[i].eno)
{
cout<<e[i].eno;
puts(e[i].ename);
puts(e[i].eaddress);
}
cout<<"u want to proceed or not";
cin>>ans;
}
while(ans=='y');
}
}
int f=0;
for(i=0;i<25;i++)
{
if(s[i].sub1<=40)
f++;
if(s[i].sub2<=40)
f++;
if(s[i].sub3<=40)
f++;
if(f>1)
{
puts(s[i].name)
cout<<s[i].rno;
}
f=0;
}
}