Вы находитесь на странице: 1из 46

1). To write a program to find Area and Perimeter of a Circle.

#include<stdio.h>
#include<conio.h>
void main()
{
int r, area, perimeter;
clrscr();
printf("enter radius value\n");
scanf("%d",&r);
area=3.14*r*r;
perimeter=2*3.14*r;
printf("area of the given circle %d\n", area);
printf("perimeter of the circle is: %d\n" , perimeter);
getch();
}

Output:-
Enter radius vaule
10
Area of the given circle 314
Perimeter of the circle is: 62

1
2). To write a program enter student no, name, and marks in
subjects and calculate sum, avg, and find out the result.

#include<stdio.h>
#include<conio.h>
void main()
{
int no,sum,avg;
char name[10];
int m1,m2,m3;
clrscr();
printf("enter the student no\n");
scanf("%d",&no);
printf("enter the student name\n");
scanf("%s",name);
printf("enter the marks in 3 subjects\n");
scanf("%d%d%d",&m1,&m2,&m3);
printf("stdent no ,is %d\n", no);
printf("student name is %s\n" ,name);
printf("student marks are %d %d %d",m1,m2,m3);
getch();
}

Output:-
Enter the student no
104
Enter the student name
xyz
Enter the marks in 3 subjects
75
75
75
Student no is 104
Student name is xyz
Student marks are 75 75 75

2
3). Write a program to display name of the day depending upon the
no entered from the key board using switchcase.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("enter any charcter: ");
scanf("%c",&ch);
switch(ch)
{
case '1':
printf("today is sunday");
break;
case '2':
printf("today is monday");
break;
case '3':
printf("today is tuesday");
break;
case '4':
printf("today is wednesday");
break;
case '5':
printf("today is thursday");
break;
case '6':
printf("today is friday");
break;
case '7':
printf("today is saturday");
break;
default:
printf("april fool");
}
getch();
}

Output:-
Enter any character
5
Today is thursday

3
4). Write a program to check whether the given no is prime or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int k=0,no,i;
clrscr();
printf("enter any no: ");
scanf("%d",&no);
for(i=0;i<no;i++)
{
if(no%i==0)
k++;
}
if(k==2)
{
printf("it is prime\n");
}
else
printf("not prime");
getch();
}

Output:-
Enter any no: 5
It is prime

4
5). Write a program to check whether the given no is Am strong or
not.

#include<stdio.h>
#include<conio.h>
void main()
{
int no,k,r,sum=0;
clrscr();
printf("enter any no\n");
scanf("%d",&no);
k=no;
while(no>0)
{
r=no%10;
sum=sum+(r*r*r);
no=no/10;
}
if(k==sum)
printf("it is amstrong no");
else
printf("not amstrong no");
getch();
}

Output:-
Enter any no
153
It is am strong no

5
6). Write a program to print the below output.
12345
2345
345
45
5

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=5;i++)
{
for(k=0;k<i;k++)
printf(" ");
for(j=i;j<=5;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

Output:-
12345
2345
345
45
5

6
7). Write a program to find out the matrix addition.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("enter the array elements for A\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("enter the array elements for B\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf("addtion array is:\n",c[i][j]);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%3d",c[i][j]);
printf("\n");
}
getch();
}

Output:-
Enter array elements for A
100
010
001
Enter array elements for B
100
010
001
Addition array is
200
020
002

7
8). Write a program to find out the matrix multiplication.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q,k;
clrscr();
printf("enter the rows and columns for A:");
scanf("%d%d",&m,&n);
printf("enter the rows and columns for B:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("enter the elements for A:\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("enter the elements for B:\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<p;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("matrix multiplication is:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%3d",c[i][j]);
printf(" \n");
}
}
else
printf("multiplication is not possible");
getch();

8
Output:-
Enter the rows and columns for A
33
Enter the rows and columns for B
Enter the elements for A
100
010
001
Enter the elements for B
100
010
001
Matirx multiplication is
100
010
001

9
9). Write a program to swap 2 nos by using call by value

#include<stdio.h>
#include<conio.h>
void swap(int x,int y);
void main()
{
int a,b;
clrscr();
printf("enter a,b values:\n");
scanf("%d%d",&a,&b);
printf("before swapping a,b values are\n%d %d\n",a,b);
swap(a,b);
getch();
}
void swap(int x ,int y)
{
int z;
z=x;
x=y;
y=z;
printf("after swapping a,b values are\n%d %d",x,y);
}

Output:-
Enter a ,b values 10 20
Before swapping a ,b values are 10 20
After swapping a ,b values are 20 10

10
10).Write a program to find out the factorial of the given no. by using recursion .

#include<stdio.h>
#include<conio.h>

int fact(int x);


void main()
{
int x,k;
clrscr();
printf("enter any no: ");
scanf("%d",&x);
k=fact(x);
printf("factorial of the given no is %d",k);
getch();
}
int fact(int x)
{
if(x==0)
return 1;
else
return x*fact(x-1);
}

Output:-
Enter any no 5
Factorial of the given no is 120

11
11). Write a program to implement the call by reference mechanism.

#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
int a,b;
clrscr();
printf("enter a,b values\n");
scanf("%d%d",&a,&b);
printf("before swapping a,b values %d %d\n ",a,b);
swap(&a,&b);
getch();
}
void swap(int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
printf("\nafter swapping a,b values are: %d %d",*x,*y);
}

Output:-
Enter a ,b values are 10 20
Before swapping a,b values are 10 20
After swapping a,b values are 20 10

12
12).Write a program to implement all string functions.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int l,k;
char s1[20]="good morning";
char s2[10]="HYDERABAD";
clrscr();
strcpy(s2,s1);
printf("copied string is %s\n",s2);
k=strlen(s2);
printf("string length is %d\n",k);
strupr(s1);
printf("string in uppercase %s\n",s1);
strlwr(s2);
printf("string in lowercase %s\n",s2);
strcat(s2,s1);
printf("concatinated string is %s\n",s2);
strrev(s1);
printf("reversed string is %s\n",s1);
l=strcmp(s1,s2);
if(l==0)
printf("two strings are equal");
else
printf("two strings are not equal");
getch();
}

Output:-
Copied string is good morning
String length is 12
String in uppercase GOOD MORNING
String in lower case good morning
Concatenated string is good morning GOOD MORNING
Reversed string is GNINROM DOOG
Two strings are not equal

13
13).Write a program to implement linear sort mechanism.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,k;
clrscr();
printf("enter array elements");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
printf("sorted elements are\n");
for(i=0;i<10;i++)
printf("%d\n",a[i]);
}

Output:-
Enter array elements 5 6 8 7 9 4 2 3 1 10
Sorted elements are 1 2 3 4 5 6 7 8 9 10

14
14). Write a program to implement linear search mechanism.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,k=1,pos;
clrscr();
printf("enter array elements\n");
for(i=1;i<=10;i++)
scanf("%d",&a[i]);
printf("enter element for search");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
if(a[i]==n)
{
k=1;
pos=i;
break;
}
}
if(k==1)
printf("search is completed %d",pos);
else
printf("element not exist");
getch();
}

Output:-
Enter array elements 1 8 5 4 6 9 7 3 2 10
Enter element for search 11
Element not exist

15
15). Write a program to implement binary search mechanism.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5]={10,20,30,40,50},i,k=1,n,top=0,mid,bott,pos;
clrscr();
printf("enter element for search\n");
scanf("%d",&n);
bott=n-1;
while(top<=bott)
{
mid=(top+bott)/2;
if(n>a[i])
top=mid+1;
else if(n<a[i])
top=mid-1;
else if(n==a[i])
{
k=1;
pos=a[i];
}
}
if(k==1)
printf("search is success%d ",pos);
else
printf("searching is unsuccess");
getch();
}

Output:-
Enter element for search 2
Searching is un success

16
16).Write a program to implement bubble sort mechanism.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,temp;
clrscr();
printf("enter array elements:\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
for(i=0;i<10;i++)
for(j=0;j<10-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
printf("sorted elements are :");
for(i=0;i<10;i++)
printf("%d\n",a[i]);
getch();
}

Output:-
Enter array elements 8 5 7 4 9 6 3 2 110
Sorted elements are : 1 2 3 4 5 6 7 8 9 10

17
17).Write a program to find out Student avg of marks by using
structures.

#include<stdio.h>
#include<conio.h>
struct std
{
int no;
char name[10];
int m1,m2,m3;
int total,avg;
};
void main()
{
struct std s;
clrscr();
printf("enter student number\n");
scanf("%d",&s.no);
printf("enter student name\n");
scanf("%s",s.name);
printf("enter student marks in 3 subjects\n");
scanf("%d%d%d",&s.m1,&s.m2,&s.m3);
s.total=(s.m1+s.m2+s.m3);
s.avg=s.total/3;
printf("student no=%d\n",s.no);
printf("student name=%s\n",s.name);
printf("stdent avg marks=%d",s.avg);
getch();
}

Output:-
Enter student number 007
Enter student name bond
Enter student marks in 3 subjects 75 75 75
Student no 007
Student name bond
Student avg marks 75

18
18).WRITE A PROGRAM TO IMPLEMENT THE
SEQUENTIAL FILE ACCESS

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>

int main()
{
char st[8];
ifstream in_file ("getline dat",ios::in);
If(!in_file)
{
cout<<"Error: file not open";
exit(1);
}
while(in_file)
{
in_file.getline(st,8);
if(in_file)
cout<<st<<endl;
}
in_file.close();
return 0;
}

Out Put:-

Error: file not open

19
19). WRITE A PROGRAM TO COPY A FILE FROM
ANOTHER.

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>

int main()
{
char st[8];
ifstream in_file ("getline dat",ios::in);
If(!in_file)
{
cout<<"Error: file not open";
exit(1);
}
while(in_file)
{
in_file.getline(st,8);
if(in_file)
cout<<st<<endl;
}
in_file.close();
return 0;
}

Out Put:-

20
20).Write a program to enter std no , name , 3 subjects marks and find
sum , avg, and display the grade by using classes.

#include<iostream.h>
#include<conio.h>
class std
{
private:
int no;
char name[10];
int m1,m2,m3;
int sum,avg,k;
public:
void getdata()
{
cout<<"enter student no";
cin>>no;
cout<<"entet student name";
cin>>name;
cout<<"enter 3 subjects";
cin>>m1>>m2>>m3;
}
void display()
{
cout<<"student no="<<no<<endl;
cout<<"student name="<<name<<endl;
sum=m1+m2+m3;
avg=sum/3;
k=(sum/300)*100;
cout<<"sum of the marks ="<<sum<<endl;
cout<<"avg of the student="<<avg<<endl;
cout<<"grade of the student" <<endl;
if(k>=75)
cout<<"I st division";
else if(60<=k<75)
cout<<"2 nd division";
else
cout<<"3 rd division";
}
};
void main()
{
std s;
clrscr();
s.getdata();
s.display();
getch();

21
}

Output:-
Enter student no
007
Enter student name bond
Enter 3 subject marks 100
100
100
Student no 007
Student name bond
Sum of the marks 300
Avg of the marks 300
Grade of the student
I st division

22
21).Write a program to implement inline function.

#include<iostream.h>
#include<conio.h>
inline int sum(int x,int y)
{
return x+y;
}
inline int mul(int x,int y)
{
return x*y;
}
void main()
{
cout<<sum(10,20)<<endl;
cout<<mul(5,2);
getch();
}

Output:-
30
10

23
22).write a program to demonstrate function overloading concept.

#include<iostream.h>
#include<conio.h>
float area(int a)
{
return (3.14*a*a);
}
int sum(int l,int k)
{
return (l+k);
}
void main()
{
clrscr();
cout<<area(10)<<endl;
cout<<sum(20,30);
getch();
}

Output:-
314
50

24
23).Write a program to implement constructor and destructor

#include<iostream.h>
#include<conio.h>
class item
{
int s;
public:
item(int a)
{
s=a;
s++;
}
~item()
{
s--;
}
void show()
{
cout<<s;
}

};
void main()
{
item i(100);
clrscr();
i.show();
getch();
}

Output:-
101

25
24).Write a program to implement operator overloading concept

#include<iostream.h>
#include<conio.h>
class sample
{
private:
int a;
int b;
public:
void getdata()
{
cout<<"enter a,b values";
cin>>a>>b;
}
void display()
{
cout<<"a value="<<a<<endl;
cout<<"b value="<<b;
}
void operator - ()
{
a=-a;
b=-b;
}
};
void main()
{
sample obj;
clrscr();
obj.getdata();
- obj;
obj.display();
getch();
}

Output:-
Enter a , b values
10
20
A value = -10
B value = -20

25). Write a program to demonstrate static functions , static data


members

26
#include<iostream.h>
#include<conio.h>
class test
{
private:
int code;
static int count;
public:
void getdata()
{
code=count+1;
}
void show()
{
cout<<"object number:"<<code<<endl;
}
static void show1()
{
cout<<"count is:"<<count<<endl;
}
};
int test::count;
void main()
{
test t1,t2;
clrscr();
t1.getdata();
t2.getdata();
test::show1();
test t3;
t3.show1();
test::show1();
t1.show();
t2.show();
getch();
}

Output:-
Count is : 0
Count is : 0
Count is : 0
Object number: 1
Object number: 1

27
26).Write a program to check which is maximum using friend
function.

#include<iostream.h>
#include<conio.h>
class sample
{
private:
int a;
int b;
public:
void getdata()
{
cout<<"enter a,b values";
cin>>a>>b;
}
friend void max(sample s);
};
void max(sample s)
{
if(s.a>s.b)
cout<<" a is maximum: "<<s.a;

else
cout<<"b is maximum"<<s.b;

}
void main()
{
sample s;
clrscr();
s.getdata();
max(s);
getch();
}

Output:-
Enter a , b values
20
30
B is maximum 30

28
27).Write a program to access the std data by using inheritance

#include<iostream.h>
#include<conio.h>
class person
{
private:
char petname[10];
public:
void getdata();
void putdata();
};
class student:public person
{
private:
int rno;
char name[10];
int rank;
public:
void getdata();
void putdata();
};
void person::getdata()
{
cout<<"enter person pet name";
cin>>petname;
}
void person::putdata()
{
cout<<"person petname is:"<<petname<<endl;
}
void student::getdata()
{
person::getdata();
cout<<"enter student rollno";
cin>>rno;
cout<<"enter student name";
cin>>name;
cout<<"enter student rank";
cin>>rank;
}
void student::putdata()
{
person::putdata();
cout<<"student rollno is:"<<rno<<endl;
cout<<"student name is:"<<name<<endl;
cout<<"student rank is:"<<rank;
}

29
void main()
{
clrscr();
student s;
s.getdata();
s.putdata();
getch();
}

Output:-
Enter person pet name chitti
Enter student roll no 100
Enter student name robo
Enter student rank
143
Person petname is: chitti
Student roll no 100
Student name robo
Student rank 143

30
28).Write a program to access the std data by using multiple
inheritance.

#include<iostream.h>
#include<conio.h>
class student
{
private:
int Hno ;
public:
void getdata();
void putdata();
};
class test
{
private:
int m1,m2;
public:
void getdata();
void putdata();
};
class result:public student,public test
{
private:
int total;
public:
void getdata();
void putdata();
};
void student::getdata()
{
cout<<"enter hallticketno \n";
cin>>Hno;
}
void student::putdata()
{
cout<<"Hall ticket no is:\n"<<Hno<<endl;
}
void test::getdata()
{
cout<<"enter marks in 2 subjects\n";
cin>>m1>>m2;
}
void test::putdata()
{
cout<<"2 subject marks are"<<m1<<m2;

31
void result::getdata()
{
student::getdata();
test::getdata();
cout<<"enter total \n";
cin>>total;
}
void result::putdata()
{
student::putdata();
test::putdata();
cout<<"\ntotal is:"<<total;
}
void main()
{
clrscr();
result r;
r.getdata();
r.putdata();
getch();
}

Output:-
Enter hall ticket no 143
Enter marks in 3 subjects 100 100
Hall ticket no is: 143
2 subjects marks are 100 100
Total is 200

32
29).Write a program to demonstrate multilevel inheritance

#include<iostream.h>
#include<conio.h>
class publication
{
private:
char title[10];
public:
void getdata();
void putdata();
};
class sales:public publication
{
private:
int msales;
public:
void getdata();
void putdata();
};
class book:public sales
{
private:
int nopages;
public:
void getdata();
void putdata();
};
void publication::getdata()
{
cout<<"enter title\n";
cin>>title;
}
void publication::putdata()
{
cout<<"Title is:\n"<<title<<endl;
}
void sales::getdata()
{
publication::getdata();
cout<<"enter monythly sales\n";
cin>>msales;
}
void sales::putdata()
{
publication::putdata();
cout<<"monthly sales are"<<msales;

33
}
void book::getdata()
{
sales::getdata();
cout<<"enter total pages\n";
cin>>nopages;
}
void book::putdata()
{
sales::putdata();
cout<<"\ntotal pages are:"<<nopages;
}
void main()
{
clrscr();
book b;
b.getdata();
b.putdata();
getch();
}

Output:-
Enter title TITLE
Enter monthly sales 1
Enter total pages 1
Title is TITLE
Monthly sales are 1
Total pages are 1

34
30).Write a program to demonstrate hierarchical inheritance

#include<iostream.h>
#include<conio.h>
class vehical
{
private:
char name[10];
public:
void getdata();
void display();
};
class lightmotor:public vehical
{
private:
int speedlimit;
public:
void getdata();
void display();
};
class heavymotor:public vehical
{
private:
char permit[20];
public:
void getdata();
void display();
};
class gearmotor: public lightmotor
{
private:
int ngears;
public:
void getdata();
void display();
};
class nongearmotor:public lightmotor
{
private:
int capacity;
public:
void getdata();
void display();
};
class passenger:public heavymotor
{
private:
int npassengers;

35
public:
void getdata();
void display();
};
class goods: public heavymotor
{
private:
int load;
public:
void getdata();
void display();
};
void vehical::getdata()
{
cout<<"enter vehical name\n";
cin>>name;
}
void vehical::display()
{
cout<<"vehical name is:\n"<<name<<endl;

}
void lightmotor::getdata()
{
vehical::getdata();
cout<<"enter speed limit\n";
cin>>speedlimit;
}
void lightmotor::display()
{
vehical::display();
cout<<"speed limit is:\n"<<speedlimit<<endl;
}
void heavymotor::getdata()
{
cout<<"enter permit\n";
cin>>permit;
}
void heavymotor::display()
{
cout<<"permit is:\n"<<permit<<endl;
}
void gearmotor::getdata()
{
lightmotor::getdata();
cout<<"enter no of gears\n";
cin>>ngears;
}

36
void gearmotor::display()
{
lightmotor::display();
cout<<"no of gears are:\n"<<ngears<<endl;
}
void nongearmotor::getdata()
{
cout<<"enter engine capacity\n";
cin>>capacity;
}
void nongearmotor::display()
{
cout<<"capacity is:\n"<<capacity<<endl;
}
void passenger::getdata()
{
heavymotor::getdata();
cout<<"enter no of passengers\n";
cin>>npassengers;
}
void passenger::display()
{
heavymotor::display();
cout<<"no of passengers :\n"<<npassengers<<endl;
}
void goods::getdata()
{
cout<<"enter loadcapacity\n";
cin>>load;
}
void goods::display()
{
cout<<"load capacity:\n"<<load<<endl;
}
void main()
{
gearmotor g;
nongearmotor n;
passenger p;
goods gd;
clrscr();
cout<<"enter gearmotor info:\n";
g.getdata();
cout<<"enter nongearmotor info :\n";
n.getdata();
cout<<"enter passenger info :\n";
p.getdata();
cout<<"enter goods info:\n";

37
gd.getdata();
cout<<"gearmotor information is:\n"<<endl;
g.display();
cout<<"nongearmotor information is:\n"<<endl;
n.display();
cout<<"passenger information is:\n"<<endl;
p.display();
cout<<"goods information is:\n"<<endl;
gd.display();
getch();
}

Output:-
Enter gearmotor info:
Enter vehical name herohonda
Enter speed limit 80
Enter no of gears 4
Enter nongear motor info:
Enter engine capacity 100
Enter passenger info :
Enter permit ap
Enter no of passengers 4
Enter goods info:
Enter loadcapacity 250

Gear motor information is:


vehical name is: herohonda
Speed limit is: 80

Non gear motor information is:


Capacity is 100

Passenger information is:


Permit is ap
No of passengers : 2

Goods information is:


Load capacity 250

38
31).Write a program to demonstrate virtual function

#include<iostream.h>
#include<conio.h>
class A
{
public:
virtual void display()
{
cout<<"HELLO"<<endl;
}
};
class B:public A
{
public:
void display()
{
cout<<"HAVE A NICE DAY\n";
}
};
void main()
{
A *p1,*p2;
clrscr();
p1=new A();
p1->display();
p2=new B();
p2->display();
getch();
}

Output:-
HELLO
HAVE A NICE DAY

39
32).Write a program to implement abstract classes

include<iostream.h>
#include<conio.h>
class A
{
virtual void display()= 0;
};
class B:public A
{
void display()
{
cout<<"hello";
}
};
void main()
{

clrscr();
B b;
getch();
}

Output:-

40
33).Write a program on function templates

#include<iostream.h>
#include<conio.h>
template <class t>
void sum(t x,t y)
{
t z;
z=x+y;
cout<<"z value is :"<<z;
}
void main()
{
int a=10,b=20;
clrscr();
sum(a,b);
cout<<endl;
float m=5.5,n=2.5;
sum(m,n);

Output:-
Z value is : 30
Z value is : 8

41
34).Write a program to implement class templates

#include<iostream.h>
#include<conio.h>
template <class t>
class A
{
private:
t a;
t b;
public:
void getdata()
{
cout<<"enter a,b values";
cin>>a>>b;
}
void display()
{
cout<<"a value is:"<<a<<endl;
cout<<"b value is:"<<b;
}
};
void main()
{
A <int> a1;
A <float> a2;
clrscr();
a1.getdata();
a1.display();
getch();
}

Output:-
Enter a , b values 20 30
A value is : 20
B value is : 30

42
35).Write a program to demonstrate arrays in structure.

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
struct std
{
int no;
char name[50];
float marks;
}s;
void main()
{
struct std s[5];
int i;
clrscr();
cout<<"enter student records\n";
for(i=0;i<5;i++)
{
cout<<"enter student no :";
cin>>s[i].no;
cout<<"enter student name :";
cin>>s[i].name;
cout<<"enter student marks :";
cin>>
s[i].marks;
}
cout<<"no\t";
cout<<"name\t";
cout<<"marks";
for(i=0;i<5;i++)
{
cout<<endl;
cout<<s[i].no<<setw(10);
cout<<s[i].name<<setw(10);
cout<<s[i].marks;
}
getch();
}

Output:-
Enter student records
Enter student no : 1
Enter student name : a
Enter student marks 100
Enter student no: 2
Enter student name : b

43
Enter student marks 100
Enter student no :3
Enter student name c
Enter student marks 100
Enter student no :4
Enter student name : d
Enter student marks 100
Enter student no : 5
Enter student name : e
Enter student marks 100

44
36). Write a program to implement pointer to pointer concept

#include<iostream.h>
#include<conio.h>
void main()
{
int x;
int *p1,**p2;
clrscr();
cout<<"enter x value";
cin>>x;
p1=&x;
p2=&p1;
cout<<*p1<<endl;
cout<<**p2;
getch();
}

Output:-
Enter x value 20
20
20

45
37).Write a program to implement arrays and pointers concept

#include<iostream.h>
#include<conio.h>
void main()
{
int a[5],*p,i;
clrscr();
cout<<"enter array elements";
for(i=0;i<5;i++)
cin>>a[5];
cout<<"array elements are..\n";
p=&a[0];
for(i=0;i<5;i++)
{
cout<<*p<<endl;
p++;
}
getch();
}

Output:-
Enter array elements
10 20 30 40 50
Array elements are..10 20 30 40 50

46

Вам также может понравиться