Вы находитесь на странице: 1из 17

CHAPTER 10 – FLOW OF CONTROL

17) Write a C++ program to find out whether a year (entered in 4-digit number
representing it) is a leap year.
#include<iostream.h>
#include<conio.h>
void main()
{
int year;
cout << "Enter a year: ";
cin >> year;
if (year%4 == 0) {
if (year%100 == 0) {
if (year%400 == 0) {
cout << year << " is a leap year.";
}
else {
cout << year << " is not a leap year.";
}
}
else {
cout << year << " is a leap year.";
}
}
else {
cout << year << " is not a leap year.";
}
getch();
}
Sample Output:-
18) Write a C++ program to find the factorial of a number.
#include<iostream.h>
#include<conio.h>
void main()
{

int num,factorial=1;
cout<<" Enter Number To Find Its Factorial: ";
cin>>num;
for(int a=1;a<=num;a++)
{
factorial=factorial*a;
}
cout<<"Factorial of Given Number is ="<<factorial<<endl;
getch();
}

Sample Output:-
19) Write a C++ program to create a simple calculator for addition, subtraction,
multiplication and division using switch...case statement.
#include<iostream.h>
#include<conio.h>
void main()
{ char op;
float num1,num2;
cout << "Enter operator either + or - or * or /: ";
cin >> op;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch(op) {
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
default:
cout << "Error! operator is not correct";
break;
}
getch();
}
Sample Output:-
20) Write a C++ program to calculate and print roots of a quadratic equation ax2+bx+c = 0
(a ≠ 0).
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{ float a, b, c, x1, x2, determinant, realPart, imaginaryPart;
cout << "Enter coefficients a, b and c: ";
cin >> a >> b >> c;
determinant = b*b - 4*a*c;

if (determinant > 0) {
x1 = (-b + sqrt(determinant)) / (2*a);
x2 = (-b - sqrt(determinant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}

else if (determinant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(determinant)) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-determinant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart << "i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart << "i" << endl;
}
getch();
}
Sample Output:-
21) Write a short program to check whether square root of a number is prime or not.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,m,f=0;
cout<<"Enter n: ";
cin>>n;
m=sqrt(n);
cout<<"\t"<<m;
for(int i=2;i<m/2;i++)
if(m%i==0)
{
f=1;
goto lb;
}
lb:
if(f==0)
{
cout<<"Prime";
}
else
{
cout<<"Not Prime";
}
getch();
}

Sample Output:-
22) Write a short program to print first n odd numbers in descending order.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
int n,m;
cout<<"Enter n: ";
cin>>n;
for(int i=n;i>=0;i--)
{
if(i%2!=0)
{
cout<<i<<endl;
}
}
getch();
}

Sample Output:-
23) Give the four sides of a rectangle. Write a program to find out whether its area is
greater than its perimeter.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float l,b,area,peri;
cout<<"Enter length and breadth: ";
cin>>l>>b;
area=l*b;
peri=2*(l+b);
cout<<"Area= "<<area;
cout<<"Perimeter= "<<peri;
if(area>peri)
{
cout<<"Area is greater than perimeter";
}
else
cout<<"Area is not greater than perimeter";
getch();
}

Sample Output:-
24) Write a short program to print the following series:
(i) 1 4 7 10 . . . . . . . 40.
(ii) 1 -4 7 -10 . . . . . . -40.

(i)
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
for(int i=1;i<=40;i+=3)
{
cout<<"\t"<<i;
}
getch();
}
(ii)
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
for(int i=1;i<=40;i+=3)
{
if(i%2!=0)
{
cout<<"\t"<<i;
}
else
{
cout<<"\t-"<<i;
}
}
getch();
}

Sample Output:-
25) Write a short program to find whether the given character is digit or a letter.

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
char ch;
cout<<"Enter a character: ";
cin>>ch;
if(ch>=48 && ch<=57)
cout<<"\n"<<"You entered a digit";
else if((ch>=65 && ch<=90) || (ch>=97 && ch<=122))
cout<<"\n"<<"You entered a letter";
else
cout<<"\n"<<"You entered a special character";
getch();
}

Sample Output:-
26) Write a short program to convert a lowercase character to uppercase.
(Hint: Subtracting 32 from a lowercase character gives you equivalent uppercase
character e.g., ‘b’ – 32 will give
you ‘B’ and ‘W’ – 32 will give you ‘W’).

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
char ch,ch2;
cout<<"Enter a lowercase character: ";
cin>>ch;
ch2=ch-32;
cout<<"\t"<<ch2;
getch();
}

Sample Output:-
27) Write a short program to find average of list of numbers entered through keyboard.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,n[10],sum=0;
float avg=0;
for(i=0;i<10;i++)
{
cout<<"Enter no."<<i<<": ";
cin>>n[i];
sum+=n[i];
}
cout<<endl<<"Sum="<<sum;
avg=(float)sum/10;
cout<<"Average: "<<avg;
getch();
}

Sample Output:-
28) Write a short program to
(i) calculate area of a triangle
(ii) find the radius of a circle whose area is given
(iii) calculate the volume and area of a sphere (volume = 4/3πr3 and Area = 4πr2 )
(i)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float area,a,b,c,s;
cout<<"Enter three sides of triangle: ";
cin>>a>>b>>C;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area= "<<area;
getch();
}
(ii)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"Enter area:";
double A;
cin>>A;
double r = pow(7 * A / 22, 0.5);
cout<<"Radius of circle is:"<<r;
getch();
}
(iii)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float r,v,a;
cout<<"Enter radius";
cin>>r;
v=(4/3)*3.14*r*r*r;
a=4*3.14*r*r;
cout<<"Volume = "<<v<<endl;
cout<<"Area = "<<a<<endl;
getch();
}

Sample Output:-
29) Write a short program to find largest number of a list of numbers entered through
keyboard.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int m,a[10],i;
for(i=0;i<10;i++)
{
cout<<"Enter no."<<i<<": ";
cin>>n[i];
}
m=a[10];
for(i=0;i<10;i++)
{
if(a[i]>m)
{
m=a[i];
}
}
cout<<"Largest = "<<m;
getch();
}

Sample Output:-
30) Write a program using nested loops to produce the following designs:
(i)

A
AB
ABC
ABCD
ABCDE
ABCDEF
(ii)
&&&&&&&
& &&&&
& &&
&
(a)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char ch='A';
int n;
cout<< "\n Enter the height of the triangle :";
cin>> n;
for(int i=1; i< = n ; i++)
{
ch = 'A';
for(int j = 1; j< = i; j++)
{
cout<< ch<< " ";
ch++;
}
cout<< "\n";
}
getch();
}
(b)
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
clrscr();
cout<<"\n Enter height of a triangle :";
cin>>n;
for(int i = n, sp = 0; i >0 ; i --, sp++)
{
for(int k=0; k<=sp; k++)
cout<<" "<<" ";
for( int j = 1; j < 2*i ; j++)
{
cout << "&" << " ";
}
cout<<"\n";
}
getch();
}

Sample Output:-

31) Write a program using nested loops to produce a rectangle of *’s with 6 rows and 20
*’s per row.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
for(int i=1;i<=6;i++)
{
for(int j=1;j<=20;j++)
{
cout<<"*";
}
cout<<endl;
}
getch();
}

Sample Output:-
32) Write a C++ program to print Fibonacci series i.e., 0 1 1 2 3 5 8 13...........
#include<iostream.h>
#include<conio.h>
void main()
{
int n, c, first = 0, second = 1, next;
 
cout << "Enter the number of terms of Fibonacci series you want" << endl;
cin >> n;
 
cout << "First " << n << " terms of Fibonacci series are :- " << endl;
 
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
cout << next << endl;
}
  getch();
}

Sample Output:-
33) Write a C++ program to check whether a number is palindrome or not.
#include<iostream.h>
#include<conio.h>
void main()
{
int n, num, rem, rev = 0;
cout << "Enter a positive number: ";
cin >> num;
n = num;
do
{
rem = num%10;
rev = (rev*10) + rem;
num = num/10;
}while (num!=0);
cout << " The reverse of the number is: " << rev << endl;
if (n==rev)
cout << " The number is a palindrome";
else
cout << " The number is not a palindrome";

getch();
}
Sample Output:-

Вам также может понравиться