Vous êtes sur la page 1sur 79

PROGRAMS IN C++

2011-2012

NAME: ALOK KUMAR
ROLL NO: 05



1. Program to print WELCOME IN C++.

#include<iostream.h>
void main()
{
cout <<"Welcome in C++ Programming";
}


OUTPUT

Welcome in C++ Programming.










2. Program to find sum of two numbers.

#include<iostream.h>
void main()
{
int num1,num2,sum;
cout<<"Enter Number 1: ";
cin>>num1;
cout<<"Enter Number 2: ";
cin>>num2;
sum=num1+num2;
cout<<"Sum of numbers: "<<sum;
}

OUTPUT

Enter Number 1: 5
Enter Number 2: 6
Sum of numbers: 11



3. Program to find square of a number.

#include<iostream.h>
void main()
{
int num1,square;
cout<<"Enter number: ";
cin>>num1;
square=num1*num1;
cout<<"Square of number is: "<<square;
}

OUTPUT

Enter number: 5
Square of number is: 25






4. Program to check whether a number is greater than or
less than other number.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1,num2;
cout<<"Enter value for num 1: ;
cin>>num1;
cout<<"Enter value for num 2: ";
cin>>num2;
if(num1>num2)
cout<<"Num1 is greater than num 2";
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
5. Program to calculate percentage marks for three
subjects.

#include<iostream.h>
void main()
{
float English, Maths, Science, Sum, Percentage;
cout<<"Enter marks of Subject ENGLISH, MATHS & SCIENCE: ";
cin>>English>>Maths>>Science;
Sum=English+Maths+Science;
Percentage=Sum*100/300;
cout<<"Percentage = "<<Percentage;
}

OUTPUT
Enter marks of Subject ENGLISH, MATHS & SCIENCE:
90
98
95
Percentage = 94.33333333


6. Program that reads temperature in Celsius and displays
it in Fahrenheit.

#include<iostream.h>
void main()
{
int Celsius, Fahrenheit;
cout<<"Enter temperature in degree CELCIUS: ";
cin>>Celsius;
Fahrenheit=9*Celsius/5+32;
cout<<"Temperature in degree FAHRENHEIT: "<<Fahrenheit;
}

OUTPUT

Enter temperature in degree CELCIUS: 40
Temperature in degree FAHRENHEIT: 104






7. Program that reads radius of a circle and prints its area.

#include<iostream.h>
void main()
{
float radius,area;
cout<<"Enter Radius of circle: ";
cin>>radius;
area=radius*3.14*radius;
cout<<"Area of Circle: "<<area;
}

OUTPUT

Enter Radius of circle: 14
Area of Circle: 615.44






8. Program that enter value of x from user and prints Y =
2x and Z = 2x -1.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
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

9. Program to convert a given number of days into years,
weeks and days.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
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
10. Program for swapping of two numbers using third
number.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a, b, c;
cout<<"Enter a : ";
cin>>a;
cout<<"Enter b : ";
cin>>b;
c=a;
a=b;
b=c;
cout<<"\na= "<<a<<"\n"<<"b= "<<b;
}

OUTPUT
Enter a: 5
Enter b: 7
a= 7
b= 5
11. Program for swapping of two numbers without using
third variable.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<"Enter a : ";
cin>>a;
cout<<"Enter b : ";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"Values after swapping: "<<"\na= "<<a<<"\nb= "<<b;
}
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.h>
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
13. Program to check whether the entered number is odd
or even.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num;
cout<<"Enter any number: ;
cin>>num;
if(num%2==0)
cout<<"Number is even";
else
cout<<"Number is odd";
}

OUTPUT
Enter any number: 5
Number is odd

Enter any number: 18
Number is even
14. Program to find Simple Interest and Compound
Interest.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
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
15. Program to find area of a triangle.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
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
For next 200 units = 50p/unit
Beyond 300 units = 60p/unit
All the users are charged motor charge also which is Rs 50.


#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
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





17. Program to enter marks in five subjects and
calculate percentage. Display grade according to
following specifications.
Percentage Grade
>90 A
<= 90 and>81 B
<= 80 and>71 C
<= 70 and>61 D
<= 60 and>51 E
<= 50 and>41 F
<=40 Fail



#include<iostream.h>
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
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.h>
#include<conio.h>
void main()
{
clrscr();
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
G H I J

19. Program to input a student type (A or B), and for
A initialize the collage account with Rs 200 otherwise
initialize the hostel account with Rs 200.

#include<iostream.h>
void main()
{
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.h>
void main()
{
char choice;
int radius, area, peri;
cout<<"Enter radius of circle: ";
cin>>radius;
cout<<"Enter 1 for area or 2 for perimeter: ";
cin>>choice;
area=22*radius*radius/7;
peri=2*22*radius/7;
if(choice=='1')
cout<<"Area: "<<area;
else
cout<<"Perimeter: "<<peri;
}

OUTPUT
Enter radius of circle: 7
Enter 1 for area or 2 for perimeter: 1
Area: 154
21. Program to find value of P if P= (w + x)/(y-z). The
value of w, x, y, z are entered by user.

#include<iostream.h>
void main()
{
float w, x, y, z, P;
cout<<"Enter numbers w, x, y, z";
cin>>w>>x>>y>>z;
P= (w + x)/(y-z);
cout<<"\nP = (w + x)/(y-z) = "<<P;
}

OUTPUT
Enter numbers w, x, y, z
5
7
6
3

P = (w + x)/(y-z) = 4


22. Program which raise any number x to a positive
power n.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
long double x,n,p;
cout<<"Enter any number x: ";
cin>>x;
cout<<"Enter value of n: ";
cin>>n;
p=pow(x,n);
cout<<"x raised to the power n is: "<<p;
}

OUTPUT
Enter any number x: 5
Enter value of n: 4
x raised to the power n is: 625

23. Program to calculate commission for the salesmen the
commission is calculated according to following rates.
Sales Commission rate
30001 onwards 15%
22000 30000 10%
12001 22000 7%
5001 12000 3%
0 5000 0%

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
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







24. Program to print whether the entered character is an
uppercase or a lowercase character or a digit or any other
character. The ASCII codes are given below.
Characters ASCII Range
0 9 48 57
A Z 65 90
a z 97 - 122

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
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
cout<<"\nThe entered character is a digit";
else
cout<<"\nThe entered character is any other character";
}

OUTPUT

Enter any Character: j
The entered character is a lower case alphabet








25. Program to print table of any number.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i;
cout<<"\nEnter any number :";
cin>>n;
for(i=1;i<=10;i++)
{
cout<<"\t";
cout<<n*i;
}
}

OUTPUT

Enter any number: 2
2 4 6 8 10 12 14 16 18 20

26. Program to print roots of a quadratic equation.

#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float a,b,c,root1,root2,delta;
cout<<"Enter the values of a, b & c of the quadratic equation of the form
\nax
2
+bx+c \n";
cin>>a>>b>>c;
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
ax
2
+bx+c
1
-5
4
Roots are real and unequal
Root1= 1
Root2= 4
27. Program to find a number is prime or not.

#include<iostream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
int n,i;
cout<<"Enter any number: ";
cin>>n;
for(i=2;i<=n-1;i++)
if(n%i==0)
{
cout<<"\nEntered number is NOT a PRIME NUMBER ";
exit(0);
}
cout<<"\nEntered number is a PRIME NUMBER ";
}
OUTPUT
Enter any number: 17
Entered number is a PRIME NUMBER
28. Program that prints the following series
1 2 4 8 16 32 64 128

#include<iostream.h>
#include<math.h>
#include<conio.h>
void main()
{
clrscr();
int j,i;

for(i=0;i<=7;++i)
{
j=pow(2,i);
cout<<j<<' ';
}
}

OUTPUT

1 2 4 8 16 32 64 128

29. Program that prints first n natural numbers and prints
their sum.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
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.h>
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
31. Program for generating the given output.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=1;i<=5;i++)
{
cout<<"\n";
for(j=1;j<=i;j++)
cout<<'*';
}
}

OUTPUT
*
**
***
****
*****
32. Program for generating the given output.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
for(i=1;i<=4;i++)
{
cout<<"\n";
for(j=1;j<=2*i-1;j++)
cout<<'*';
}
}

OUTPUT
*
***
*****
*******

33. Program for generating the given output.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=1;i<=5;++i)
{
cout<<"\n";
for(int j=1;j<=i;++j)
cout<<i;
}
}

OUTPUT
1
22
333
4444
55555
34. Program for generating the given output.

#include<iostream.h>
void main()
{
int i, j, k, n;
cout<<"Enter number of lines: ";
cin>>n;
for(i=1;i<=n;i++)
{
cout<<"\n";
for(j=1;j<=n-i;j++)
cout<<' ';
for(k=1;k<=i;k++)
cout<<'*';
}
}
OUTPUT
Enter number of lines: 3
*
**
***
35. Program to print factorial of a number.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, n, facto=1;
cout<<"Enter any number: ";
cin>>n;
for(i=1;i<=n;i++)
facto*=i;
cout<<"\n"<<n<<" factorial = "<<facto;
}

OUTPUT

Enter any number: 5

5 factorial = 120


36. Program for generating the given output.

#include<iostream.h>
void main()
{
char ch;
int i=ch, j, n;
cout<<"Enter number of lines: ";
cin>>n;
for(i=65;i<=n+65;i++)
{
cout<<"\n";
for(j=65;j<=i;j++)
cout<<char(j);
}
}
OUTPUT
Enter number of lines: 4
A
AB
ABC
ABCD
37. Program for generating the given output.
#include<iostream.h>
void main()
{
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
& & & &
& & &
& &
&
38. Program to check whether a number is palindrome or
not.
#include<iostream.h>
#include<string.h>
void main()
{
int n, n1, n2=n, rev=0;
cout<<"Enter any number: ";
cin>>n;
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

39. Program to check whether entered character is an
alphabet or not.

#include<iostream.h>
#include<ctype.h>
void main()
{
char ch;
cout<<"Enter character :";
cin>>ch;
if(isalpha(ch))
cout<<"It is an alphabet.";
else
cout<<"It is not an alphabet.";
}

OUTPUT
Enter character: a
It is an alphabet.

Enter character: %
It is not an alphabet.
40. Program to check whether the entered character is
of uppercase or lowercase.

#include<iostream.h>
#include<ctype.h>
void main()
{ 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
cout<<"It is not an alphabet.";
}
OUTPUT
Enter character: r
It is an alphabet.
It is a lower case.
41. Program to concatenate two strings.

#include<iostream.h>
#include<stdio.h>
#include<string.h>
void main()
{
char s1[10], s2[10];
cout<<"Enter string 1:";
gets(s1);
cout<<"Enter string 2:";
gets(s2);
cout<<strcat(s1,s2);

}
OUTPUT
Enter string 1: GOOD
Enter string 2: LUCK
GOODLUCK
Enter string 1: WEL
Enter string 2: COME
WELCOME
42. Program to find out number of words present in a
line.

#include<iostream.h>
#include<string.h>
void main()
{
int n=1;
char str[100];
cout<<"Enter line:";
cin.getline(str,100);
for(int i=1; str[i]!='\0'; i++)
{
if ( str[i-1]!=' ' && str[i]==' ')
n=n+1;
}
cout<< \nNumber of words = <<n;
}
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
43. Program to print a word by deleting 1 letter from
end in each turn.
#include<iostream.h>
#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--)
{ for(int j=0; j<=i; j++)
cout<<str[j];
cout<<\n;
}
}
OUTPUT
Enter string: HELLO
HELLO
HELL
HEL
HE
H
44. Program to check whether the given string is
palindrome or not.
#include<iostream.h>
#include<string.h>
void main()
{ char str[20];
int len, i, f=1;
cout<<"Enter string:";
cin.getline(str,20);
len=strlen(str);
for(i=0, len=len-1; i<=len/2; i++, len--)
{ 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.
45. Program to find whether a given character is present
in a string or not and also find the position of character.

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{ clrscr();
char str[20], ch;
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
is:<< i;
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

Enter character: t
The given character is present in the string
The position of the character is: 32




46. Program to find cube of a number (using
function).
#include<iostream.h>
int cube(int);
void main()
{
int a,c;
cout<<"Enter number: ";
cin>>a;
c=cube(a);
cout<<"Cube of the number: "<<c;
}
int cube(int b)
{
int n;
n=b*b*b;
return n;
}

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.h>
#include<conio.h>
void even_odd(int);
void main()
{
clrscr();
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
Largest odd number: 15
Largest even number: 18
48. Program to calculate factorial of a number (using
function).
#include<iostream.h>
void factorial(int);
void main()
{ 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
49. Program to find largest number of a list of no.
entered through keyboard (using function).
#include<iostream.h>
void largest();
void main()
{ largest();
}
void largest()
{ char ans='y'; int n, large=0;
while(ans=='y')
{ 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.h>
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)
{ if(d>e && d>f)
cout<<"First number is greatest";
else if(e>d && e>f)
cout<<"Second number is greatest";
else if(f>d && f>e)
cout<<"Third number is greatest";
}
OUTPUT
Enter 3 number: 5
9
2
Second number is greatest
51. Program to find largest and smallest element in a
vector.
#include<iostream.h>
void main()
{ int v[10], large, small, i;
cout<<"Enter the value in vector ";
for(i=0;i<10; i++)
cin>>v[i];
large=v[1];
small=v[1] ;
for (i=0;i<10;i++)
{ if(v[i]>large)
large=v[i];
else if(v[i]<small)
small=v[i];
}
cout<<"\n Largest element ="<<large;
cout<<"\n Smallest element= "<<small;
}
OUTPUT
Enter the value in vector
5 6 8 4 3 4 9 5 1 2
Largest element = 9
Smallest element= 1
52. Program for swapping of two numbers (using
function).
#include<iostream.h>
void swap(int,int);
void main()
{ 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
Value after swapping numbers.
Num1: 15
Num2: 9
53. Program to find sum of digits of a number (using
function).
#include<iostream.h>
int sum(int);
void main()
{ 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.h>
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: 2 6 9
8 1 5
4 2 3
2 6 9
1 5
3
55. Program to print and convert all the elements of
an array positive (using function).
#include<iostream.h>
void negative(int arr[],int);
void main()
{ int size=10, arr[size];
cout<<"Enter elements of an array:";
for(int i=0; i<size; i++)
cin>>arr[i];
negative(arr,size);
}
void negative(int arr1[], int size1)
{ for(int i=0; i<size1; i++)
{ 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 2 -6 -8 5 -9 5 4 8 -9
1 2 6 8 5 9 5 4 8 9
56. Program to generate the given output.
#include<iostream.h>
void main()
{ 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];
for(i=0; i<5; i++)
for(int j=0; j<5; 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
1 2 3 4 5
1 2 3 4 0
1 2 3 0 0
1 2 0 0 0
1 0 0 0 0
57. Program to display a 2D array in 1D array.
#include<iostream.h>
void main()
{ int A[3][3], B[9];
int i,j,k=0;
cout<<"Enter Array A:";
for(i=0; i<3; i++)
for(j=0; j<3; j++)
cin>>A[i][j];
for(i=0; i<3; i++)
{ cout<<"\n";
for(j=0; j<4; j++)
cout<<A[i][j]<<" ";}
for(i=0; i<3; i++)
for(j=0; j<3; j++)
{ B[k]=A[i][j];
k++;}
for(k=0; k<9; k++)
cout<<B[k];
}
OUTPUT
Enter Array A: 1 2 3
1 2 3
1 2 3
Array B: 1 2 3 1 2 3 1 2 3
58. Program to concatenate two arrays.
#include<iostream.h>
void main()
{ 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: 0 1 2 3 4
Enter array B: 5 6 7 8 9
Array C: 0 1 2 3 4 5 6 7 8 9
59. Program to replace a number from array with 0
and take all 0s of the array to the left.

#include<iostream.h>
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;
}
for(i=0; i<9; i++)
cout<<arr[i]<<"\n";
cout<<"\n";
for(k=0;k<2; k++)
{
for(i=8; i>=0; i--)
{
if(arr[i]==0)
{
for(int j=i;j>0;j--)
{
t=arr[j];
arr[j]=arr[j-1];
arr[j-1]=t;
}
}
}
}
cout<< Array after replacing <<n<<: ;
for(i=0; i<9; i++)
cout<<arr[i];
}

OUTPUT
Enter array: 6 5 4 6 9 5 1 5 8
Enter number: 5
Array after replacing 5:
0 0 0 6 4 6 9 1 8

60. Program to calculate the sum of elements of the
rows of matrix.
#include<iostream.h>
void main()
{ int A[3][3], sum;
cout<<"Enter matrix A:";
for(int i=0; i<3; i++)
{ for(int j=0; j<3; j++)
cin>>A[i][j];
}
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: 1 2 3
4 5 6
7 8 9
Sum of row 1 is 6
Sum of row 2 is 15
Sum of row 3 is 24
61. Program to multiply two matrices.
#include<iostream.h>
#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 2 1
2 1 2
1 2 1
Enter matrix B: 2 1 2
1 2 1
2 1 2

Matrix A matrix B:
5 8 5
10 4 10
5 8 5
62. Program to delete duplicate elements of an array
with 0 and take all 0 to right.
#include<iostream.h>
void main()
{ int A[10];
cout<<"Enter array:";
for(int i=0; i<10; i++)
cin>>A[i];
for(i=0; A[i]!='\0'; i++)
{ for(int j=i+1; A[j]!='\0'; j++)
{ if(A[i]==A[j])
{ for(int k=j; A[k]!='\0'; k++)
A[k]=A[k+1]; }
}}
for(i=0; A[i]!='\0'; i++)
{ if(A[i]==0)
A[i]='\0'; }
cout<< \nNew array:;
for(i=0; i<10; i++)
cout<<A[i]<<" ";
}
OUTPUT
Enter array: 5 8 9 6 4 8 9 3 6 5
New array: 5 8 9 6 4 3 0 0 0 0
63. Program to calculate compound interest for 50 clients
of an investment company.
Details (including costume name, code and date of starting,
number of years, interest rate and total amount) are stored
in an array of structures.
#include<iostream.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
struct clients
{
char cname[20];
int ccode;
int cday;
int cmonth;
int cyear;
int irate;
int totalamount;
int tot_years;
};
clients c[50];
void main()
{
clrscr();
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];
}

}
64. Program to store information of 10 employee and
to display information of an employee depending
upon the employee number given.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct employee
{
int eno;
char ename[20];
char eaddress[20];
}e[10];
void main()
{
clrscr();
int i,n;
char ans='y';
for(i=0;i<10;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');
}
}







65. Program to create an array containing details of
25 students (including Roll number, name, marks in
three subjects) and print out a list of students who
have failed in more than 1 subject. Assume 40%
marks as pass marks.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct student
{ int rno;
char name[20];
int sub1;
int sub2;
int sub3;
}s[25];
void main()
{
for(int i=0;i<25;i++)
{
cout<<"Enter student Roll number : ";
cin>>s[i].rno;
cout<<"Enter student name : ";
gets(s[i].name);
cout<<"Enter marks obtained in three subjects : ";
cin>>s[i].sub1>>s[i].sub2>>s[i].sub3;
}
cout<<"list of students failed in more than 1 subject";
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;
}
}

Vous aimerez peut-être aussi