c/c++ hackerrank, my code is working but im getting 0,38/10 points(dunno why) code works -


so doing hackerrank coding challenges , begginer in programming. code working fine on visual studio , dev c++, hackerrank saying code working good. dont want tell me wrong, tell me there way better code(there probably) , possible put in loot without using if statement. here question : https://www.hackerrank.com/challenges/c-tutorial-for-loop here code: http://pastebin.com/nwicnyqy

#include <iostream> #include <cstdio> using namespace std;  int main() {    int i, n;    for(i=0;i<2;i++){      scanf("%d", &n);      if(n==1) {         printf("one \ntwo \nthree \nfour \nfive \nsix \nseven \neight \nnine \neven \nodd");     }     else if(n==2) {         printf("two \nthree \nfour \nfive \nsix \nseven \neight \nnine \neven \nodd");     }     else if(n==3) {         printf("three \nfour \nfive \nsix \nseven \neight \nnine \neven \nodd");     }     else if(n==4) {         printf("four \nfive \nsix \nseven \neight \nnine \neven \nodd");     }     else if(n==5) {         printf("five \nsix \nseven \neight \nnine \neven \nodd");     }     else if(n==6) {         printf("six \nseven \neight \nnine \neven \nodd");     }     else if(n==7) {         printf("seven \neight \nnine \neven \nodd");     }     else if(n==8) {         printf("eight \nnine \neven \nodd");     }     else if(n==9){         printf("nine \neven \nodd");     }    }    return 0; } 

this answer not fully answer question, give enough hints solve it.

you misread specifications of question: given 2 numbers a , b , need write down all numbers between a , b (bounds inclusive).

reading input

since need iterate on numbers between a , b, 1 better first reads these numbers:

int a, b; std::cin >> a; std::cin >> b; 

iterating on numbers

next have use for loop iterate on numbers n between a , b. each number have something:

for(int n = <lower>; n <= <upper>; n++) {     //do } 

action every n

as specified in question, each of these numbers, need write down something. if numbers between 1 , 9 (inclusive), need print english equivalent. long sequence of if(x == val) statements can replaced switch-case statement:

switch(n) {     case 1 :         //do 1         break;     case 2 :         //do 2         break;     //...     case 9 :         //do 9         break;     default :         //do if above didn't match         break; } 

if number less 1 or greater 9. program execute default case. in case, need check whether number even, or odd , print this. can using if statement:

if((n&0x01) == 0x00) {     //n even, } else {     //n odd, } 

by combining above patterns, can construct program solves problem.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -