All CPP - 241118 - 074100
All CPP - 241118 - 074100
Program Code
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
float a,b,c,root1,root2,img;
cout<<"Enter the three coefficients: ";
cin>>a>>b>>c;
cout<<"Roots of the equation are: \n";
if(b*b>=4*a*c)
{
root1=(-b+sqrt(b*b-4*a*c))/(2*a);
root2=(-b-sqrt(b*b-4*a*c))/(2*a);
cout<<root1<<"\n"<<root2;
}
else
{
img=sqrt(-(b*b-4*a*c))/(2*a);
cout<<-b/(2*a)<<"+i"<<img<<"\n";
cout<<-b/(2*a)<<"-i"<<img<<"\n";
}
return 0;
}
...................................................................................................................................
Program Code
#include<iostream>
using namespace std;
int main()
{
int n1,n2,n3,largest;
cout<<"Input three numbers:";
cin>>n1>>n2>>n3;
if(n1>n2)
{
if(n1>n3)
{
largest=n1;
}
}
else if(n2>n3)
{
largest=n2;
}
else
{
largest=n3;
}
cout<<"Largest number is:"<<largest;
..........................................................................................................
3. Sum of the Digits
Aim
Find the sum of the digits of an integer number.
Program Code
#include<iostream>
using namespace std;
int main()
{
int n,sum=0,d;
cout<<"Input a number:";
cin>>n;
while(n>0)
{
d=n%10;
sum=sum+d;
n=n/10;
}
cout<<"Sum of the digits:"<<sum;
}
...................................................................................................................
4. Sum of the squares
Aim
Find the sum of the squares of the first N natural numbers.
Program Code
#include<iostream>
using namespace std;
int main()
{
int n,i,sum=0;
cout<<"Enter the number to find sum of squares up to that number :";
cin>>n;
for(i=1;i<=n;i++)
{
sum=sum+(i*i);
}
cout<<"The sum of the squares up to "<<n<<" is:"<<sum;
return 0;
}
......................................................................................................
5. Value of nCr
Aim
Define a function to find the factorial of a number. Using this function find the value of nCr.
Program Code
#include<iostream>
using namespace std;
long fact(int n)
{
long f=1;
for(int i=1;i<=n;i++)
{
f=f*i;
}
return f;
}
int main()
{
int n,r;
cout<<"Input values for \n ";
cout<<"\t n:";
cin>>n;
cout<<"\t r:";
cin>>r;
if(r>n)
{
cout<<"Wrong input ";
}
else
{
cout<<"The value of nCr is :"<<fact(n)/(fact(r)*fact(n-r));
}
return 0;
}
.......................................................................................................................................
6. Average Height
Aim
Input the height of 10 students and find average height.
Programme Code
#include<iostream>
using namespace std;
int main()
{
float height[10],sum=0,avg;
int i;
cout<<"Input height of 10 students:";
for(i=0;i<10;i++)
{
cin>>height[i];
}
for(i=0;i<10;i++)
{
sum=sum+height[i];
}
avg=sum/10;
cout<<"Avrage height of the students:"<<avg;
}
................................................................................................................
7. Palindrome Number
Aim
Input a number and check whether it is palindrome or not
Programme Code
#include<iostream>
using namespace std;
int main()
{
int n,revno=0,temp;
cout<<"Input the number:";
cin>>n;
temp=n;
while(n>0)
{
revno=revno*10+n%10;
n=n/10;
}
if(temp==revno)
cout<<"Palindrome";
else
cout<<"Not a palindrome";
}
.......................................................................................................................................
8. Array Sorting
Aim
Create an array to store the heights of some students and sort the values.
Program Code
#include<iostream>
using namespace std;
int main()
{
int height[20],n,i,j,temp;
cout<<"Number of students:";
cin>>n;
cout<<"Enter the heights of the students:";
for(i=0;i<n;i++)
cin>>height[i];
for(i=0;i<n;i++)
{
for(j=i;j<n;j++)
{
if(height[i]>height[j])
{
temp=height[i];
height[i]=height[j];
height[j]=temp;
}
}
}
cout<<"Heights of the students in ascending order:";
for(i=0;i<n;i++)
{
cout<<height[i]<<" ";
}
}
...........................................................................................
9. Swap using Pointers
Aim
Define a function to swap the contents of the two variables. Using this function,
interchange the values of three variables. E.g. A->B->C->A.
Programme Code
#include<iostream>
using namespace std;
void swap(int *x,int *y);
int main()
{
int a,b,c;
cout<<"Input the values of\n\ta:";
cin>>a;
cout<<"\tb:";
cin>>b;
cout<<"\tc:";
cin>>c;
swap(&a,&b);
swap(&a,&c);
cout<<"After swap operation\n\ta:"<<a<<"\n\tb:"<<b<<"\n\tc:"<<c;
return 0;
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
....................................................................................................
10. Structure to Store Student Details
Aim
Write a program to find the total marks of a student by defining a structure with the
details such as roll number, name and marks of six subjects.
Programme Code
#include<iostream>
using namespace std;
struct student
{
int rollno;
char name[30];
int mark[6];
};
int main()
{
student s1;
int total;
cout<<"Enter student details\n";
cout<<"\tRoll Number:";
cin>>s1.rollno;
cout<<"\tName:";
cin.ignore();
cin.getline(s1.name,30);
cout<<"\tEnglish:";
cin>>s1.mark[0];
cout<<"\tSL:";
cin>>s1.mark[1];
cout<<"\tPhysics:";
cin>>s1.mark[2];
cout<<"\tChemistry:";
cin>>s1.mark[3];
cout<<"\tMaths:";
cin>>s1.mark[4];
cout<<"\tCS:";
cin>>s1.mark[5];
total=s1.mark[0]+s1.mark[1]+s1.mark[2]+s1.mark[3]+s1.mark[4]+s1.mark[5];
system("clear");
cout<<"Student details you submitted here is......."
<<"\n\tRoll number:"<<s1.rollno
<<"\n\tName:"<<s1.name
<<"\n\tMarks obtained in ..........."
<<"\n\tEnglish:"<<s1.mark[0]
<<"\n\tSL:"<<s1.mark[1]
<<"\n\tPhysics:"<<s1.mark[2]
<<"\n\tChemistry:"<<s1.mark[3]
<<"\n\tMaths:"<<s1.mark[4]
<<"\n\tCS:"<<s1.mark[5]
<<"\n\tTotal Mark:"<<total;
}