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

CO

MPUT
ER
SCIEN
CE
PRO
GRAM
S

NAME- ADITYA
SINGH
CLASS- XII S/C
ROLL NO - 4
SCHOOLAHMEDABAD
PUBLIC SCHOOL

PROGRAM-23
/*Write a c++ programthat uses an area() function for the calculation of area of a
triangle or a
rectangle or a square.Number of sides suggest about the shape for which area is
to be calculated.*/
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
float area(float a,float b,float c)
{ float s,ar;
s=(a+b+c)/2;
ar=sqrt(s*(s-a)*(s-b)*(s-c));
return ar;
}
float area(float a,float b)
{ return a*b; }
float area(float a)
{ return a*a; }
int main()
{ system("cls");
int choice,s1,s2,s3,ar;
do
{ cout<<"\nArea Main Menu\n";
cout<<"1. Triangle \n";
cout<<"2. Square \n";
cout<<"3. Rectangle \n";
cout<<"4. Exit\n";
cout<<"Enter your choice(1-4):";

cin>>choice;
cout<<"\n";
switch(choice)
{ case 1: cout<<"Enter 3 sides\n";
cin>>s1>>s2>>s3;
ar=area(s1,s2,s3);
cout<<"The area is"<<ar<<"\n";
break;
case 2: cout<<"Enter a side\n";
cin>>s1;
ar=area(s1);
cout<<"The area is"<<ar<<"\n";
break;
case 3: cout<<"Enter the length and breadth\n";
cin>>s1>>s2;
ar=area(s1,s2);
cout<<"The area is"<<ar<<"\n";
break;
case 4: break;
default:cout<<"Wrong Choice!!\n";
};
}while(choice>0&&choice<4);
getch();
return 0;
}
/*
Area Main Menu
1. Triangle

2. Square
3. Rectangle
4. Exit
Enter your choice(1-4):2

Enter a side
12
The area is 144
*/

PROGRAM-27
//Program to illustrate the working of a function returning an object.

#include<iostream.h>
#include<conio.h>
class Distance{ int feet,inches;
public:
void getdata(int f,int i)
{ feet=f;inches=i;
} void printit(void)
{ cout<<feet<<"feet"<<inches<<"inches"<<"\n";
}
Distance sum(Distance d2);
};
Distance Distance::sum(Distance d2)
{ Distance d3;
d3.feet=feet+d2.feet+(inches+d2.inches)/12;
d3.inches=(inches+d2.inches)%12;
return(d3);
}
int main()
{ Distance Length1,Length2,total;
Length1.getdata(17,6);
Length2.getdata(13,8);
total=Length1.sum(Length2);
cout<<"Length1:";
Length1.printit();
cout<<"Length2:";
Length2.printit();
cout<<"Total length:";
total.printit();

getch();
return 0;
}
/*
Length1:17 feet 6 inches
Length2:13 feet 8 inches
Total length:31 feet 2 inches */

PROGRAM-30
/*Define a class named HOUSING in C++ with the following description:

Private Members:
REG_NO

integer (Ranges 10 - 2000)

NAME Array of characters (String)


TYPE Character
COST Float

Public Members:
Function Read_Data() to read an object of HOUSING type.
Function Display() to display the details of an object.
Function Draw-Nos() to choose and display the details of 2 houses selected
randomly from an array of 10
objects of type HOUSING. Use random function to generate the registration nos.
to match with REG_NO from the
array.*/

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
class HOUSING{
private:
int REG_NO;
char NAME[50];
char TYPE[20];
float COST;
public:
void Read_Data();
void Display();
void Draw_Nos(); };

void HOUSING::Read_Data()
{ cout<<"Enter Reg No (10 - 1000): ";
cin>>REG_NO;
cout<<"Name: ";
gets(NAME);
cout<<"Type: ";
gets(TYPE);
cout<<"Cost: ";
cin>>COST;
}
void HOUSING::Display()
{ cout<<"Reg No:"<<REG_NO<<endl;
cout<<"Name: "<<NAME<<endl;
cout<<"Type: "<<TYPE<<endl;
cout<<"Cost: "<<COST<<endl;
}
void HOUSING::Draw_Nos()
{ int Arr[5];
int no1,no2,i;
randomize();
no1=random(991)+10;//Generate a radom no between 10-1000
no2=random(991)+10;//Generate a radom no between 10-1000
cout<<"Random no-1: "<<no1<<"
for(i=0;i<100;i++)
{ if((Arr[i]==no1)||(Arr[i]==no2))
Display(); }
}
void main(){

Random no-2: "<<no2;

clrscr();
HOUSING a1;
a1.Read_Data();
a1.Display();
a1.Draw_Nos();
getch();
}/*
Enter Reg No (10 - 1000): 45
Name: Cloths
Type: Housing
Cost: 50000
Reg No:45
Name: Cloths
Type: Housing
Cost: 50000
Random no-1: 847

Random no-2: 142 */

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