Sie sind auf Seite 1von 64

DTU

C PROGRAMMING LAB






Name : MUKUL SEHRAWAT
Roll No. : DTU/2K13/B01/0147
Group : B01









INDEX
DTU/2K13/B01/0147
SNO. PROGRAM DATE SIGN
1) Program to find the simple interest.

2) Program to find the sum and average of 10
numbers.

3) Program to find greatest of 10 numbers.

4) Program to check whether entered number is
prime or not.

5) Program to implement switch-case statement.

6) Program for Fibonacci series.

7) Program to find the sum of digits of a 5-digit
number.

8) Program to reverse a 5-digit number.

9) Program to find area of circle, rectangle, square,
triangle using functions.

10) Program to find factorial using recursion.

11) Program to find number of vowels in the entered
string.


12) Program to check whether the entered string is a
palindrome or not, using pointers.


13) Program to convert uppercase to lowercase and
vice-versa.


14) Program to convert binary number to decimal
number and vice-versa.


15) Program to sort an array using bubble sort

16) Program to print:
1
12
123
1234
12345


17) Program for addition of two 3*3 matrices.

18) Program to multiply two 3*3 matrices.

19) Program to search a number from an array.

20) Program to generate employee details using
structures.

21) Program to sort an array using selection sort

22) Program to pass and return pointer to a function

23) Program to pass an array call() as pointer to calculate
the sum of all elements of array

24) Program to read a file and after converting all lower
case to uppercase and write to another file

25) Program to find the length of a string without using

DTU/2K13/B01/0147


































string function slen() and pass the string of
characters
26) Program to implement constructor and destructor

27) Program to overload operator +

28) Program to find the area of different shapes using
function overloading

29) Program to implement multiple inheritance

30) Define a class right angled triangle and determine its
area and perimeter through two separate functions

DTU/2K13/B01/0147
PROGRAM NO.-1 prg1.c

Programe to find simple interest


#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
floatpr,rt,smp,t;
printf("\nEnter PRINCIPLE AMOUNT:\n");
scanf("%f",&pr);
printf("\nEnter RATE OF INTEREST:\n");
scanf("%f",&rt);
printf("\nEnter TIME PERIOD:\n");
scanf("%f",&t);
smp=(pr*rt*t)/100;
printf("\nSIMPLE INTEREST IS:\n%f",smp);
getch();
}
























DTU/2K13/B01/0147
OUTPUT































DTU/2K13/B01/0147
PROGRAM NO 2 prg2.c

Program to find sum and average of 10 numbers

#include<stdio.h>
#include<conio.h>
void main()
{
float list[10],sum=0,avg;
int i;
clrscr();
printf("\nEnter ten numbers\n");
for(i=0;i<10;i++)
{
scanf("%f",&list[i]);
sum=sum+list[i];
}
avg=sum/10.0;
printf("\nSUM of the entered numbers is %f\n",sum);
printf("\nAVERAGE of the entered number is %f\n",avg);
getch();
}



























DTU/2K13/B01/0147
OUTPUT




























DTU/2K13/B01/0147
PROGRAM NO 3 prg3.c

Program to find greatest of the 10 numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int num,large;
clrscr();
printf("Enter ten Numbers\n");
for(int i=0;i<10;i++)
{
scanf("%d",&num);
if(i==0)
large=num;
else if(num>large)
large=num;
}
printf("The greatest number entered is : %d",large);
getch();
}



















DTU/2K13/B01/0147
OUTPUT






























DTU/2K13/B01/0147
PROGRAM NO 4 prg4.c

Program to check whether entered number is prime or not.

#include<stdio.h>
#include<conio.h>
#include<math.h>
int check_prime(int num)
{
float square_root;
int i; square_root=sqrt(num);
if (num==1)
return 2;
for(i=2;i<=square_root;i++)
{ if(num%i==0)
return 0;
}return 1; }
int main()
{ int d,num;
clrscr(); printf("Enter the number to be checked except 0\n");
scanf("%d",&num);
d=check_prime(num);
if(d==1)
printf("This is a prime number");
else if(d==2) printf("It's coprime");
else printf("This is a composite number");
getch();
return 0; }


















DTU/2K13/B01/0147

OUTPUT




























DTU/2K13/B01/0147

PROGRAM NO 5 prg5.c

Program to implement switch case statement.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c; int ch; clrscr();
printf("Enter the 1 number \n");
scanf("%d",&a);
printf("Enter the 2 number \n");
scanf("%d",&b);
printf("Enter the choice of your arithmetic operation \n");
printf("1.Addition \n2.Subtraction \n3.Multiplication \n4.Division\n");
scanf("%d",&ch);
switch(ch)
{ case 'A' : c=a+b;
printf("The sum is %d",c); break;
case 'B': c=a-b;
printf("The difference is %d",c); break;
case 'C' : c=a*b;
printf("The multiplication is %d",c); break;
case 'D' : c=a/b;
printf("The division is %d",c); break;
default: printf("You entered a wrong choice");
}
getch(); }

















DTU/2K13/B01/0147

OUTPUT





























DTU/2K13/B01/0147

PROGRAM NO 6 prg6.c

Program for Fibonacci Series.
#include<stdio.h>
#include<conio.h>
void main()
{
int first,second,third,n,i;
first=0;
second=1;
clrscr();
printf("\nHow many elements:\t");
scanf("%d",&n);
printf(" %d %d ",first,second);
for(i=2;i<n;++i)
{
third=first+second;
printf(" %d ",third);
first=second;
second=third;
}
getch();
}






















DTU/2K13/B01/0147

OUTPUT






























DTU/2K13/B01/0147

PROGRAM NO 7 prg7.c

Program to find sum of digits of a 5 digit number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
int x,sum;
sum=0;
clrscr();

printf("\nEnter a 5-digit no.:\t");
scanf("%d",&n);
while(n>0)
{
x=n%10;
sum=sum+x;
n=n/10;
}
printf("\nSum of the digits of the 5-digit no. is:%d\t",sum);
getch();
}





















DTU/2K13/B01/0147

OUTPUT





























DTU/2K13/B01/0147

PROGRAM NO 8 prg8.c

Program to reverse the digits of a 5 digit number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
int m,rev;
rev=0;
clrscr();

printf("\nEnter a 5-digit no.:\t");
scanf("%d",&n);
while(n>0)
{
m=n%10;
rev=rev*10+m;
n=n/10;
}
printf("\nReverse of 5-digit no. is:%d\t",rev);
getch();
}




















DTU/2K13/B01/0147
OUTPUT




















+






DTU/2K13/B01/0147
PROGRAM NO 9 prg9.c


Program to find area,perimeter of circle square rectangle and triangle.

#include<stdio.h>
#include<conio.h>
#include<math.h>
float r,c_a,c_p,l,b,r_a,r_p,x,s_a,s_p,a,b,c,s,t_a,t_p;
void circle();
void rectangle();
void square();
void triangle();
void main()
{
clrscr();
circle();
rectangle();
square();
triangle();
getch();
}
void circle()
{
printf("\nEnter RADIUS of circle:\t");
scanf("%f",&r);
c_a=(22.0/7.0)*r*r;
c_p=2*(22.0/7.0)*r;
printf("\nCircle AREA and PERIMNETER respectively (in sq. units and units)\n
%f\t%f",c_a,c_p);
}
void square()
{
printf("\nEnter SIDE of square:\t");
scanf("%f",&x);
s_a=x*x;
s_p=4.0*x;
printf("\nSquare AREA and PERIMETER respectively (in sq. units and units)\n
%f\t%f",s_a,s_p);
}
void rectangle()
{
printf("\nEnter LENGTH and BREADTH of rectangle:\t");
scanf("%f \t %f",&l,&b);
r_a=l*b;
r_p=2*(l+b);
printf("\nRectangle AREA and PERIMETER respectively (in sq. units and units)\n
%f\t%f",r_a,r_p);
}
DTU/2K13/B01/0147
void triangle()
{
printf("\nEnter SIDES of triangle:\t");
scanf("%f %f %f",&a,&b,&c);
t_p=a+b+c;
s=t_p/2.0;
t_a=sqrt(s*(s-a)*(s-b)*(s-c));
printf("\nSquare AREA and PERIMETER respectively (in sq. units and units)\n
%f\t%f",t_a,t_p); }



































DTU/2K13/B01/0147


OUTPUT

























DTU/2K13/B01/0147


PROGRAM NO 10 prg10.c

10. Program for factorial using recursion.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n Enter a number :");
scanf("%d",&n);
printf("\n Factorial value=%d",fact(n));
getch();
}
int fact(int k)
{
if(k==0)
return 1;
else
return k*fact(k-1);
}























DTU/2K13/B01/0147



OUTPUT

























DTU/2K13/B01/0147



PROGRAM NO 11 prg11.c

Program to find number of vowels in a string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{int num,n,i;
char ch[50];
num=0;i=0;
printf("Enter the string in which you want to find no of vowels \n");
gets(ch);
n=strlen(ch);
for(i=0;i<=n;i++)
{
if(ch[i]=='a'||ch[i]=='e'||ch[i]=='i' ||ch[i]=='o'||ch[i]=='u'||ch[i]=='A'||ch[i]=='E'||ch[i]=='I'
||ch[i]=='O'||ch[i]=='U')
{
num++;
}
}
printf("Number of vowels is %d",num);
getch();
return 0;
}

















DTU/2K13/B01/0147




OUTPUT


























DTU/2K13/B01/0147


PROGRAM NO 12 prg12.c


Program to check palindrome using pointers.
#include<string.h>
#include<stdio.h>
#include<conio.h>

int main()
{
char str[30];
int i,j,n,flag;
flag=0;
clrscr();
printf("\nEnter a string:");
gets(str);
n=strlen(str)-1;
for(i=n,j=0;i>=0,j<=0;i--,j++)
{ if(str[i]==str[j])
flag=1;
else
flag=0; }
if(flag==1)
printf("\nThe string is a palindrome");
else if(flag==0)
printf("\nThe string is not a palindrome");
getch();
return 0; }
















DTU/2K13/B01/0147



OUTPUT


























DTU/2K13/B01/0147



PROGRAM NO 13 prg13.c

Program to convert uppercase to lowercase and vice-versa.

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char ch[20];
int i,l;
i=0;
printf("\nEnter a string:\t");
gets(ch);
l=strlen(ch);
for(i=0;i<l;i++)
{
if(ch[i]>='A'&&ch[i]<='Z')
ch[i]=ch[i]+32;
else if(ch[i]>='a'&&ch[i]<='z')
ch[i]=ch[i]-32;
}
puts(ch);
getch();
return 0;
}

















DTU/2K13/B01/0147

OUTPUT









































DTU/2K13/B01/0147
PROGRAM NO 14 prg14.c


Program to convert binary to decimal and vice versa

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

void bin_dec(long int num) // Function Definition
{
long int rem,sum=0,power=0;
while(num>0)
{
rem = num%10;
num = num/10;
sum = sum + rem * pow(2,power);
power++;
}

printf("Decimal number : %d",sum);
}

void main()
{
long int num;
clrscr();
printf("Enter the Binary number (0 and 1): ");
scanf("%ld",&num);
bin_dec(num);

getch();
}














DTU/2K13/B01/0147

OUTPUT









































DTU/2K13/B01/0147

PROGRAM NO 15 prg15.c


Program to sort an array using bubble sort
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
void bubble_sort(int [],int);
void main()
{
int a[30],n,i;
printf("nEnter no of elements :");
scanf("%d",&n);
printf("nEnter array elements :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble_sort(a,n);
getch();
}
void bubble_sort(int a[],int n)
{
int i,j,k,temp;
printf("nUnsorted Data:");
for(k=0;k<n;k++)
printf("%5d",a[k]);
for(i=1;i< n;i++)
{
for(j=0;j< n-1;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("nAfter pass %d : ",i);
for(k=0;k< n;k++)
printf("%5d",a[k]);
}
}



DTU/2K13/B01/0147

OUTPUT




























DTU/2K13/B01/0147

PROGRAM NO 16 prg16.c
Program to print
1
12
123
1234
12345

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

void main()
{
int i, j;
clrscr();
for(i=1;i<7;i++)
{
for(j=1;j<i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}

















DTU/2K13/B01/0147
OUTPUT










































DTU/2K13/B01/0147
PROGRAM NO 17 prg17.c

Program to add two 3X3 matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3],B[3][3],C[3][3],i,j;

printf("\nEnter MATRIX A:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&A[i][j]);
}
printf("\nMATRIX A: ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf(" %d ",A[i][j]);
}
printf("\nEnter MATRIX B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&B[i][j]);
}
printf("\nMATRIX B: ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf(" %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("\nSum of MATRICES is:\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf(" %d ",C[i][j]);
}
getch();}
DTU/2K13/B01/0147
OUTPUT





















PROGRAM NO 18























DTU/2K13/B01/0147
PROGRAM NO 18 prg18.c

PROGRAM TO MULTIPLY 3*3 MATRICES

#include<stdio.h>
#include<conio.h>
void main()
{
int A[3][3],B[3][3],C[3][3],i,j,k;
clrscr();
printf("\nEnter MATRIX A:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&A[i][j]);
}
printf("\nMATRIX A: ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf(" %d ",A[i][j]);
}
printf("\nEnter MATRIX B:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&B[i][j]);
}
printf("\nMATRIX B: ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
printf(" %d ",B[i][j]);
}
printf("\nProduct of MATRICES is:\n");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
C[i][j]=0;
for(k=0;k<3;k++)
C[i][j]=C[i][j]+A[i][k]*B[k][j];
printf(" %d ",C[i][j]);
}
}
getch();}

DTU/2K13/B01/0147
OUTPUT

















DTU/2K13/B01/0147
PROGRAM NO 19 prg19.c

Program to search for a number from an array.

#include<stdio.h>
#include<conio.h>
void main()
{
int A[100],n,s,i,flag=0;
clrscr();
printf("\nHow many elements? (less than 100):\t");
scanf("%d",&n);
printf("\nEnter elements\n");
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("\nEnter a number to search for:\t");
scanf("%d",&s);
for(i=0;i<n;i++)
{
if(s==A[i])
{
printf("\nElement found at position %d",i+1);
flag=1;
break;
}
}
if(flag==0)
printf("\nElement not found.\n");
getch();
}



















DTU/2K13/B01/0147
OUTPUT

















































DTU/2K13/B01/0147
PROGRAM NO 20 prg20.c

Program to generate employee details using structure.

#include<conio.h>
#include<stdio.h>
struct employee
{
char name[20],empid[10];
int age;
float sal;
}X;
void main()
{
clrscr();
printf("Enter the details of the SILLYCONE employee \n");
printf("Enter Employes Name :");
gets(X.name);
printf("Enter the employer id info:");
gets(X.empid);
printf("Enter the age :");
scanf("%d",&X.age);
printf("Enter the salary of employe :");
scanf("%f",&X.sal);
printf("\nName of Employee:");
puts(X.name);
printf("Employee id info:");
puts(X.empid);
printf("Age:%d",X.age);
printf("\nSalary:%f",X.sal);
getch();
}

















DTU/2K13/B01/0147
OUTPUT
































DTU/2K13/B01/0147
PROGRAM NO 21 prg21.c

Program to implement insertion sort in ascending order

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

int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++)
{d = c;
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;

d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
return 0;
}













DTU/2K13/B01/0147

OUTPUT


























DTU/2K13/B01/0147
PROGRAM NO 22 prg22.c

Program to pass and return a pointer to a function

#include<stdio.h>
#include<conio.h>
/* function declaration */
double getAverage(int *arr, int size);

int main ()
{
/* an int array with 5 elements */
int balance[5] = {1000, 2, 3, 17, 50};
double avg;

/* pass pointer to the array as an argument */
avg = getAverage( balance, 5 ) ;

/* output the returned value */
printf("Average value is: %f\n", avg );
getch();
return 0;
}
double getAverage(int *arr, int size)
{
int i, sum = 0;
double avg;

for (i = 0; i < size; ++i)
{
sum += arr[i];
}

avg = (double)sum / size;

return avg;
}







DTU/2K13/B01/0147

OUTPUT
















































DTU/2K13/B01/0147
PROGRAM NO 23 prg23.c

Program to pass an array to a function call() as a pointer to calculate the sum of the array

#include<stdio.h>
#include<conio.h>
double call(int *arr, int size);
int main ()
{

int balance[5];
double sum;
printf("enter 5 numbers");
for(int i=0;i<5;i++)
{
scanf("%d",&balance[i]);
}
sum = call( balance, 5 ) ;
printf("sum value is: %f\n", sum );
getch();
return 0;
}

double call(int *arr, int size)
{
int i, sum = 0;
for (i = 0; i < size; ++i)
{
sum += arr[i];
}


return sum;
}














DTU/2K13/B01/0147

OUTPUT









































DTU/2K13/B01/0147


PROGRAM NO 24 prg24.c

Program to copy content of one file into another after changing its case from lower
to upper

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
main()
{

FILE *fp1,*fp2;
int d;
char ch,fname1[20],fname2[20];
clrscr();x
printf("\n enter sourse file name");
gets(fname1);
printf("\n enter sourse file name");
gets(fname2);
fp1=fopen(fname1,"r");
fp2=fopen(fname2,"w");
if(fp1==NULL||fp2==NULL)
{
printf("unable to open");
exit(0);
}
do
{
while(ch!=EOF)
{
d=toupper(ch);
printf("%c",d);
ch=fgetc(fp1);
fputc(ch,fp2);
}


}
while(ch!=EOF);
fcloseall();
getch();
}




DTU/2K13/B01/0147



OUTPUT







































DTU/2K13/B01/0147



PROGRAM NO 25 prg25.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
slen(char *input)
{
printf("%s\n", input);
int length = 0;
while(input[length]!='\0')
{
length++;

}
printf("%d\n", length);
return(0);
}
main()
{

char *input = "This is a string";
slen(input);
getch();
return 0;

}














DTU/2K13/B01/0147
OUTPUT










































DTU/2K13/B01/0147
PROGRAM NO 26 prg26.c
#include<iostream.h>
#include<conio.h>
class MyClass
{
public:
int x;
MyClass();
~MyClass();
};
// Implement MyClassconstructor.
MyClass::MyClass()
{
x = 10;
}

MyClass::~MyClass()
{
cout<< "Destructing ...\n";
}
void main()
{
MyClass ob1;
MyClass ob2;
cout <<ob1.x<< " " <<ob2.x<<"\n";
MyClass::~MyClass();
getch();
}



















DTU/2K13/B01/0147
OUTPUT










































DTU/2K13/B01/0147
PROGRAM NO 27 prg27.c

Program to overload the operator +

#include <iostream>
using namespace std;
class myClass
{
public:
int data;
myClass &operator +(myClass &rhs)
{
myClass temp;
temp.data = data + rhs.data;
return temp;
};
};
int main()
{
myClass A;
myClass B;
A.data = 11;
B.data = 21;
A = A+B;
cout << A.data;
cin.get();
return 0;
}











-






DTU/2K13/B01/0147
OUTPUT










































DTU/2K13/B01/0147
PROGRAM NO 28 prg28.c

Program to find area and volume of different shapes using function overloading

#include <iostream.h>
#include <conio.h>
using namespace std;
class measure
{
public:
void shape(int r);
void shape(int l,int b);
void shape(float t,int d,int e);
void shape(long a);
void shape(float c, long int g);
void shape(double j);
void shape(float h, double f);
};
void measure::shape(int r)
{
cout<<"area of the circle is "<<3.14*r*r;
}
void measure::shape(int l,int b)
{
cout<<"area of the rectangle is"<<l*b;
}
void measure::shape(float t,int d,int e)
{
cout<<"area of the triangle is"<<t*d*e;
}
void measure::shape(long a)
{
cout<<"area of the square is"<<a*a;
}
void measure::shape(float c, long int g)
{
cout<<"Volume of the cone is "<<3.14*c*c*g/3;
}
void measure::shape(double j)
{
cout<<"Volume of the sphere is "<<4*3.14*j*j*j/3;
}
void measure::shape(float h, double f)
{
cout<<"\nVolume of the Cylinder is "<<3.14*f*f*h;
}
int main()
{
int r,d,e,l,b;
float t,c,h;
DTU/2K13/B01/0147
long a;
int ch;
double j,f;
long int g;
measure obj;
cout<<"\tCALCULATION OF AREA AND VOLUME";
cout<<"\n\n1. area of circle";
cout<<"\n2. area of rectangle";
cout<<"\n3. area of triangle";
cout<<"\n4. area of square";
cout<<"\n5. Volume of the cone";
cout<<"\n6. Volume of the sphere";
cout<<"\n7. Volume of the cylinder";
cout<<"\n\tEnter your choice ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the value of radius of the circle \n";
cin>>r;
obj.shape(r);
break;
case 2:
cout<<"enter the sides of rectangle \n";
cin>>l>>b;
obj.shape(l,b);
break;
case 3:
cout<<"enter the sides of triangle \n";
cin>>d>>e;
obj.shape(0.5,d,e);
break;
case 4:
cout<<"enter the sides of square";
cin>>a;
obj.shape(a);
break;
case 5:
cout<<"\nEnter the radius of the cone";
cin>>c;
cout<<"\nEnter the height of the cone";
cin>>g;
obj.shape(c,g);
break;
case 6:
cout<<"\nEnter the radius";
cin>>b;
obj.shape(b);
break;
case 7:
DTU/2K13/B01/0147
cout<<"\nEnter the radius";
cin>>f;
cout<<"\nEnter the height";
cin>>h;
obj.shape(h,f);
break;
default:
cout<<"\nThe choice entered is a wrong choice";
}
getch();
}

































DTU/2K13/B01/0147
OUTPUT










































DTU/2K13/B01/0147
PROGRAM NO 29 prg29.c

Program to implement multiple inheritance

#include<iostream.h>
#include<conio.h>
class M
{
protected:
int m;
public :
void get_M();
};
class N
{
protected:
int n;
public:
void get_N();
};
class P: public M, public N
{
public:
void display(void);
};
void M ::get_m(int x)
{
m=x;
}
void N::get_n(int y)
{
n=y;
}
void P:: display(void)
{
cout<<m=<<m<<endl;
cout<<n=<<n<<endl;
cout<<m*n=<<m*n<<endl;
}
main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
getch();
}


DTU/2K13/B01/0147
PROGRAM NO 30 prg30.c
#include<iostream.h>
#include<conio.h>


class rtriangle
{
float p,b,h;
float area,perimeter;

public:

float area1(float p,float b);
float perimeter1 (float p,float b,float h);
}c;

rtriangle::area1(int p,int b)
{
cout<<"please enter the base and height of the triangle");

c.area=(c.p*c.h)/2;

return c.area;
}

rtriangle::perimeter1(float,float,float)
{
cout<<"the perimeter is="<<endl;
c.perimeter=c.p+c.b+c.h;
return c.perimeter;
}

void main()
{ float p,b,h;
cout<<"please enter the base , perpendicular, and height of the triangle");
cin>>p>>b>>h;
c.area1(p,b);
c.perimeter1(p,b,h);
}

Das könnte Ihnen auch gefallen