You are on page 1of 20

C++

Programs

It include programs related following chapter:


Arrays
Functions
Structures

Name Chaand Chopra


Class-XI-G
Roll No.-8
Year-2011-2012

Contents
1.Program to print the largest and smallest no. from the array given by user .......................................... 2
2.Program to print the average and total sales of month taken from user through array........................ 3
3.Program to print the sum of 2 array taken from the user........................................................................ 4
4.Program to reverse the string enter by the user. ..................................................................................... 6
6.Program to print the cube of the no. enter ed by user ............................................................................. 8
7.Program to illustrate the call by value method of function invoking....................................................... 9
8.Program to print the square of no. given by the user ............................................................................. 10
9.Program to swap two values ................................................................................................................... 11
10.Program to take roll no. and name and from the user using the structure. ........................................ 12
11. Program to print member type of a gym member................................................................................ 13
12.Program to take feet and inches from the user and find the total. ...................................................... 14
13.Program to search an element in the array entered by the user. ........................................................ 15
14.Simple program of structure. ................................................................................................................ 16
15.Program to take name and type of the movie from the user and his and check that he is eligible for
the movie..................................................................................................................................................... 18

Program to print the largest and smallest number of the array given by user.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int v[50],large,small;
int i,n;
cout<<"Enter how many elements ? \n";
cin>>n;
cout<<"Enter values in the vector \n";
for(i=0;i<n;i++)
cin>>v[i];
large=v[1];
small=v[1];
for(i=0;i<n;i++)
{
if(v[i]<large)
large =v[i];
else if(v[i]<small)
small=v[i];
}
cout<<"\nLargest Element ="<<large ;
cout<<"\nSmallest Element ="<<small ;
}

Output:

Program to print the average and total sales of month taken from user through
array
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
const int size=3 ;
float sales[size],avg=0,total=0 ;
for(int i=0;i<size;i++)
{
cout<<"enter sales made on day"<<i+1<<":";
cin>>sales[i];
total+=sales[i];
}
avg=total/size;
cout<<"\n Total sales ="<<total<<"\n";
cout<<"Average sales ="<<avg<<"\n";
return 0;
}
Output:

Program to print the sum of 2 array taken from the user.


#include<iostream.h>
#include<conio.h>
void main()
{
int arr[i][j],arr2[i][j],sum[i][j],i,j;
cout<<"Enter the matrix :";
for(int i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
cin>>arr[i][j];
}
}
for(int i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
cin>>arr2[i][j];
}
}
for(int i=0;i<=3;i++)
{
for(int j=0;j<=3;j++)
{
sum[i][j]=arr[i][j]+arr2[i][j];
}
}
for(int i=0;i<=3;i++)

5
{
for(int j=0;j<=3;j++)
{
cout<<sum[i][j];
}
}
getch();
}
Output:

Program to reverse the string enter by the user .

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
clrscr();
char str[80];
int i :
cout<<"\n Enter any string (max 80 chars) :";
gets(str);
str[0]=toupper(str[0]);
for(i=0;str[i]!='\0';i++)
if(str[i]=='')
str[i+1]=toupper(str[i+1]);
cout<<"\nUpdated string is:"<<str;
getch();
}
Output:

Program to print the length of the string entered by user


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char str[80];
int i;
cout<<"\nEnter any string (max. 80) :";
gets(str);
for(int i=0;str[i]!='\0';++i);
cout<<"\n Length of the string is :"<<i;
getch();
}
Output:

Program to print the cube of the

no. enter ed by user

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float cube(float);
float x, y;
cout<<"\n Enter number whose cube is to be calculated : \n";
cin>>x;
y=cube(x);
cout<<"\n The cube of "<<x<<"is "<<y<<"\n";
getch();
return 0;
}
float cube(float a)
{
float n;
n=a*a*a;
return(n);
}
Output:

Program to illustrate the call by value method of function invoking


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int change(int);
int orig=10;
cout<<"\n The original value is "<<orig<<"\n";
cout<<"\n Return value of function change () is "<<change(orig)<<"\n";
cout<<"\n The value after function change() is over"<<orig<<"\n";
getch();
}
int change(int a )
{
a=20;
return a;
}
Output:

10

Program to print the square of no. given by the user

#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
float sqaure(float);
float x, y;
cout<<"\n Enter number whose sqaure is to be calculated : \n";
cin>>x;
y=sqaure(x);
cout<<"\n The sqaure of "<<x<<"is "<<y<<"\n";
getch();
return 0;
}
float sqaure(float a)
{
float n;
n=a*a;
return(n);
}
Output:

11

Program to swap two values


#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
void swap(int &,int &);
int a,b;
a=7;
b=4;
cout<<"\n The original value are: \n";
cout<<"a="<<",b="<<b<<"\n";
getch();
return 0;
}
void swap(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
cout<<""\n The swapped values are :\n";
cout<<"a="<<x<<",b="<<y<<"\n";
}
Output:

12

Program to take roll no. and name and from the user using the structure.
#include<iostream.h>
#include<conio.h>
struct student
{
float m1, m2, m3, total, per;
int roll;
char name[10];
};
void main( )
{
student st;
cout<<"Enter name: ";
cin>>st.name;
cout<<"Enter roll number: ";
cin>>st.roll;
cout<<"Enter marks of 3 subjects: ";
cin>>st.m1>>st.m2>>st.m3;
st.total=st.m1+st.m2+st.m3;
st.per=st.total/3;
cout<<"Total: "<<st.total;
cout<<"Percentage is: "<<st.per;
getch( );
}
Output:

13

Program to print member type of a gym member.


#include<iostream.h>
struct supergym
{
int membernumber;
char membernumber[20];
char membertype[4];
};
void main( )
{
supergym person1, person2;
cout << "Member Number: " ;
cin >> person1.membernumber ;
cout << "Member Name: " ;
cin >> person1.membername ;
strcpy (person1.membertype, "MIG") ;
person2 = person1 ;
cout << "Member Number: "<<person2.membernumber<<endl ;
cout << "Member Name: "<<person2.membername<<endl ;
cout << "Member Type: "<<person2.membertype<<endl ;
}
Output:

14

Program to take feet and inches from the user and find the total.

#include<iostream.h>
#include<conio.h>
struct distance
{

int feet;

int inches;
};
int main()
{

clrscr();
Distance length1,lenght2;

void prnsum(distance & l1,distance&l2);


cout<<Enter length 1 :<<\n;
cout<<Feet :;
cin>>length1.feet ;
cout<<\n<<Inches;
cin>>length2.feet;
prnsum(length1,length2);
return 0;
}
void prnsum(distance & l1,distance & l2)
{

distance l3;

l3.feet=l1.feet+l2.feet+(l1.inches+l2.inches)/12;
l3.inches=(l1.inches+l2.inches)%12;
cout<<\n \n Total Inches :<<l3.inches;
return ;
}

Output:

15

Program to search an element in the array entered by the user.


#include<iostream.h>
#include<conio.h>
void main()
{
int A[20],size,i,flag=0,num,pos;
cout<<"\n Enter the number of element in the array";
cin>>size;
cout<<"\n Enter the element of array :";
for(i=0;i<size;i++)
{
cin>>A[i];
}

cout<<"\n Enter the element to be searched for:";


cin>>num;
for(i=0;i<size;i++)
{
if(A[i] ==num)
{
flag=1;
pos=i;
break;
x}
if(flag==0)
cout<<"\nElement found at position"<<pos+1;
else
cout<<"\n Element not found";

16
getch();
}
}

Output:

17

Simple program of structure.


#include<iostream.h>
struct Point
{

int X,Y;

};
void main (Point P)
{

cout<< P.X<<':'<<P.Y<<endl;

}
void main()
{
Point U={20,10},V,W;
V=U;
V.X+=20;
W=V;
U.Y+=10;
U.X+=5;
W.X-=5;
Show (U);
Show (V);
Show (W);
}
Output:

18

Program to take name and type of the movie from the user and his and check that
he is eligible for the movie.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
struct movie
{
char movie_name[20];
char movie_type;
int ticket_cost;
int age;
} MOVIE;
char a,u;
MOVIE.ticket_cost=100;
cout<<"Enter the name of movie";
gets(MOVIE.movie_name);
cout<<"Enter the Type of Movie (u/a):";
cin>>MOVIE.movie_type;
cout<<"Enter Your Age :";
cin>>MOVIE.age;
if (MOVIE.movie_type==a)
cout<<"This movie is adult.";
else if (MOVIE.age<18)
cout<<"The movie is not for you.";
else if (MOVIE.age>18)
cout<<"The movie is for you.";

19
else
cout<<"Enjoy!";
if(MOVIE.movie_type==u)
cout<<you can watch this movie;
else
cout<<Enjoy!!!;
}
Output:

You might also like