0% found this document useful (0 votes)
44 views5 pages

Lab 2

The documents contain 5 C++ programs that demonstrate different programming concepts like function overloading, macros, simple interest calculation, pass by reference in functions, and swapping of variables.

Uploaded by

Dewanand Giri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views5 pages

Lab 2

The documents contain 5 C++ programs that demonstrate different programming concepts like function overloading, macros, simple interest calculation, pass by reference in functions, and swapping of variables.

Uploaded by

Dewanand Giri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

Program

#include<iostream>
#include<cstdlib>
using namespace std;

float area(float r)
{
return(3.14 * r * r);
}
float area(float b,float h)
{
return(0.5 * b * h);
}
float area(float l,float b)
{
return (l * b);
}
int main()
{
float b,h,r,l;
int ch;

do
{
cout<<"\n\n *****Menu***** \n";
cout<<"\n 1. Area of Circle";
cout<<"\n 2. Area of Triangle";
cout<<"\n 3. Area of Rectangle";
cout<<"\n 4. Exit";
cout<<"\n\n Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
{
cout<<"\n Enter the Radius of Circle : ";
cin>>r;
cout<<"\n Area of Circle : "<<area(r);
break;
}
case 2:
{
cout<<"\n Enter the Base & Height of Triangle : ";
cin>>b>>h;
cout<<"\n Area of Triangle : "<<area(b,h);
break;
}
case 3:
{
cout<<"\n Enter the Length & Bredth of Rectangle : ";
cin>>l>>b;
cout<<"\n Area of Rectangle : "<<area(l,b);
break;
}
case 4:
exit(0);
default:
cout<<"\n Invalid Choice... ";
}
}while(ch!=4);
return 0;
}

2. Program 2

/**
* C program to find square and cube of a number using macro
*/

#include <stdio.h>

// Define macro to find square and cube


#define SQUARE(x) (x * x)
#define CUBE(x) (x * x * x)

int main()
{
int num;

// Input a number from user


printf("Enter any number to find square and cube: ");
scanf("%d", &num);

// Calculate and print square


printf("SQUARE(%d) = %d\n", num, SQUARE(num));
// Calculate and print cube
printf("CUBE(%d) = %d\n", num, CUBE(num));

return 0;
}

3. PROGRAM 3

#include<i
ostream>
#include<conio.h>

using namespace std;

float interest(float p,float t,float r){ return((p*t*r)/100);}

int main(){
float p,t,r;
cout<<"Principle = ";cin>>p;
cout<<"Time = ";cin>>t;
cout<<"Rate = ";cin>>r;
cout<<"Simple Interest = "<<interest(p,t,r);
getch();
return(0);
}
4. Program 4

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void intrest(int p, int t, float ra=9.25);
int main()
{
char na[100];
int p,t,r,age;
clrscr();
cout<<endl<<"ENTER A NAME OF CUSTOMER:";
cin>>na;

cout<<endl<<"ENTER AGE OF CUSTOMER:";


cin>>age;

if(age<60)
{
cout <<endl<< "Enter Principal Amount: ";
cin >> p;
cout <<endl<< "Enter Time in Years: ";
cin >> t;
intrest(p,t);
}
else
{
cout <<endl<< "Enter Principal Amount: ";
cin >> p;
cout <<endl<< "Enter Time in Years: ";
cin >> t;
cout <<endl<< "Enter intrest rate: ";
cin >> r;
intrest(p,t,r);
}
getch();
return 0;
}
void intrest(int p, int t, float ra)
{
float si;
si= p * t * ra / 100;

cout<<endl<<"Simple Intrest is :"<<si;


}

Program 5
#include<iostream.h>
#include<conio.h>

void​ swap(​int​ *num1, ​int​ *num2);

void​ main() {
​int​ x, y;

printf("\nEnter First number : ");


scanf("%d", &x);

printf("\nEnter Second number : ");


scanf("%d", &y);

printf("\nBefore Swaping x = %d and y = %d", x, y);


swap(&x, &y); // Function Call - Pass By Reference

printf("\nAfter Swaping x = %d and y = %d", x, y);


getch();
}

void​ swap(​int​ *num1, ​int​ *num2) {


​int​ temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}

You might also like