Counting and printing integers from user inputs using array, nested while and for loop
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows XP Pro SP2
Header file: Standard
Additional library: none/default
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: none
To do: Counting and printing integers from user inputs, which stored in array used together with nested while and for loop
To show: How to use the nested 'while', array and for loop in C programming
// Nested 'while' statements, for loop and array
#include <stdio.h>
// this program use an array variable
void main(void)
{
// array variable...
int arrange[5];
// normal variable
int count = 0, number = 0;
printf("\nPrompting you for 5 numbers\n");
printf("Each number should be from 1 to 10\n");
// while condition...
while(count<5)
{
// set the initial condition...
number = 0;
// another while condition...
while((number < 1) || (number > 10))
{
printf("Enter number %d of 5: ", count + 1);
/* scanf("%d", &number); */
scanf_s("%d", &number, 1);
}
// inner while loop stop here...
arrange[count] = number;
count++;
}
// outer while loop stop here...
// start the for loop for printing the result...
for (count = 0; count < 5; count++)
printf("\nValue %d is %d", count + 1, arrange[count]);
printf("\n");
}
Output example:
Prompting you for 5 numbers
Each number should be from 1 to 10
Enter number 1 of 5: 3
Enter number 2 of 5: 11
Enter number 2 of 5: 5
Enter number 3 of 5: 21
Enter number 3 of 5: 7
Enter number 4 of 5: 6
Enter number 5 of 5: 27
Enter number 5 of 5: 2
Value 1 is 3
Value 2 is 5
Value 3 is 7
Value 4 is 6
Value 5 is 2
Press any key to continue . . .