[go: up one dir, main page]

0% found this document useful (0 votes)
4 views20 pages

CPP Programs

The document contains ten C++ programs that perform various mathematical and algorithmic tasks, including calculating areas of geometric figures, finding roots of quadratic equations, generating Fibonacci series, summing squares of natural numbers, checking for palindromes, determining prime numbers, summing digits of integers, finding the largest number in an array, searching for an item in an array, and calculating factorials. Each program includes user input prompts and outputs the results accordingly. The programs demonstrate basic programming concepts and operations in C++.

Uploaded by

yadhunath980
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)
4 views20 pages

CPP Programs

The document contains ten C++ programs that perform various mathematical and algorithmic tasks, including calculating areas of geometric figures, finding roots of quadratic equations, generating Fibonacci series, summing squares of natural numbers, checking for palindromes, determining prime numbers, summing digits of integers, finding the largest number in an array, searching for an item in an array, and calculating factorials. Each program includes user input prompts and outputs the results accordingly. The programs demonstrate basic programming concepts and operations in C++.

Uploaded by

yadhunath980
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/ 20

PROGRAM 1

//area of figures
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,b,c,d,r,l,n;
float Ar,s;
cout<<"\n program to find area of figures \n";
cout<<"\n enter1-circle \n";
cout<<"\n enter2-rectangle \n";
cout<<"\n enter3-triangle \n";
cout<<"\n enter your choice 1,2,3 \n";
cin>>n;
switch (n)
{
case 1: cout<<"\n enter the radius \n";
cin>>r;
Ar=3.14*r*r;
cout<<"\n area of circle is : "<<Ar;
break;
case 2: cout<<"\n enter the length and breadth \n";
cin>>l>>b;
Ar=l*b;
cout<<"\n area of rectangle is :"<<Ar;
break;
case 3: cout<< "\n enter 3 sides \n";
cin>>a>>b>>c;
s=(a+b+c)/2;
Ar= sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"\n Area of triangle is :"<<Ar;
break;
default:cout<<"\n please enter 1,2,3 \n";
}
return 0;
}
PROGRAM 1 OUTPUT

program to find area of figures

enter1-circle

enter2-rectangle

enter3-triangle

enter your choice 1,2,3


3

enter 3 sides
5
6
5

Area of triangle is :12


PROGRAM 2
//find the roots of quadratic equation by inputting three coefficients
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,b,c,d;
float r1,r2;
cout<<"\n program to find roots \n";
cout<<"\n enter the values ofa,b,c,d \n";
cin>>a>>b>>c;
d=(b*b)-4*a*c;
if(d==0)
{
cout<<"\n roots are real and equal \n";
r1=r2=-b/(2*a);
cout<<"\n root 1 ="<<r1;
cout<<"\n root 2 ="<<r2;
}
else if(d>0)
{
cout<<"\n roots are real and unequal \n";
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d) )/(2*a);
cout<<"\n root1 = "<<r1;
cout<<"\n root2="<<r2;
}
else
{
cout<<"\n roots are imaginary \n";
}
return 0;
}
PROGRAM 2 OUTPUT
program to find roots

enter the values ofa,b,c,d


2
4
1

roots are real and unequal

root1 = -0.292893
root2: = -1.70711
PROGRAM 3
//program to print first N terms of fibonacci series
#include<iostream>
using namespace std;
int main()
{
int a=-1,b=1,c,n;
cout<<"\n enter numbers of terms in the series \n";
cin>>n;
cout<<a<<"\t"<<b;
for (int i=1;i<=n;i++)
{
c=a+b;
cout<<"\t"<<c;
a=b;
b=c;
}
return 0;
}
PROGRM 3 OUTPUT
enter numbers of terms in the series
10
-1 1 0 1 1 2 3 5 8 13 21 34
PROGRAM 4
//find the sum of squares of first n natural numbers
#include<iostream>
using namespace std;
int main()
{
int n,sum=0;
cout<<"\n enter a positive integer\n";
cin>>n;
for(int i=1; i<=n;++i)
{
sum = i*i ;
}
cout<<" sum of squares is "<<sum;
return 0;
}
PROGRAM 4 OUTPUT
enter a positive integer
7
sum of squares is 49
PROGRAM 5
//input a number and check whether it is paliandrome or not
#include<iostream>
using namespace std;
int main()
{
int n,rev=0,s;
cout<<"\n program to check whether number is paliandrome or not\n";
cout<<"\n enter the number\n";
cin>>n;
s=n;
while(n>0)
{
rev=(rev*10)+n%10 ;
n = n/10;
}
if(rev==s)
{
cout<<"\n"<<s<<" is a paliandrome \n";
}
else{
cout<<"\n <<s<< is not a paliandrome \n";
}
return 0;
}
PROGRAM 5 OUTPUT
program to check whether number is paliandrome or not

enter the number


88

88 is a paliandrome
PROGRAM 6
//check whether a number is prime or not
#include<iostream>
using namespace std;
int main()
{
int n,i,f=0;
cout<<"\n program to check prime or not\n";
cout<<"\n enter a number \n";
cin>>n;
if(n==1)
{
cout<<"\n 1 is not a prme \n";
}
else if(n==2)
{
cout<<"\n 2 is a prime number\n";
}
else
{
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
f=1;
}break;
}
if(f==0)
{
cout<<"\n"<<n<<" is a prime";
}
else
{
cout<<"\n"<<n<<" is not a prime \n";
}
return 0;
}
}
PROGRAM 6 OUTPUT
program to check prime or not

enter a number
5

5 is a prime
PROGRAM 7
//sum of digits of an integer number
#include<iostream>
using namespace std;
int main()
{
int n,sum=0;
cout<<"\n program to find the sum of digits of an integer number\n";
cin>>n;
while(n>0)
{
sum=sum+n%10;n=n/10;
}
cout<<"\n sum of digits is " <<sum;
return 0;
}
PROGRAM 7 OUTPUT
program to find the sum of digits of an integer number
567

sum of digits is 18
PROGRAM 8
//find the largest number of arrays
#include<iostream>
using namespace std;
int main()
{
int A[50],i,n,l;
cout<<"\n enter the number of students\n";
cin>>n;
cout<<"\n enter"<<n<<" numbers\n";
for(i=0;i<n;i++)
{cin>>A[i];
}
cout<<"\n numbers are \n";
for(i=0;i<n;i++)
{
cout<<A[i]<<" ";
}
l=A[0];
for(i=1;i<n;i++)
{
if(A[i]>l)
{
l=A[i];
}
}
cout<<"\n largest number is "<<l;
return 0;
}
PROGRAM 8 OUTPUT
enter the number of students
6

enter6 numbers
12
34
9
13
67
54

numbers are
12 34 9 13 67 54
largest number is 67
PROGRAM 9
#include<iostream>
using namespace std;
int main()
{
int A[50],i,n,item,f=0;
cout<<"\n enter the number of arrays\n";
cin>>n;
cout<<"\n enter"<<n<<" numbers\n";
for(i=0;i<n;i++)
{cin>>A[i];
}
cout<<"\n numbers are \n";
for(i=0;i<n;i++)
{
cout<<A[i]<<" ";
}
cout<<"\n enter the item to be searched \n";
cin>>item;
for(i=0;i<n;i++)
{
if(A[i]==item)
{
f=1;
cout<<"\n"<<item<<" is found at "<<i+1;
}
}
if(f==0)
{
cout<<"\n" <<item<<" does not exist ";
}
return 0;
}
PROGRAM 9 OUTPUT
enter the number of arrays
7

enter7 numbers
12
3
9
56
78
98
4

numbers are
12 3 9 56 78 98 4
enter the item to be searched
4

4 is found at 7
PROGRAM 10
//find the factorial of a number
#include<iostream>
using namespace std;
int main()
{
int fact=1,n,i;
cout<<"\n program to find the factorial of a number\n";
cout<<"\n enter the number \n";
cin>>n;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"\n factorial of "<<n<<"is" <<fact;
return 0;
}
PROGRAM 10 OUTPUT
program to find the factorial of a number

enter the number


6

factorial of 6 is 720

You might also like