C++ | Condition statement if-else

Introduction

This is a introduction to if-else conditional statements.The first program prints the number in words. and second one name starting from given alphabet.The program can be extended by using more different alphabets & numbers.Go enjoy the program. Lets begin…………….

Program to print number in words

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type int
int a;
//Input the number save it in 'a'
cout<<"Enter the no."<<endl;
cin>>a;
//condition if-else statement
if(a==1)
{
//print one if a is 1
cout<<"One"<<endl;
}
//else
else
{
if(a==2)
{
//print two if a is 2
cout<<"Two"<<endl;
}
//else
else
{
if(a==3)
{
//print three if a is 3
cout<<"Three"<<endl;
}
//else
else
{
if(a==4 || a==5)
{
//print four or five if a is 4 or 5
cout<<"Number may be four or five"<<endl;
}
//else
else
cout<<"Invalid input"<<endl;
}
}
}
//get character
getch();
}

Output

Enter the no.
2
Two

or

Enter the no.
5
Number may be four or five

or

Enter the no.
0
Invalid input

Program to print A for apple

#include<iostream.h>
#include<conio.h>
void main()
{
//clear the screen.
clrscr();
//declare variable type char
char a;
//Input the alphabet save it in 'a'
cout<<"Enter the alphabet"<<endl;
cin>>a;
//condition if-else statement
if(a=='A')
{
//print apple if a is A
cout<<"apple"<<endl;
}
//else
else
{
if(a=='B')
{
//print ball if a is B
cout<<"ball"<<endl;
}
//else
else
{
if(a=='C')
{
//print cat if a is C
cout<<"cat"<<endl;
}
//else
else
{
if(a=='D' || a=='E')
{
//print doll or elephant if a is D or E
cout<<"doll or elephant"<<endl;
}
//else
else
cout<<"Invalid input"<<endl;
}
}
}
//get character
getch();
}

Output

Enter the alphabet
A
apple

or

Enter the alphabet
D
doll

or

Enter the alphabet
H
Invalid input

How does it work

  1. You enter the number or alpahbet  in and is saved in ‘a’variable.
  2. If a is 1 prints one.
  3. If a is 2 prints two
  4. Same way for other numbers.

Extending it

The program can be extended by using different numbers and alpabhbets .You can take the inputs and print it accordingly.

Explanation.

  1. Include ‘iostream.h’ and ‘conio.h’ files.
  2. Add void main.
  3. Start program by first clearing the screen.
  4. Declare the variables as int or char (name them as you want.)
  5. Add cout and cin of variable of inputs.(as shown in program)
  6. Add the conditional statement if-else.
  7. Cout the name or word.

At the end

You learnt creating the c++ program of Conditional statement if-else and printing the names and number in words according to if-else conditional statement. So now enjoy the program.

Please comment on the post and share it.
And like it if you liked.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s