0% found this document useful (0 votes)
50 views41 pages

C Programs

The document contains 21 code snippets showing examples of C++ programs implementing various OOP concepts like classes, inheritance, polymorphism etc. The programs cover concepts like class, objects, functions, single inheritance, multiple inheritance, multilevel inheritance, friend functions, static members etc.

Uploaded by

upscdestiny2
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)
50 views41 pages

C Programs

The document contains 21 code snippets showing examples of C++ programs implementing various OOP concepts like classes, inheritance, polymorphism etc. The programs cover concepts like class, objects, functions, single inheritance, multiple inheritance, multilevel inheritance, friend functions, static members etc.

Uploaded by

upscdestiny2
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/ 41

1. Program to add two numbers.

#include<iostream.h>

#include<conio.h>

int main()

int a,b,sum;

cout<<”\n Enter first number :”;

cin>>a;

cout<<”\n Enter second number :”;

cin>>b;

sum=a+b;

cout<<”\n Sum of the given numbers is :”<<sum;

getch();

return 0;

OUTPUT:-

1
2. Program to find area of a circle.

2
#include<iostream.h>

#include<conio.h>

int main()

float radius , area;

cout<<”\n Enter the radius of circle:”;

cin>>radius;

area=3.14*radius*radius;

cout<<”\n Area of the circle is :”<<area;

getch();

OUTPUT:-

3
3. Program to find simple interest.

4
#include<iostream.h>

#include<conio.h>

void main()

float P, R,T, S_I;

cout<<”\n Enter the principal value:”;

cin>>P;

cout<<”\n Enter the Rate of interest:”;

cin>>R;

cout<<”\n Enter the Time :”;

cin>>T;

S_I=(P*R*T)/100;

cout<<”\n The Simple interest is:”<<S_I;

getch();

clrscr();

OUTPUT:-

5
4. Program to find area of a square .

#include<iostream.h>

#include<conio.h>

int main()

int side,area;

cout<<”\n Enter the side of square:”;

cin>>side;

area=side*side;

6
cout<<”\n The area of the square is:”<<area;

getch();

return 0;

OUTPUT:-

5.Program to find volume of cylinder.

7
#include<iostream.h>

#include<conio.h>

int main()

float R,H,Volume;

cout<<”\n Enter the radius:”;

cin>>R;

cout<<”\n Enter the height:”;

cin>>H;

Volume=3.14*R*R*H;

cout<<”\n Volume of the cylinder is:”<<Volume;

getch();

OUTPUT:-

8
6. Program to check whether a number is
even or odd.

#include<iostream.h>

#include<conio.h>

int main()

int n;

cout<<”\n Enter the number you want to check”;

cin>>n;

if(n==0)

cout<<”\n Wrong input!!!”;

9
}

else

(n%2==0)?cout<<n<<”is even”:cout<<n<<”is odd”;

getch();

return 0;

OUTPUT:-

10
7. Program to check whether a number is
prime or not.

#include<iostream.h>

#include<conio.h>

int main()

int num,I,m=0,flag=0;

cout<<”\n Enter the number you want to check”;

cin>>num;

if(num==0||num==1)

cout<<num<<”is not prime”<<endl;

m=num/2;

for(i=2;i<=m;i++)

{
if(num%i==0)

cout<<num<<”is not prime”<<endl;

flag=1;

11
break;

if(flag=0)

cout<<num<<”is Prime”<<endl;

return 0 ;

OUTPUT:-

8. Program to check whether a number is


palindrome or not.

12
#include<iostream.h>

#include<conio.h>

int main()

int n,num,digit,rev=0;

cout<<”Enter a positive number :”;

cin>>num;

n=num;

do

digit=num%10;

rev=(rev*10)+digit;

num=num/10;

while(num!=0);

cout<<”\n Reverse of the given number is:”<<rev;

if(rev==n)

cout<<”\n The given number is a palindrome number “;

else

13
{

cout<<”\n The given number is not a palindrome number “;

return 0;

OUTPUT:-

14
9. Program to print all the Armstrong
numbers between 1 and 500.

#include<iostream.h>

#include<conio.h>

int main()

int a , count=1, remainder, sum;

while(count<=500)

a=count;

sum=0;

remainder=a%10;

sum=sum+(remainder*remainder*remainder);

a=a/10;

if(count==sum)

cout<<”\n”<<count<<”is an Armstrong number “;

15
count++;

return 0;

OUTPUT:-

10. Program to swap two values using call by


reference.

#include<iostream.h>

#include<conio.h>

16
void swap(int &val1, int&val2)

int temp;

temp=val1;

val1=val2;

val2=temp;

int main()

int a,b;

cout<<”\n Enter the first value:”;

cin>>a;

cout<<”\n Enter the second value:”;

cin>>b;

cout<<”\n Before swapping “<<”\n A=”<<a<<”\n B=”<<b<<endl;

swap(a,b);

cout<<”\n After swapping”<<”\n A=”<<a<<”\n B=”<<b<<endl;

return 0;

OUTPUT:-

17
11. Program to swap two values using call by
value.

#include<iostream.h>

#include<conio.h>

void swap(int,int);

int main()

int a,b;

cout<<”\n Enter first number::”;

cin>>a;

18
cout<<”\nEnter second number::”;

cin>>b;

cout<<”\n Before swapping, value of::\n\ta=”<<a<<”\tb=”


<<b<<”\n”;

swap(a,b);

cout<<”\n Outside function After swapping,value of::\n\ta=”


<<a<<”\tb=”<<b<<”\n”;

getch();

return 0;

void swap(int a, int b)

int c;

c=a;

a=b;

b=c;

cout<<”\n Inside function After swapping,value of::\n\ta=”


<<a<<”\tb=”<<b<<”\n”;

OUTPUT:-

19
12. Program to find if a year is leap or
not .
#include<iostream.h>

#include<conio.h>

int main()

int year;

cout<<"Enter a year:"<<endl:

cin>>year;

if(year%4==0)

20
{

if (year%100==)

if (year%400==0)

cout<<year<<"is a leap year."<<endl;

else

cout<<year<<"is a leap year"<<endl;

else

cout<<year<<"is a not a leap year";

getch();

return 0;

OUTPUT:-

21
13.Program to find factorial using
function.
#include<iostream.h>

#include<conio.h>

long factorial(int);

int main()

int n;

cout<<"enter a positive integer:";

getch();

return 0;

long factorial(int n)

22
long fact=1

for(int i=1;i<=n,i++0)

fact=fact*i;

return fact

OUTPUT:-

14.Program to find factorial using


recursion.
#include<iostream.h>

#include<conio.h>

23
int factorial(int n);

int main()

int n;

cout<<"Enter a positive integer:";

cin>>n;

cout<<'/n factorial of "<<n<<"="<<factorial(n);

getch(0;

return 0;

int factorial(int n)

if(n>1)

return n *factorial(n-1);

else

return 1;

OUTPUT:-

24
15. Program to implement class.
#include<iostream.h>

#include <conio.h>

class serum

public:

char* main_ingredient;

char*brand;

int price;

};

int main()

serum serum1;

serum1.main_ingredient ="Vitamin - c";

25
serum1.brand = "Blossom";

serum1.price = 349;

serum.serum2;

serum2.main_ingredient ="Niacinamide";

serum2.brand=399;

cout<<"serum1"<<"/n enriched with


"<<serum1.main_ingredient<<"formulated
by<<serum1.brand<<"grab at "<<serum1.price;

cout<<"serum2"<<"/n enriched with


"<<serum1.main_ingredient<<"formulated
by<<serum1.brand<<"grab at "<<serum1.price;

getch();

return0;

OUTPUT:-

16. Program to implement friend


function.

26
#include<iostream.h>

#include <conio.h>

class B ;

class A ;

int n;

public :

void putdata(int i)

x=i;

friend void max(A,B);

};

class B

int y;

public:

void putdata(int i)

y=i:

friend void max(A,B);

};

27
void max(Aa,Bb)

if(a.x>=b.y)

cout<<a.x<<"is the larger number.";

else

cout<<b.y<<"is the larger number";

int main()

A a;

B b;

a. put data (118);

b. put data(678);

max(a,b);

getch();

return0;

OUTPUT:-

28
17. Program to implement friend class.

#include<iostream.h>

#include <conio.h>

class class B ;

class class A ;

private :

int numA;

firend class class B;

piblic:

class A():num A (100){}

};

class classB

private :

int numB;

29
public:

classB():numB(48){}

int add()

classA objectA;

return objectA.numA + numB;

};

int main()

clrscr();

classB objectB;

cout<<"sum="<<object B.add()

getch();

return0;

OUTPUT:-

30
18. Program to implement static member
function.

#include<iostream.h>

#include <conio.h>

class Mystudent

private:

static int staticvar;

public:

static void static function ()

cout<<"\n regular function called !" <<ends;

};

int Myclass::staticvar=0;

int main()

Myclass::staticfunction();

Myclass object:

object regular function();

31
getch();

return0;

OUTPUT:-

19. Program to implement single


inheritance.
#Class base

public:

int x;

void getdata()

cout<<"/n Enter the value of x :";

cin>>x;

32
}};

Class derive ; public base

private:

int y ;

public;

void readdata()

cout<<"/n Enter the value of y :";

cin>>y;

void product()

cout<<"/n product = "<<x*y;

}};

int main()

derive a;

a.getdata();

a.readdata();

a.product();

getch();

return 0;

33
}

OUTPUT:-

20. Program to implement multiple


inheritance.
#include<iostream.h>

#include<conio.h>

Class A

protected;

int a;

34
public:

void get_a(int):

};

Class C : public A , public B

public:

void display(void);

};

void A :: get_a(int x)

a=x;

void B :: get_b(int y)

b=y;

void (::display(void))

cout<<" value of a "<<a<<"/n";

cout<<"vallue of b "<<b<<"/n";

int main()

35
Cc;

c.get_a(100):

c.get_b(200);

c.display();

getch();

return 0;

OUTPUT:-

21. Program to implement multilevel


inheritance.
#include<iostream.h>

#include <conio.h>

class A

36
public:

void display ()

COUT <<" Base class context.";

};

class B: public A{};

class C:public B{};

int main()

C obj ;

obj display ();

getch();

return0;

OUTPUT:-

37
22. Program to implement virtual
function.
#include<iostream.h>

#include<conio.h>

Class shape

public:

Virtual float calculate Area() Const{

return 0;

};

Class Rectangle: public shape

private:

float length , width;

public:

Rectangle(float l, float w) : length(l),width(w){}

float calculate Area() Const{

return length*width;

};

Class square : public shape{

38
private:

float side;

public:

square(float s):side(s){}

float calculateArea() Const{

return side*side;

};

int main()

Rectangle rect(7,8);

square sq(5);

cout<<"Area of rectangle : "<<rect.calculateArea()<<endl;

cout<<"Area of square : "<<sq.calculateArea()<<endl;

getch();

return 0;

OUTPUT:-

39
40
41

You might also like