Write a C program to print all numbers between a and b (a and b inclusive) using while loops.
Input Format:
Input consists of 2 integers. The first integer corresponds to a and the second integer corresponds to b.
Output Format:
Refer Sample Input and Output for formatting specifications.
[All text in bold corresponds to input and the rest corresponds to output]
Sample Input and Output 1:
Enter the value of a
10
Enter the value of b
4
10
9
8
7
6
5
4
Sample Input and Output 2:
Enter the value of a
4
Enter the value of b
10
4
5
6
7
8
9
10
Code:
#include<stdio.h> int main() { int a,b; printf("Enter the value of a\n"); scanf("%d",&a); printf("Enter the value of b\n"); scanf("%d",&b); while(1) { if(a>b){ printf("%d\n",a); --a;} else if(a<b){ printf("%d\n",a); ++a;} else if(a==b){ printf("%d\n",a); break; } } return 0; }
This comment has been removed by the author.
ReplyDelete#include
ReplyDeleteint main()
{
int a,b;
printf("Enter the value of a\n");
scanf("%d",&a);
printf("Enter the value of b\n");
scanf("%d",&b);
while(a<=b)
{
printf("%d\n",a);
a++;
}
while(a>=b)
{
printf("%d\n",a);
a--;
}
return 0;
}
output:
> Enter the value of a
> 5
> Enter the value of b
> 10
> 5
6
7
8
9
10
11
10
now why it goes to 11 and then 10 again?
Input : 9 9
ReplyDeleteGive Output : No Number
PRINT Numbers Within the RangeQUESTION DESCRIPTIONWrite a C program to print all numbers between a and b ( a and b inclusive) using a while loop.Input format:Input consists of 2 integers. The first integer corresponds to a and the second integer corresponds to b . Assume a>=b.Output format:Refer sample input and output for formatting specifications.TEST CASE 1INPUT8 1OUTPUT8 7 6 5 4 3 2 1TEST CASE 2INPUT9 9OUTPUTNo Number
ReplyDelete