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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. C
  3. Conditionals and Loops
  4. For Loop in C
  5. Discussions

For Loop in C

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 692 Discussions, By:

recency

Please Login in order to post a comment

  • anthonyoriebir
    1 week ago+ 0 comments
    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    
    int main() 
    {
        int a, b, i; 
        char* words[9] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        
        
        scanf("%d\n%d", &a, &b);
             for(i = a; i <= b; i++)
                if (i <=9 ){
                printf("%s\n", words[i-1]);
                }else{   
                    switch (i % 2) {
                        case 0:
                            printf("even\n");
                            break;
                        default:
                            printf("odd\n");
                            break;
                        }
                    }
        
       
        return 0;
    }
    
    2|
    Permalink
  • vkp035127
    1 week ago+ 0 comments

    include

    int main() { int a,b; scanf("%d",&a); // printf("\n"); scanf("%d",&b); int i; for(i=a;i<=b && i>=a;i++) { if(i>=1 && i<=9) { if(i==1) printf("one\n"); if(i==2) printf("two\n"); if(i==3) printf("three\n"); if(i==4) printf("four\n"); if(i==5) printf("five\n"); if(i==6) printf("six\n"); if(i==7) printf("seven\n"); if(i==8) printf("eight\n"); if(i==9) printf("nine\n");

        }
        if(i>9 && i%2==0)
        printf("even\n");
        if(i>9 && i%2!=0)
        printf("odd\n");
    }
    return 0;
    

    }

    0|
    Permalink
  • yssnabbou2002
    2 weeks ago+ 0 comments

    include

    include

    int main() { int a,b,i; char tab[][9]={"one","two","three","four","five","six","seven","eight","nine"}; do{ printf("Enter an integer a :"); scanf("%d",&a); printf("Enter an integer b :"); scanf("%d",&b); }while(a>b);

    for(i=a;i<=b;i++)
    {
        if(i>=1 && i<=9)
        {
           printf("%s\n",tab[i-1]);
        }
        else if(i%2==0)
            printf("even\n");
        else if(i%2!=0)
            printf("odd\n");
    }
    
    
    return 0;
    

    }

    0|
    Permalink
  • ptmkhanh29
    2 weeks ago+ 0 comments

    C

    int main() 
    {
        int a, b;
        char *words[] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        //printf("Please input interval [a,b]:\n");
        scanf("%d\n%d", &a, &b);
      	// Complete the code.
        //printf("Please input integer N: \n");
        //scanf("%d", &n);
        for(int i = a; i <= b; i++){
            if(i > 9){
                if(i%2 == 0) printf("even\n");
                else printf("odd\n");
            }
            else if(i >= 1 && i < 9){
                int mod = i%9;
                printf("%s\n", words[mod-1]);
            }
            else printf("nine\n");
        }
        return 0;
    }
    
    0|
    Permalink
  • mohmkad200
    3 weeks ago+ 1 comment

    I wanted to print it out like this printf("%s",a<=9?num[a-1]: a%2==0?"even\n":"odd\n" but it does not work, do you know why?

    #include <stdio.h>
    #include <string.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    
    int main() 
    {
        int a, b;
        scanf("%d\n%d", &a, &b);
      	// Complete the code.
          char *num[]={"one","two","three","four","five","six","seven","eight","nine"};
        for(a;a<=b;a++)
        {
            if(a<=9)
                printf("%s\n", num[a-1]);
            else{
                if(a>9)
                    if(a%2==0)
                        printf("even\n");
                    else
                        printf("odd\n");
            }    
        }
    
        return 0;
    }
    
    -1|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy