C Programming Code Examples C > For Loops and While Loops Code Examples Program to Read Input Until User Enters a Positive Integer Program to Read Input Until User Enters a Positive Integer The positive numbers 1, 2, 3... are known as natural numbers. The programs below takes a positive integer (let say n) as an input from the user and calculates the sum up to n. #include <stdio.h> int main() { int n, j, sum = 0; do { printf("Enter a positive integer: "); scanf("%d",&n); } while (n <= 0); for(j=1; j <= n; ++j) { sum += j; // sum = sum+j; } printf("Sum = %d",sum); return 0; }