Exploring C Loops: Printing Numbers from 1 to N Using 'while' Loop
#include <stdio.h>
int main()
{
// Declare variables to store user input and loop counter
int a, b = 1;
// Prompt the user to enter how many digits they want
printf("How many digits do you want: \n");
// Read the input from the user
scanf("%d", &a);
// Loop to print numbers from 1 to the user-defined number 'a'
while (a >= b)
{
// Print the current value of 'b'
printf("%d \n", b);
// Increment 'b' to move to the next number
b++;
}
return 0;
}
SAME THING CAN BE DONE BY USING THIS ALSO
#include <stdio.h>
int main()
{
// Declare a variable to store the user's input
int a;
// Prompt the user to enter a digit
printf("Enter a digit: ");
// Read the input from the user
scanf("%d", &a);
// Loop to print numbers from 1 to the user-defined number 'a'
for (int b = 1; a >= b; b++)
{
// Print the current value of 'b'
printf("%d \n", b);
}
return 0;
}
Comments
Post a Comment