What you learn from the problem
- This solution will help you To learn how to use Conditional Statements in C++.
- if else and if else-if are the two of the most used conditions in c and c++,this two conditional statements are mostly used in c and c++.
Conditional Statements in C++
SYNTAX
if conditional statement
if(condition)
{
statement1;
}
- if the condition becomes true the statement will true ,if the condition become false program will terminates.
if -else conditional statement
if(condition)
{
statement1;
}
else
{
statement2;
}
if the condition becomes true the 1st statement will true else the statement2 will true
if -else-if else conditional statement
- if - else if - else: In this structure, dependent statements are chained together and the for each statement is only checked if all prior conditions in the chain evaluated to false.
- Once a evaluates to true, the bracketed code associated with that statement is executed and the program then skips to the end of the chain of statements and continues executing.
- If each in the chain evaluates to false, then the body of bracketed code in the else block at the end is executed.
if(condition)
{
.....
}
else if (second condition)
{
..........
}
else if (statement ...)
{
......
}
else
{
}
Input Format
Constraints
Output Format
If , ,, then print the lowercase English word corresponding to the number (e.g., one for , two for , etc.); otherwise, print Greater than 9.
Sample input
Sample Output 0
Explanation 0
Sample Input 1
Sample Output 1
Explanation 1
Sample Input 2
Sample Output 2
Disclaimer :-
the hole problem statement are given by HackerRank.com but the solution are generated by the ProgrammingRam authority if any of the query regarding this post or website fill the following contact form thank you.
Explanation about problem
- ---> first of all you have to take inputs from the
- ---> keyboard if the number entered is between
- ---> 1 to 9 then you have to display the number in
- ---> word else if the number greater than 9 is
- ---> entered you have to display Greater than 9
Coding
#include<iostream>
using namespace std;
int main()
{
int n;
cin>>n;
if(n==1)
{
cout<<"one";
}
else if(n==2){
cout<<"two";
}
else if(n==3){
cout<<"three";
}
else if(n==4){
cout<<"four";
}
else if(n==4){
cout<<"four";
}
else if(n==5){
cout<<"five";
}
else if(n==6){
cout<<"six";
}
else if(n==7){
cout<<"seven";
}
else if(n==8){
cout<<"eight";
}
else if(n==9){
cout<<"nine";
}
else {
cout<<"Greater than 9";
}
return 0;
}
Explanation of solution
- ---> first of all add the required header file then start of main function after
- that we declare integer type n and take this input from by using cin>>n.
- ---> Then here we use the conditional statement in c++ and check if n==1
- print 'one' and again check n==2 if yes then print 'two' like this we continue
- till nine if n==9 then print 'nine'.
- ---> Then here we check if user enter the number greater than 9
- we have to print "Greater than 9" that we have written in the
- else condition that will print in last.
Also check Out
Also check Out Projects for Resume
- ---> And that is for this article friends hope all of you enjoy this
- article if still you have any doubts please comment below
- i will try to solve that .
Post a Comment
Post a Comment
Please do not spam in comment