Sei sulla pagina 1di 20

2.

/*Write a program to enter marks of a student and display grade and message.*/

#include <conio.h>
#include <iostream.h>

void main()
{
clrscr();
float marks;
cout<<”Enter the marks of the student\n”;
cin>>marks;
if(marks<=100 && marks>=85)
cout<<”Grade – A\nDistinction”;

else if(marks<85 && marks>=65)


cout<<”Grade – B\nGood”;

else if(marks>=0 && marks<65)


cout<<”Grade – C\nAverage”;

else
cout<<”Invalid Input”;

getch();
}

1.
4.
/*Write a menu driven program using switch case to calculate the area of circle,
square, rectangle and triangle.*/

#include <conio.h>
#include <iostream.h>

void main()
{
clrscr();
float pi=3.14, r, s, l, b, h, bot;
int n;
cout<<“What would you like to find out?\n”<<“1. Area of Circle\n”<<“2. Area of
Square\n”<<“3. Area of Rectangle\n”<<“4. Area of Triangle\n”;
cin>>n;
switch(n)
{
case 1 : cout<<“Enter the Radius of the Circle\n”;
cin>>r;
cout<<“Area of the circle is ”<<pi*r*r;
break;

case 2 : cout<<“Enter the Side of the Square\n”;


cin>>s;
cout<<“Area of the Square is ”<<s*s;
break;

case 3 : cout<<“Enter the Length and Breadth of the Rectangle\n”;


cin>>l>>b;
cout<<“Area of the Rectangle is ”<<l*b;
break;

case 4 : cout<<“Enter the Base and Height of the Triangle\n”


cin>>bot>>h;
cout<<“Area of the Triangle is ”<<bot*h*0.5;
break;

default : cout<<“Invalid input”;


break;
}
getch();
}

3.
6.
/*Write a program to input n-digit number and check if it is Armstrong*/

#include <conio.h>
#include <iostream.h>

void main()
{
clrscr();
long int a, n, s=0, m;
cout<<"Enter the Number\n";
cin>>n;
m=n;
while(n>0)
{
a = n%10;
s = s + (a*a*a);
n=n/10;
};
if(s==m)
cout<<"It is an Armstrong Number";

else
cout<<"It is not an Armstrong Number";

getch();
}

5.
8.
/*Write a program to input a number and print its reverse*/

#include <conio.h>
#include <iostream.h>

void main()
{
clrscr();
long int a, n, s=0, m;
cout<<"Enter the Number\n";
cin>>n;
m=n;
while(m>0)
{
if(m%10==0)
cout<<0;
m=m/10;
}
while(n>0)
{
a = n%10;
s = s*10 + a;
n=n/10;
};
cout<<s;
getch();
}

7.
10.
/*Write a program to find the sum of all odd and sum of all even numbers in the
range of 1 - 50 */

#include <conio.h>
#include <iostream.h>

void main()
{
clrscr();
int i, se=0,so=0;
for(i=1;i<=50;i++)
{
if(i%2==0)
se=se+i;
else
so=so+i;
};
cout<<"The sum of Even numbers between 1 and 50 is "<<se;
cout<<"\nThe sum of Odd numbers between 1 and 50 is "<<so;
getch();
}

9.
12.
/*Write a program to find Factorial of a number by using while loop */

#include <conio.h>
#include <iostream.h>

void main()
{
clrscr();
int n, f=1;
cout<<"Enter the number\n";
cin>>n;
while(n!=0)
{
f=f*n;
n=n-1;
};
cout<<"The Factorial is "<<f;
getch();
}

11.
14.
/*Write a program to display the Fibonacci Series 0 1 1 2 3 5 8 13 ...... N terms */

#include <conio.h>
#include <iostream.h>
#include <process.h>

void main()
{
clrscr();
int i, n, t1=0, t2=1, t3;
cout<<"Enter the number of terms\n";
cin>>n;
if(n==1)
{
cout<<t1;
getch();
exit(0);
}
else if(n==2)
{
cout<<t1<<" "<<t2;
getch();
exit(0);
}
else
for(i=1;i<=n;i++)
{
if(i==1)
cout<<t1<<" ";
if(i==2)
cout<<t2<<" ";
t3=t1+t2;
t1=t2;
t2=t3;
cout<<t3<<" ";
};
getch();
}

13.
16.
/*Write a program to print all the prime number between 1 - 50 */

#include <conio.h>
#include <iostream.h>

void main()
{
clrscr();
int i, j, a=1;
for(i=1;i<=50;i++)
{
for(j=2;j<=(i/2);j++)
{
if(i%j==0)
a=0;
}
if(a==1)
cout<<i<<"\n";
}

getch();
}

15.
/*Write a program to find 1 – x + x2/2! – x3/3! + ……….. N terms*/
#include <iostream.h>
#include <conio.h>
#include <math.h>

void main()
{
clrscr();
int x,i,n,j,fac,sum=0;
cout<<"Enter the value of x\n";
cin>>x;
cout<<"Enter the number of terms you want to print\n";
cin>>n;
for(i=0;i<n;i++)
{
fac=1;
for(j=1;j<=i;j++)
{
fac=fac*j;
}
if(i%2==0)
sum=sum+(pow(x,i)/fac);
else
sum=sum-(pow(x,i)/fac);
}
cout<<"The sum is "<<sum;
getch();
}

17.
18.
\*Print the pattern 1 */
12
123
1234

#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();
int I, j;
for(i=1;i<=4;i++)
{
for(j=1;j<=5-I;j++)
cout<<” “;

for(j=1;j<=I;j++)
cout<<j<<” “;
cout<<”\n”;
}
getch();
}

19.
20.

Potrebbero piacerti anche