c++
1.FIND THE LARGEST OF 3 NUMBERS
Aim:Write a c++program to find the largest of 3 numbers
Program code
#include<iostream>
using namespace std;
int main()
{
int n1,n2,n3;
cout<<"program to find the largest of 3 numbers\n";
cout<<"Enter 3 numbers";
cin>>n1>>n2>>n3;
if(n1>n2&&n1>n3)
cout<<n1<<"is largest";
else if(n2>n3)
cout<<n2<<"is largest";
else
cout<<n3<<"is largest";
return 0;
}.
2.WORD OF DIGIT USING SWITCH
Aim:Write a c++ program to print the word of digit(0-9) using switch statement
Program code
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"program to print the word of given digit\n";
cout<<"Enter a digit from (0-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;
}
3.AREA OF RECTANGLE,CIRCLE AND TRIANGLE USING SWITCH
Aim:Write a c++ program to find the area of rectangle,circle and triangle using switch statement
#include<iostream>
using namespace std;
int main()
{
int l,b,r,base,h,ch;
float area;
cout<<"1.Area of Rectangle\n";
cout<<"2.Area of Circle\n";
cout<<"3.Area of Triangle\n";
cout<<"Enter a Choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"Enter length and breadth of a Rectangle";
cin>>l>>b;
area=l*b;
cout<<"Area of Rectangle="<<area;
break;
case 2:cout<<"Enter radius of a circle";
cin>>r;
area=3.14*r*r;
cout<<"Area of Circle="<<area;
break;
case 3:cout<<"Enter base and height of Triangle";
cin>>base>>h;
area=0.5*base*h;
cout<<"Area of Triangle="<<area;
break;
default:cout<<"Invalid choice";
}
return 0;
}
4.SUM OF SQUARES OF FIRST N NATURAL NUMBERS
Aim:Write a c++ program to find the sum of squares of first n natural numbers
Program code
#include<iostream>
using namespace std;
int main()
{
int n,i,s=0;
cout<<"program to print the sum of squares of n natural numbers\n";
cout<<"Enter the limit";
cin>>n;
for(i=1;i<=n;i++)
s=s+(i*i);
cout<<"Sum of squares="<<s;
return 0;
}
5.SUM OF DIGITS
Aim:Write a c++ program to find the sum of digits of a given number
Program code
#include<iostream>
using namespace std;
int main()
{
int n,rem,s=0;
cout<<"program to print the sum of digits\n";
cout<<"Enter a number";
cin>>n;
while(n>0)
{
rem=n%10;
s=s+rem;
n=n/10;
}
cout<<"Sum of digits="<<s;
return 0;
}
6..PALINDROME OR NOT
.Aim: Wite a c++program to lnput a number and check Whether it is palindrome or not.
Program code
#include<iostream>
using namespace std;
int main()
{
int num,rev=0,rem,copy;
cout<<"Enter a number ";
cin>>num;
copy=num;
while(num>0)
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
if(copy==rev)
cout<<"Given Number is palindrome";
else
cout<<"Given Number is not palindrome";
return 0;
}
7.LENGTH OF STRING
Aim:Write a c++ program to find the length of a given string without using strlen() function
Program code
#include<iostream>
using namespace std;
int main()
{
int length=0,i;
char s[20];
cout<<"program to find the length of string\n";
cout<<"Enter a string";
cin.getline(s,20);
for(i=0;s[i]!='\0';i++)
{
length++;
}
cout<<"Length of given string="<<length;
return 0;
}
8. AVERAGE HEIGHT OF 10 STUDENTS
Aim:Write a c++ program to find the average height of 10 students
Program code
#include<iostream>
using namespace std;
int main()
{
int h[20],i,n,s=0;
float avg;
cout<<"program to print the average height of n students\n";
cout<<”enter the number of students”;
cin>>n;
cout<<"Enter the heights of students: ";
for(i=1;i<=n;i++)
{
cin>>h[i];
s=s+h[i];
}
avg=s/10.0;
cout<<"Average height of the students is "<<avg<<"\n";
return 0;
}
9.FACTORIAL USING FUNCTION
Aim:Write a c++ program to find the factorial of a given number using function
Program code
#include<iostream>
using namespace std;
void fact(int n)
{
int f=1,i;
for(i=1;i<=n;i++)
f=f*i;
cout<<"factorial of "<< n <<" is "<<f;
}
int main()
{
int n;
cout<<"Enter a number ";
cin>>n;
fact(n);
return 0;
}
10.INTERCHANGE VALUES OF 2 VARIABLES
Aim:Write a c++ program to interchange the values of 2 variables (a->b,b->a)
Program code
#include<iostream>
using namespace std;
void swap(int&a,int&b)
{
int t;
t=a;
a=b;
b=t;
}
int main()
{
int x,y;
cout<<"Enter two numbers for swapping :";
cin>>x>>y;
cout<<"Values before swapping are:"<<x<<"\t"<<y<<"\n";
swap(x,y);
cout<<"Values after swapping are:"<<x<<"\t"<<y<<"\n";
return 0;
}
11.STUDENT DETAILS USING STRUCTURE
Aim:Write a c++ program to input student details such as roll number,name and mark and print the
student details
Program code
#include<iostream>
using namespace std;
struct student
{
int rollno;
char name[20];
int mark;
};
int main()
{
student s;
cout<<"Enter your roll number";
cin>>s.rollno;
cout<<"Enter your name";
cin>>s.name;
cout<<"Enter your mark";
cin>>s.mark;
cout<<"STUDENT DETAILS\n";
cout<<"----------------\n";
cout<<"ROLL NUMBER="<<s.rollno<<"\n";
cout<<"NAME="<<s.name<<"\n";
cout<<"MARK="<<s.mark;
return 0;
}