imp c++ prog 2025
imp c++ prog 2025
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();
}
#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();
}
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();
}
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();
}