Sie sind auf Seite 1von 7

Chapter 10 Flow of Control

Que1.Write a program to check whether a number is positive or not.

Code:

//Program to check whether a no. is positive or not

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int n;

cout<<"Enter the value of n";

cin>>n;

if(n>0)

cout<<"Positive number";

else

cout<<"Negative number";

getch();

The output will be:

Enter the value of n 12

Positive number
Que2. Write a program that accepts a number from user and checks whether it is an odd
number or not.

Code:

//Program to check whether a no. is odd or not

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int n;

cout<<"Enter the number";

cin>>n;

if(n%2==0)

cout<<"Number is even";

else

cout<<"Number is odd";

getch();

The output will be:


Enter the number 15

Number is odd

Que3. Write a program in C++ to check whether a person is eligible or not.

Code:

//

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int age;

cout<<"Enter your age : ";

cin>>age;

if(age>=18)

cout<<"You are eligible";

else

cout<<"You are not eligible";

getch();

The output will be:

Enter your age : 16


You are not eligible

Que4. Write a program that accept x and n from the user. And print the x raise to power n.
Also check whether n is +ve or not.

Code:

//

#include<iostream.h>

#include<conio.h>

#include<math.h>

void main()

clrscr();

int x,n,ans;

cout<<"Enter the value of x";

cin>>x;

cout<<"Enter the value of n";

cin>>n;

if(n>0)

cout<<pow(x,n);
else

cout<<"Power is negative";

getch();

The output will be:

Enter the value of x 2

Enter the value of n -7

Power is negative

Que5. Write a program to accept principal and time from the user. Find SI accordingly:

Principal Rate

>10000 20%

>7000 15%

>5000 8%

<=5000 5%

Code:

//

#include<iostream.h>

#include<conio.h>

void main()

clrscr();

int r,t,SI; long p;


cout<<"Enter principal amount";

cin>>p;

cout<<"Enter time period";

cin>>t;

if(p>10000)

r=20;

else if(p>7000)

r=15;

else if(p>5000)

r=8;

else

r=5;

SI=(p*r*t)/100;

cout<<"Simple interest is"<<SI;

getch();

The output will be:

Enter principal amount 9000

Enter time period 5

Simple interest is6750

Das könnte Ihnen auch gefallen