Professional Documents
Culture Documents
C Programs Class 11 PDF
C Programs Class 11 PDF
PROGR A MS IN C+ +
2011-2012
#include<iostream.h>
void main()
{
cout <<"Welcome in C++ Programming";
}
OUTPUT
#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
#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
#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
#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
#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
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
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
#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
#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
OUTPUT
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
for(i=0;i<=7;++i)
{
j=pow(2,i);
cout<<j<<' ';
}
}
OUTPUT
1 2 4 8 16 32 64 128
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
OUTPUT
Enter the number of numbers: 10
Sum of even numbers: 30
Sum of odd numbers: 25
OUTPUT
*
**
***
****
*****
OUTPUT
*
***
*****
*******
OUTPUT
1
22
333
4444
55555
OUTPUT
5 factorial = 120
OUTPUT
Enter character: a
It is an alphabet.
Enter character: %
It is not an alphabet.
#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.
#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
#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
#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 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
#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
Enter number: 5
Factorial of 6 is 120
OUTPUT
Enter the value in vector
5 6 8 4 3 4 9 5 1 2
Largest element = 9
Smallest element= 1
#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
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
Matrix A × matrix B:
5 8 5
10 4 10
5 8 5
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
#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');
}
}
if(f>1)
{
puts(s[i].name)
cout<<s[i].rno;
}
f=0;
}
}