C++ Programs - CSlab-3
C++ Programs - CSlab-3
C++ PROGRAMS
----------------------------------------------------------------------------------------------------------------------------
AIM
Input the three coefficients of a quadratic equation and find the roots.
Program
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int a,b,c;
float d,r1,r2;
cout<<”Enter the three coefficients”;
cin>>a>>b>>c;
if(a==0)
{
cout<<”Not a quadratic equation “;
}
d=(b*b) -4*a*c;
if(d==0)
{
r1=(-b)/(2*a);
cout<<”roots are real and equal and root is “ <<r;
}
else if(d>0)
{
r1=(-b +sqrt(d))/(2*a);
r2=(-b -sqrt(d))/(2*a);
cout<<”roots are real and unequal”<<”\n”;
cout<<”root 1= “<<r1<<”\t root 2= “<<r2;
}
else
cout<<”Roots are complex and imaginary \n”;
return 0;
}
Sample output
2 -5 3
Roots are real and unequal
AIM
Input a digit and display the same in word. (Zero for 0, One for 1, ...., Nine for 9)
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<”Enter a number between 0 to 9”;
cin>>n;
switch(n)
{
case 0: cout<<"Zero";
break;
case 1: cout<<"One";
break;
case 2: cout<<"Two";
break;
case 3: cout<<"Three";
break;
case 4: cout<<"Four";
break;
case 5: cout<<"Five";
break;
case 6: cout<<"Six";
break;
case 7: cout<<"Seven";
break;
case 8: cout<<"Eight";
break;
case 9: cout<<"Nine";
break;
default: cout<<"Invalid input! ";
}
return 0;
}
Sample output
AIM
#include<iostream>
using namespace std;
int main()
{
int n,i,sum=0;
cout<<"Enter the value for n";
cin>>n;
for(i=1;i<=n;i++)
sum=sum+ i*i;
cout<<"Sum of squares of First "<<n<<" Natural numbers = "<<sum;
return 0;
}
Sample output
4)Palindrome
Aim
Program
#include<iostream>
using namespace std;
int main()
{
int num,rev=0,digit,copy;
cout<<"Enter the number ";
cin>>num;
copy=num;
while(num>0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
if(copy==rev)
cout<<"Given Number is palindrome";
else
cout<<"Given Number is not palindrome";
return 0;
}
Sample output
121
Aim
Program
#include<iostream>
using namespace std;
int main()
{
int num,i,flag=0;
cout<<"Enter the number ";
cin>>num;
for(i=2;i<=num/2;i++)
{
if(num%i == 0)
{
flag++;
break;
}
}
if(flag==0)
{
cout<<”Prime number\n”;
}
else
{
cout<<”Not a prime number\n”;
}
return 0;
}
Sample output
29
Prime number
6)Sorting of array
Aim
Create an array to store the heights of n students and sort the values
#include <iostream>
using namespace std;
int main()
{
int n,temp;
cout<<"Enter the size of array: ";
cin>>n;
int a[n];
cout<<"\nEnter the elements: ";
for(int i=0; i<n; i++)
cin>>a[i];
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
cout<<"\nThe sorted array is ";
for(int i=0; i<n; i++)
cout<<a[i]<<" ";
return 0;
}
Sample output
7)Length of a string
Aim
#include <iostream>
using namespace std;
int main()
{
char str[30];
int i,length=0;
cout<<"Enter the string:";
gets(str);
for(i=0;str[i]!='\0';i++)
{
length++;
}
cout<<"Length of the string is:"<<length<<endl;
return 0;
}
Sample output
Malayalam
Aim
Define a function to find the factorial of a number. Using this function find the value of nCr
Program
#include<iostream>
using namespace std;
int factorial(int n)
{
int i,fact=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
return fact;
}
int main()
{
int n,r,nCr;
cout<<"Enter the value of n and r for finding nCr";
cin>>n>>r;
if(n>=r && r>=0)
{
nCr = fact(n)/( fact(r) * fact (n-r));
}
cout<<"nCr = "<<nCr;
return 0;
}
Sample output
53
nCr = 10
Aim
Create a structure to represent admission number, name and marks given for CE, PE of a subject. Input
the details of student and display admission number, name and total marks obtained.
Program
#include <iostream>
using namespace std;
struct student
{
char name[50];
int adm_no;
float ce,pe,te;
};
int main()
{
student s;
cout << "\nEnter name of the student: ";
cin >> s.name;
cout << "\nEnter admission number: ";
cin >> s.adm_no;
cout << "\nEnter CE,PE marks: ";
cin >> s.ce>>s.pe;
s.te=s.ce+s.pe;
cout << "\nDisplaying details of the student" ;
cout << "\nName: " << s.name;
cout << "\nAdmission number:: " << s.adm_no;
cout << "\nCE marks: " << s.ce;
cout << "\nPE marks: " << s.pe;
cout << "\nTotal marks: " << s.te;
return 0;
}
Sample output
Name: Sandra
CE mark : 18
PE mark : 54
Aim
Input two numbers swap them by defining a function with pointer arguments.
#include<iostream>
using namespace std;
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int num1,num2;
cout<<"Enter the numbers ";
cin>>num1>>num2;
cout<<"Before Swapping: num1 = "<<num1<<" \t num2 ="<<num2;
swap(num1,num2);
cout<<"\nAfter Swapping : num1= "<<num1<<"\t num2 ="<<num2;
return 0;
}
Sample output
15 23