/*
Program to display numbers between 0 to 9 as well as 9 to 0 simultaneously one
by one */
#include<iostream.h>
#include<conio.h>
void
main()
{
int
i,j;
clrscr();
for(i=0,j=9;i<=9,j>=0;i++,j--)
{
cout<<i<<"
" <<j<<"\n";
getch();
}
}
OUTPUT
:
0
9
1
8
2
7
3
6
4
5
5
4
6
3
7
2
8
1
9
0
_____________________http://sundcs.blogspot.com/_________________
/*
Program to display student information using structures */
#include<iostream.h>
#include<conio.h>
struct
student
{
char
name[20];
int
rollno,age;
};
void
main()
{
struct
student s;
clrscr();
cout<<"enter
the name of the student\n";
cin>>s.name;
cout<<"enter
the rollno of the student\n";
cin>>s.rollno;
cout<<"enter
the age of the student\n";
cin>>s.age;
cout<<"The
entered information of the student is\n";
cout<<"Name
is:"<<s.name;
cout<<"\nRoll
No:"<<s.rollno;
cout<<"\nAge
is:"<<s.age;
getch();
}
OUTPUT:
enter
the name of the student
Sunil
enter
the rollno of the student
44
enter
the age of the student
27
The
entered information of the student is
Name
is: Sunil
Roll
No: 44
Age
is: 27
_____________________http://sundcs.blogspot.com/_________________
/*
Program to display student information using functions */
#include<iostream.h>
#include<conio.h>
struct
student
{
char
name[20];
int
rollno,age;
}s;
void
read()
{
cout<<"enter
the name of the student\n";
cin>>s.name;
cout<<"enter
the rollno of the student\n";
cin>>s.rollno;
cout<<"enter
the age of the student\n";
cin>>s.age;
}
void
display()
{
cout<<"The
entered information of the student is\n";
cout<<"Name
is:"<<s.name;
cout<<"\nRoll
No:"<<s.rollno;
cout<<"\nAge
is:"<<s.age;
}
void
main()
{
clrscr();
read();
display();
getch();
}
OUTPUT:
enter
the name of the student
Sunil
Rawat
enter
the rollno of the student
44
enter
the age of the student
27
The
entered information of the student is
Name
is: Sunil Rawat
Roll
No: 44
Age
is: 27
_____________________http://sundcs.blogspot.com/_________________
/*
Program to initialize a few members of n-array of structures
and display the contents of all the structures */
and display the contents of all the structures */
#include<iostream.h>
#include<conio.h>
struct
student
{
char
name[20];
int
rollno,age;
}s[10];
void
read()
{
int
i,n;
cout<<"Enter
the limit\n";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter
the name of the student\n";
cin>>s[i].name;
cout<<"enter
the rollno of the student\n";
cin>>s[i].rollno;
cout<<"enter
the age of the student\n";
cin>>s[i].age;
}
}
void
display()
{
int
i,n;
cout<<"The
entered information of the students are\n";
for(i=0;i<n;i++)
{
cout<<"\nName
is:"<<s[i].name;
cout<<"\nRoll
No:"<<s[i].rollno;
cout<<"\nAge
is:"<<s[i].age;
}
}
void
main()
{
clrscr();
read();
display();
getch();
}
OUTPUT:
Enter
the limit
3
enter
the name of the student
Sumit
enter
the rollno of the student
1
enter
the age of the student
11
enter
the name of the student
Ravi
enter
the rollno of the student
2
enter
the age of the student
10
enter
the name of the student
Sunit
enter
the rollno of the student
3
enter
the age of the student
09
The
entered information of the students are
Name
is: Sumit
Roll
No: 1
Age
is: 11
Name
is: Ravi
Roll
No: 2
Age
is: 10
Name
is: Sunit
Roll
No: 3
Age
is: 09
_____________________http://sundcs.blogspot.com/_________________
/*
Program to read a set of names, roll no, height and weight of a
number of students from keyboard and sort them in
ascending order of names */
number of students from keyboard and sort them in
ascending order of names */
#include<iostream.h>
#include<conio.h>
#include<string.h>
struct
student
{
char
name[20];
int
rollno;
float
height,weight;
}s[3];
void
read()
{
int
i;
for(i=0;i<3;i++)
{
cout<<"enter
the name of the student\n";
cin>>s[i].name;
cout<<"enter
the rollno of the student\n";
cin>>s[i].rollno;
cout<<"enter
the height of the student\n";
cin>>s[i].height;
cout<<"enter
the weight of the student\n";
cin>>s[i].weight;
}
}
void
display()
{
int
i;
cout<<"The
sorted information of the students are\n";
for(i=0;i<3;i++)
{
cout<<"\nName
is:"<<s[i].name;
cout<<"\nRoll
No:"<<s[i].rollno;
cout<<"\nHeight
is:"<<s[i].height;
cout<<"\nWeight
is:"<<s[i].weight;
}}
void
sort()
{
char
temp[20];
int
i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if
(strcmp(s[i].name,s[j].name)<0)
{
strcpy(temp,s[i].name);
strcpy(s[i].name,s[j].name);
strcpy(s[j].name,temp);
}}}}
void
main()
{
clrscr();
read();
sort();
display();
getch();
}
OUTPUT:
enter
the name of the student
Sumit
enter
the rollno of the student
1
enter
the height of the student
5.2
enter
the weight of the student
45
enter
the name of the student
Ravi
enter
the rollno of the student
2
enter
the height of the student
5.3
enter
the weight of the student
47
enter
the name of the student
Sunit
enter
the rollno of the student
3
enter
the height of the student
5.4
enter
the weight of the student
49
The
sorted information of the students are
Name
is:Sunit
Roll
No:3
Height
is:5.4
Weight
is:49
Name
is:Ravi
Roll
No:2
Height
is:5.3
Weight
is:47
Name
is:Sumit
Roll
No:1
Height
is:5.2
Weight
is:45
_____________________http://sundcs.blogspot.com/_________________
/*
Program to read a set of names, roll no, date of birth and date
of joining of an employee from keyboard */
of joining of an employee from keyboard */
#include<iostream.h>
#include<conio.h>
struct
date_of_birth
{
char
month[20];
int
date,year;
};
struct
date_of_joining
{
char
month[20];
int
date,year;
};
struct
student
{
char
name[20];
int
rollno;
struct
date_of_birth dob;
struct
date_of_joining doj;
}s[3];
void
read()
{
int
i;
for(i=0;i<3;i++)
{
cout<<"enter
the name of the student\n";
cin>>s[i].name;
cout<<"enter
the rollno of the student\n";
cin>>s[i].rollno;
cout<<"enter
the date of birth of the student\n";
cout<<"month:";
cin>>s[i].dob.month;
cout<<"date:";
cin>>s[i].dob.date;
cout<<"year:";
cin>>s[i].dob.year;
cout<<"enter
the date of joining of the student\n";
cout<<"month:";
cin>>s[i].doj.month;
cout<<"Date:";
cin>>s[i].doj.date;
cout<<"Year:";
cin>>s[i].doj.year;
}
}
void
display()
{
int
i;
cout<<"The
entered information of the students are\n";
for(i=0;i<3;i++)
{
cout<<"\nName
is:"<<s[i].name;
cout<<"\nRoll
No:"<<s[i].rollno;
cout<<"\nDate
of birth is:";
cout<<s[i].dob.date<<"-"<<s[i].dob.month<<"-"<<s[i].dob.year;
cout<<"\nDate
of joining is:";
cout<<s[i].doj.date<<"-"<<s[i].doj.month<<"-"<<s[i].doj.year;
}
}
void
main()
{
clrscr();
read();
display();
getch();
}
OUTPUT
:
enter
the name of the student
Sumit
enter
the rollno of the student
1
enter
the date of birth of the student
month:JAN
date:25
year:1981
enter
the date of joining of the student
month:JAN
Date:25
Year:2005
enter
the name of the student
Ravi
enter
the rollno of the student
2
enter
the date of birth of the student
month:DEC
date:4
year:1975
enter
the date of joining of the student
month:JULY
Date:23
Year:2009
The
entered information of the students are
Name
is:Sumit
Roll
No:1
Date
of birth is:25-JAN-1981
Date
of joining is:25-JAN-2005
Name
is:Ravi
Roll
No:2
Date
of birth is:4-DEC-1975
Date
of joining is:23-JULY-2009
_____________________http://sundcs.blogspot.com/_________________
/*
Program to print a matrix using functions */
#include<iostream.h>
#include<conio.h>
int
a[5][5],m,n;
void
read()
{
int
i,j;
cout<<"Enter
the number of rows \n";
cin>>m;
cout<<"enter
the number of columns\n";
cin>>n;
cout<<"Enter
the elements of rows and columns\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>a[i][j];
}
}
}
void
display()
{
int
i,j;
cout<<"The
entered matrix is\n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
void
main()
{
clrscr();
read();
display();
getch();
}
OUTPUT
:
Enter
the number of rows
3
enter
the number of columns
2
Enter
the elements of rows and columns
1
2
3
4
5
6
The
entered matrix is
1 2
3 4
5 6
_____________________http://sundcs.blogspot.com/_________________
/*
Program to find the sum of integers as well as floats (concept of
function overloading) */
function overloading) */
#include<iostream.h>
#include<conio.h>
int
sum(int,int);
float
sum(float,float);
void
main()
{
int
a,b;
float
fa,fb;
clrscr();
cout<<"Enter
two integers\n";
cin>>a>>b;
cout<<"Enter
two floats\n";
cin>>fa>>fb;
cout<<"Sum
of two integers"<<"
"<<sum(a,b)<<"\n";
cout<<"Sum
of two float is"<<" "<<sum(fa,fb);
getch();
}
int
sum(int a,int b)
{
return
(a+b);
}
float
sum(float fa,float fb)
{
return
(fa+fb);
}
OUTPUT:
Enter
two integers
3
6
Enter
two floats
3.4
2.3
Sum
of two integers 9
Sum
of two float is 5.7
_____________________http://sundcs.blogspot.com/_________________
/*
Program to read a two-dimensional square matrix and display
its transpose */
its transpose */
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a[2][2],i,j;
clrscr();
cout<<"Enter
the elements of 2*2 matrix\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cin>>a[i][j];
}
}
cout<<"Tranposed
matrix is\n";
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
cout<<a[j][i]<<"\t";
}
cout<<"\n";
}
getch();
}
OUTPUT:
Enter
the elements of 2*2 matrix :
2 3
5 4
Tranposed
matrix is:
2 5
3 4
_____________________http://sundcs.blogspot.com/_________________
/*
Program to read a set of numbers and store it as a 1-D array.
Again read a number n and check whether it is present in
the array. If present, find out its occurrences */
Again read a number n and check whether it is present in
the array. If present, find out its occurrences */
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a[10],i,n;
int
count=0;
int
flag;
clrscr();
cout<<"Enter
ten elements\n";
for(i=0;i<10;i++)
{
cin>>a[i];
}
cout<<"Enter
the element whose occurrence u want to find\n";
cin>>n;
for(i=0;i<10;i++)
{
if
(a[i]==n)
{
flag=1;
count++;
}
}
if(flag==1)
{
cout<<"The
total occurrences of the searched element is"<<"
"<<count;
}
else
cout<<"Element
not found";
getch();
}
OUTPUT:
Enter
ten elements
1
2 3 1 2 2 5 6 2 2
Enter
the element whose occurrence u want to find
2
The
total occurrences of the searched element is 5
_____________________http://sundcs.blogspot.com/_________________
/*
Program to read a set of lines and store them in an array.
Again read a string s and find its occurrence in the array */
Again read a string s and find its occurrence in the array */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void
main()
{
char
str[20][5];
char
s[20];
int
n,i,flag,loc=0;
clrscr();
cout<<"Enter
the limit of the strings\n";
cin>>n;
cout<<"Enter
the strings\n";
for(i=1;i<=n;i++)
{
gets(str[i]);
}
cout<<"Enter
the string to be searched\n";
gets(s);
for(i=1;i<=n;i++)
{
if
(strcmp(s,str[i])==0)
{
flag=1;
loc=i;
}
}
if
(flag==1)
cout<<"The
string is present in the array is at location" <<"
"<<loc;
else
cout<<"String
not found";
getch();
}
OUTPUT:
Enter
the limit of the strings
3
Enter
the strings
I
AM
SUNIL
Enter
the string to be searched
SUNIL
The
string is present in the array is at location 3
_____________________http://sundcs.blogspot.com/_________________
/*
Program to print all possible permutations of 4 digit
numbers */
numbers */
#include
<iostream.h>
#include<conio.h>
class
perm
{
public:
void putdata();
};
void
perm:: putdata()
{
int a,b,c,d;
for(a=1; a<5; a++)
{
for(b=1; b<5; b++)
{
for(c=1; c<5; c++)
{
for(d=1; d<5; d++)
{
if(!(a==b || a==c || a==d || b==c || b==d
|| c==d))
cout<<"\t"<<a<<b<<c<<d;
}
}
}
}
}
void
main()
{
clrscr();
perm
sr;
sr.putdata();
getch();
}
OUTPUT:
1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321
_____________________http://sundcs.blogspot.com/_________________
/* Program to read a set of numbers and store
in 1-D array. Also
check its number of occurrences and print the relevant
check its number of occurrences and print the relevant
Locations */
#include<iostream.h>
#include<conio.h>
void
main()
{
int
a[10],i,n;
int
count=0;
int
loc=0;
int
flag;
clrscr();
cout<<"Enter
ten elements\n";
for(i=1;i<=10;i++)
{
cin>>a[i];
}
cout<<"Enter
the element whose occurrence u want to find\n";
cin>>n;
for(i=1;i<=10;i++)
{
if
(a[i]==n)
{
flag=1;
count++;
loc=i;
cout<<"\nlocation
of the searched element is"<<" "<<loc;
}
}
if(flag==1)
{
cout<<"\nThe
total occurrences of the searched element is"<<"
"<<count;
}
else
cout<<"Element
not found";
getch();
}
OUTPUT:
Enter
ten elements
1
2 4 6 2 7 2 5 2 8
Enter
the element whose occurrence u want to find
2
location
of the searched element is 2
location
of the searched element is 5
location
of the searched element is 7
location
of the searched element is 9
The
total occurrences of the searched element is 4
_____________________http://sundcs.blogspot.com/_________________
/*
Program to find the sum of series:
x-x^3/3!+x^5/5!..........x^n/n! */
x-x^3/3!+x^5/5!..........x^n/n! */
#
include<iostream.h>
#include<conio.h>
#include<math.h>
class series
{
float x,sum,t;
int n,i;
public:
void getdata();
void putdata();
};
void
series:: getdata()
{ cout<<"\n Enter n, x values :
";
cin>>n>>x;
x=x*(3.1412/180);
t=x;
sum=x;
for(i=1;i<=n;i++)
{t=pow(t,-1)*(2*i-1)*x*x/(t*i*(t*i-1));
sum=sum+t;
}
}
void
series:: putdata()
{ cout<<"\n Result= "<<sum;
}
void
main()
{
clrscr();
series
sr;
sr.getdata();
sr.putdata();
getch();
}
OUTPUT:
Enter
n, x values : 2 3
Result= -1.004079
_____________________http://sundcs.blogspot.com/_________________
/*
Program to Print the following series:
0
101
21012
3210123
432101234
54321012345
*/
#include<iostream.h>
#include<conio.h>
class series
{ public:
void putdata();
};
void
series:: putdata()
{ int i,j,c=9,m,k;
for(i=0;i<=5;i++)
{ for(k=5;k>=i;k--)
{ cout<<" ";
} for(j=i;j>=1;j--)
{ cout<<j;
}
for(m=0;m<=i;m++)
{ cout<<m;
}
cout<<"\n";
c=c-2;
}
}
void
main()
{
clrscr();
series
sr;
sr.putdata();
getch();
}
OUTPUT
:
0
101
21012
3210123
432101234
54321012345
_____________________http://sundcs.blogspot.com/_________________
/*
Program to read a line from keyboard using for loop */
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void
main()
{
int
i,len;
char
string[20];
clrscr();
cout<<"enter
the line\n";
gets(string);
len=strlen(string);
for(i=0;i<len;i++)
{
cout<<string[i];
getch();
}
}
OUTPUT:
enter
the line
My
Name is Sunil
My
Name is Sunil
_____________________http://sundcs.blogspot.com/_________________
/*
Program to display name of the day in a week using switch
statement */
statement */
#include<iostream.h>
#include<conio.h>
void
main()
{
int
i;
clrscr();
cout<<"Enter
your choice\n";
cin>>i;
switch(i)
{
case
1:
cout<<"Sunday";
break;
case
2:
cout<<"monday";
break;
case
3:
cout<<"tuesday";
break;
case
4:
cout<<"Wednesday";
break;
case
5:
cout<<"thursday";
break;
case
6:
cout<<"Friday";
break;
case
7:
|
break;
default:
cout<<"Wrong
choice";
}
getch();
}
OUTPUT:
Enter
your choice
1
Sunday
_____________________http://sundcs.blogspot.com/_________________
/*
Program to display the name of day in a week using if-else
statement */
statement */
#include<iostream.h>
#include<conio.h>
void
main()
{
int
i;
clrscr();
cout<<"Enter
your choice\n";
cin>>i;
if
(i==1)
cout<<"Sunday";
else
if (i==2)
cout<<"monday";
else
if(i==3)
cout<<"tuesday";
else
if(i==4)
cout<<"Wednesday";
else
if(i==5)
cout<<"thursday";
else
if(i==6)
cout<<"Friday";
else
if(i==7)
cout<<"Saturday";
else
cout<<"Wrong
choice";
getch();
}
OUTPUT:
Enter
your choice
2
monday