0% found this document useful (0 votes)
26 views7 pages

imp c++ prog 2025

The document contains a collection of C++ programs demonstrating various programming concepts such as object-oriented programming, mathematical operations, and array manipulations. Key examples include calculating factorials, generating Fibonacci series, checking for prime numbers, and finding the GCD of two numbers. Additionally, it covers functions for calculating areas of geometric shapes and demonstrates function overloading.

Uploaded by

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

imp c++ prog 2025

The document contains a collection of C++ programs demonstrating various programming concepts such as object-oriented programming, mathematical operations, and array manipulations. Key examples include calculating factorials, generating Fibonacci series, checking for prime numbers, and finding the GCD of two numbers. Additionally, it covers functions for calculating areas of geometric shapes and demonstrates function overloading.

Uploaded by

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

C++ IMPORTANT PROGRAMS-2025

1) write a oop program in c++ to find factorial of given number.


#include<iostream.h>
#include<conio.h>
class fact
{
private:
int n;
public:
void getdata()
{
cout<<”enter value of n”<<endl;
cin>>n;
}
void display()
{
for(int i =1,f=1;i <=n; i++)
{
f=f* i ;
}
cout<<”Factorial of number “<<n<<” is =”<<f<<endl;
}
};
void main()
{
fact obj;
obj.getdata();
obj.display();
getch();
}

2) write a c++ program to display a series of 15 term of the Fibonacci series.


#include<iostream.h>
#include<conio.h>
class fibonacci
{
private:
int f0,f1,fib;
public:
fibonacci() //constructor
{f0=0;
f1=1;
}

void process()
{
int i ,n;
cout<<enter number of elements”<<endl;
cin>>n;
cout<<f0<<”\t”<<f1<<”\t”;

1
for( i =3,i <n; i ++)
{
fib=f0+f1;
cout<<fib<<”\t”;
f0=f1;
f1=fib ;
}
};

void main()
{
clrscr();
fibonacci f;
f .process();
getch();
}

3. Write a c++ program to accept a number and test whether it is prime or not.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
Int prime,c=0;
cout<<”enter the number”;
cin>>prime;
for ( int i=2; i <prime; i++)
{ if (prime % i==0)
c=1;
}
if( c==0)
cout<<”the number”<<prime <<”is prime number”<<endl;
else
cout<<”the number”<<prime <<”is not a prime number”<<endl;
getch();
}

4. write a C++ program to find GCD of two natural numbers.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n1,n2;
cout<<”enter two natural numbers”;
cin>>n1>>n2;

2
while(n1 != n2)
{
if(n1>n2)
n1=n1-n2;
If(n2>n1)
n2=n2-n1;
}
cout<<”The GCD is:”<<n1;
getch();
}

5. Write a c++ program with a function to find whether the year entered is leap year or
not?

#include<iostream.h>
#include<conio.h>
int leap(int); //function declaration
void main()
{
clrscr();
int n;
cout<<”enter the year”<<endl;
cin>>n;
cout<< leap(n);
getch();
}

int leap (int n)


{
If( n%4==0 && n%100 !=0 || n%400==0)
cout<<”the year is leap year”;
else
cout<<” the year is not leap year”;
return n;
}

6. Write a c++ program with computecircle() function (or void computercircle(float


&a, float &c,float &r) or void circle(float &a, float &c, float &r)) that returns the
area a and circumference c of a circle with given radius r.
#include<iostream.h>
#include <conio.h>
void computecircle(float &a, float &c,float &r);
void main()
{
clrscr();
float r,a,c;
cout<<”enter the value for radius r”;
cin>>r;
computecircle(a,c,r);
cout<<”the area with radius”<<r<<”is”<<a;
cout<<”the circumference is”<<c<<endl;
getch();
}
3
void computecircle( float &a, float &c, float &r)
{
a=3.14*r*r;
c=3.14*2*r;
}
7. implement a class rectangle .each object of this class will represent a rectangle
accepting its length and width as float.include an area function which will calculate
the area of the rectangle.

class rectangle
{
private:
float len,width;
public:
void getdata()
{cout<<”enter the length and width”<<endl;
cin >>len>>width;
}
void area()
{cout<<”area of rectangle=”<<len*width;
}
};
void main()
{
clrscr();
rectangle r;
r .getdata();
r .area();
getch();
}
8. Write a class based program in c++ to find area of a triangle.
#include<iostream.h>
#include<conio.h>
class triangle
{
private:
float a,b,h;
public;
void area()
{
cout<<”enter base and height”<<endl;
cin>>b>>h;
a=0.5*b*h;
cout<<”area of triangle is =:”<<a;
}
};
void main()
{
clrscr();
triangle t;
t.area();
getch();
}
4
9. Write a c++ program to find largest in an array of 10 float numbers.
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a[10];
cout<<”enter 10 float values”<<endl;
for(int i =0; i<=9;i++)
{
cin>>a[i];
}
int largest=a[0];
for(int i =0; i<=9;i++)
{
if (a[i ]>largest)
{
largest=a[ i];
}
}
cout<<”largest of all 10 values is”<<largest;
getch();
}
10. Write a c++program to sort 10 integers numbers in ascending order.
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n[10],i,j,temp;
cout<<”enter 10 integers”<<endl;
for ( i=0;i<=9;i++)
{ cin>>n[i];
}

for(i=0;i<=9;i++)
{
for(j=1,j<=9;j++)
{
if(n[i]>n[j];
{ temp=n[i];
n[j]=n[i];
n[j]=temp;
}
}
}
cout<<”sorted list in ascending order is”<<endl;

for ( i=0;i<=9;i++)
{ cout>>n[i]<<endl;
}
getch();
}

5
11. write an oop in c++ to read an integer number and find the sum of digits of integer
( input 125 otuput 8 ie 1+2+5=8)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int val,num,sum=0;
cout<<”enter the number:”
cin>>val;
num=val;
while(num !=0)
{
sum=sum+num%10;
num=num/10;
}
cout<<”the sum of the digits of”<<val<<”is”<<sum;
getch();
}

12. Write a c++ program to overload add( ) function which will add two
integer[ add(int,int)] and three integers[ add(int,int,int)]

#include<iostream.h>
#include<conio.h>
int add(int,int);
int add(int,int,int);
void main ( )
{
clrscr( );
int a,b,p,q,r,a1,a2;
cout<<”enter two numbers”;
cin>>a>>b;
a1=add(a,b);
cout<<”addittion of two numbers = “<<a1;
cout<<”enter three numbers”;
cin>>p>>q>>r;
a2=add(p,q,r);
cout<<”addittion of two numbers = “<<a2;
getch();
}

int add(int n1,int n2)


{return n1+n2;
}

int add(int m1,int m2,int m3)


{return m1+m2+m3;
}

6
13. write a c++ program to find largest of four given integers using max() function to
return largest of four given integers; int max(int,int,int,int)

#include<iostream.h>
#include<conio.h>
int max(int,int,int,int);
void main ( )
{
clrscr( );
int w,x,y,z;
cout<<”enter four integers”;
cin>>w>>x>>y>>z;
cout<<”maximum=”<<max(w,x,y,z);
getch();
}
int max(int n1,int n2,int n3, intn4)
{
int max=n1;
if(n2>max)
max=n2;
if(n3>max)
max=n3;
if (n4>max)
max=n4;
return max;
}
14. Write a program in c++ to initialize the array of 10 integers and find sum and average
of all the elements of array.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10], i, sum;
float avg;
cout<<”enter 10 elements”;
for(i=0;i<10;i++)
{cin<<a[i];
}
sum=0;
cout<<”\n the array =”<<endl;
for(i=0;i<10;i++)
{cout<<a[i];<<” ”;
sum=sum+a[i];
}
avg=sum/10;
cout<<”\n sum of all element is=”<<sum;
cout<<”\n average of all elements of array is =”<<avg;
getch();
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy