Write a program to print out the numbers 10 through 49 in the following manner:
ID: 3546082 • Letter: W
Question
Write a program to print out the numbers 10 through 49 in the following manner:
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
How would you do it? Here is an example of poorly written code:
for ( int i = 10; i < 50; i++ ) {
switch (i) {
case 19:
case 29:
case 39:
System.out.println(" " + i); // move to the next line
Break;
default: System.out.print(" " + i);
}
}
This code is not written well because it works only for printing 10 through 49. Try to develop the code so that it can be extended easily to handle any range of values. You can do this coding in two ways: with a nested-for statement or with modulo arithmetic. (If you divide a number by 10 and the remainder is 9, then the number is 9, 19, 29, or 39, and so forth.)
Explanation / Answer
#include<stdio.h>
int main()
{
int a,b;
scanf("%d%d", &a,&b);
int temp=a;
while(1)
{
printf("%d ",temp);
if(temp==b)
break;
if(temp%10==9)
printf(" ");
temp++;
}
printf(" ");
return 0;
}
Input first number and the second number in the prompt :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.