We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

In an "If statement" what will happen if we directly give "even".Instead of giving through an array?
Example:
int i,b,c;
string a[11]={"odd","one","two","three","four","five","six","seven","eight","nine"};
cin>>c>>b;
for(i=c;i<=b;i++)
{
if((i>9) && (i%2==0))
cout<<"even"<

not clicking in my head why the else statement works. I thought the else block executes if the two previous conditions are both false no? I would have thought the cout << a[i+1] would be apart of the other two code blocks.

you have to remove the break statements otherwise you will have only one output ( of the matching case ).
if you skip the break statements you will get all the outputs after your matching statement

#include<iostream>#include<cstdio>usingnamespacestd;intmain(){// Complete the code.inta,b;intn=0;stringintMap[9]={"one","two","three","four","five","six","seven","eight","nine"};cin>>a>>b;if((a<=9)&&(b<=9)){for(n=a;n<=b;n++){cout<<intMap[n-1]<<endl;}}elseif((a<=9)&&(b>9)){for(n=a;n<=9;n++){cout<<intMap[n-1]<<endl;}for(n=10;n<=b;n++){if(n%2==0){cout<<"even"<<endl;}else{cout<<"odd"<<endl;}}}else{for(n=a;n<=b;n++){if(n%2==0){cout<<"even"<<endl;}else{cout<<"odd"<<endl;}}}return0;}

//Compares the values of a, b with 9 and if values <= 9 then//Prints all values from 'a' uptill 'b' in string formatif((a<=9)&&(b<=9)){for(n=a;n<=b;n++){cout<<intMap[n-1]<<endl;}}//Compares the values of a, b with 9//And if the value of a <= 9 and value of b > 9 then....elseif((a<=9)&&(b>9)){//Prints all the values starting from 'a' uptill 9//in string format and then....for(n=a;n<=9;n++){cout<<intMap[n-1]<<endl;}//For values which are greater than 9 and <= to 'b'//Prints 'even' or 'odd' for themfor(n=10;n<=b;n++){if(n%2==0){cout<<"even"<<endl;}else{cout<<"odd"<<endl;}}}Hopethathelps.

for(n=10;n<=b;n++)
checking the value of n is less than b
if(n%2==0)
suppose the value of n you given is 10
if statement will find the remainder by using the modulus %
that is n%2
10%2 just divide the reminder will always be zero which means if its divisible by 2 it is even number..

you have a for loop and it start counting from 10 , after check the condition if n is inferior or egal to b , when it is true it increment the value of n ...
if n%2 == 0 , means if n is divisible by 2 , he will display even
I hope it helps you :-)

can u please explain the string type in C++ .I searched it over net but didn't got the satisfying result. According to me String is an array of char but you used it as a predefined data-type.how?

After your "else if", your "else" statement can be skipped. It will pass all tests cases without. Just saying...
Your code is great by the way and pass all tests cases anyway ;-)

You do if (a % 2 == 0) I disagree in 2 ways:
1) a isn't a good variable name.
2) modulo is slower than & (aka bitand)

if (number & 1 != 0) number is odd. It's binary representation ends in 1.
Of course this, afaik, only works with 2's complement numbers (for negative nums) which is only standard in every single cpu available in the last 40 years.

Who knows… Maybe IBM will release another sign magnitude cpu and when it doesn't sell you can keep on using my technique. ._.

## For Loop

You are viewing a single comment's thread. Return to all comments →

## include

## include

using namespace std; int main() { int a,b; cin>>a>>b; for(a;a<=b;a++) {

if(a<=9) { if(a==8) cout<<"eight\n"; else if(a==9) cout<<"nine\n"; else if(a==7) cout<<"seven\n"; else if(a==6) cout<<"six\n"; else if(a==5) cout<<"five\n"; else if(a==4) cout<<"four\n"; else if(a==3) cout<<"three\n"; else if(a==2) cout<<"two\n"; else if(a==1) cout<<"one\n"; } else {

} return 0; }

its easier and better looking to use a switch statement: switch (count){ case 1: cout << "one" << endl; break; case 2: cout << "two" << endl; break; case 3: cout << "three" << endl; break; case 4: cout << "four" << endl; break; case 5: cout << "five" << endl; break; case 6: cout << "six" << endl; break; case 7: cout << "seven" << endl; break; case 8: cout << "eight" << endl; break; case 9: cout << "nine" << endl; break; default: if (count%2 == 0) cout << "even" << endl; else cout << "odd" << endl;

exactly...but here the quesion is realted to loop so we should ans according to the question.

but the questionnis looking for the numbers(in words) in order. so if you use break statement they wont outcome in order.

This is my code

nice bruh!

in your code why are u useing the variable c,b;

b and c are variables to take 2 inputs.

actually its use for take input.

thanks man i was missing somnething out. my code is the same. arrays solved it in some lines of code!

What if b>c?? it wont work for b>c test case

Easy, just add a while loop and take input for C before B. While b is greater than c, do cin again until it's false.

Or just swap them if(b>c)

or just use switch case.. code is here.. https://github.com/nick-mehta/hackerrank/blob/master/for%20loop.cpp

that's great man, you have clarified my doubt. thanks a lot

thnkx

perfect....!

nice code

thanks bro u cracked it

In an "If statement" what will happen if we directly give "even".Instead of giving through an array? Example: int i,b,c; string a[11]={"odd","one","two","three","four","five","six","seven","eight","nine"}; cin>>c>>b; for(i=c;i<=b;i++) { if((i>9) && (i%2==0))

cout<<"even"<

i dont understand this code.From where variable c and b start.can u explain me?

sweet

sweet

the initializer for string didn't work out with me

worked for me. https://freeunusedeshop.codes/

Nice work!

Nice

A simplified way

right code but one condition is missing.. after the first else give another "if" which is n>9...so that it gives the required output....

use swich case like this https://github.com/nick-mehta/hackerrank/blob/master/for%20loop.cpp

very very goog bro!

Why do we use num[n-1], likw why do we use n-1 and why not n?

because array index starting from 0 and to print one we must have to print 0th elements thats it.

but where did you declare the variable 'n' and ''sum''?

thank u so much.

Great job

nice code bro

Very slight improvement to your if-then-else:

this is simple logic

thanks

cool

most effective and presicely written

that's amazing :D

good idea bro

but we have to print a also but your code starts printing next to a??? eg if a=7 then your code starts printing eight nine and so on

read question carefully we have to print each interval between a and b

great

Genius

amazing algorithm use bruh! I was making this code so long.

awsome brother....

not clicking in my head why the else statement works. I thought the else block executes if the two previous conditions are both false no? I would have thought the cout << a[i+1] would be apart of the other two code blocks.

Why do you add the c and b?

why are your output giving only : even odd eight nine

tell me please.

try this code https://github.com/nick-mehta/hackerrank/blob/master/for%20loop.cpp

thanks bro !

nice approach bro!

can anyone explain me this code please

Nice

bahut sahi bnaya bro

great..!!

nice implementation :0

very good solution bro actully i am trying it without adding even and odd in the array

you have to remove the break statements otherwise you will have only one output ( of the matching case ). if you skip the break statements you will get all the outputs after your matching statement

thanks this works in c++. I get problems with cin undclared if I used C

same here

c dosnt hace cin u should use scanf

Great code, I like it.

Here is mine:

for(n=10;n<=b;n++){

if (n%2==0){ cout<<"even"<

why you have used intmap[n-1]

thank you so much...

for(n=10;n<=b;n++) checking the value of n is less than b if(n%2==0) suppose the value of n you given is 10 if statement will find the remainder by using the modulus % that is n%2 10%2 just divide the reminder will always be zero which means if its divisible by 2 it is even number..

you have a for loop and it start counting from 10 , after check the condition if n is inferior or egal to b , when it is true it increment the value of n ... if n%2 == 0 , means if n is divisible by 2 , he will display even I hope it helps you :-)

what is the use of String intMap[]

can u please explain the string type in C++ .I searched it over net but didn't got the satisfying result. According to me String is an array of char but you used it as a predefined data-type.how?

are you askin about its library file? If that is true,

## include

Love your code but arrays havent been introduced here yet! so why did u use it? Cant it be done without arrays?

After your "else if", your "else" statement can be skipped. It will pass all tests cases without. Just saying... Your code is great by the way and pass all tests cases anyway ;-)

Much simple, try this.

it helped

sir, can you explain in easy way, please

Unnecessarily making it so long and unreadable.

I think it is better than writing multiple "if-else" and one who can code can read it without any trouble.

what for the values when a and b both are greater than 9

pretty long one.

exactly wwhat i did nut there are slightly better codes available we wont need those many if else and for loops inside.

This works but is not correct, it will not work if the numbers greater than 9 are fewer than the numbers that are les than or equal to 9.

for example a=3 b=5 out put would be; three four five

no odds or evens

thank you http://freexboxlivecodes2019.com/

Thanks a lot

Don't waste your time typing all that out

you could also use an arry insted on usong all these lines

tu chutia hai sale bahenchod

thanks

You do

`if (a % 2 == 0)`

I disagree in 2 ways: 1)`a`

isn't a good variable name. 2) modulo is slower than`&`

(aka`bitand`

)`if (number & 1 != 0)`

number is odd. It's binary representation ends in 1. Of course this, afaik, only works with 2's complement numbers (for negative nums) which is only standard in every single cpu available in the last 40 years.Who knows… Maybe IBM will release another sign magnitude cpu and when it doesn't sell you can keep on using my technique. ._.