- Prepare
- C
- Conditionals and Loops
- For Loop in C
- Discussions
For Loop in C
For Loop in C
+ 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; }
+ 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 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 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; }
+ 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; }
Sort 692 Discussions, By:
Please Login in order to post a comment