Here, we’ll use while loop to compute the sum of a few number series. (You can see part 4 of the while loop tutorial here.)







Q.1. Write a C program to compute 1 + 2 + 3 + … + n, using while loop. Here, n (>=1) is input given by the user.
Answer.
//Program to compute the sum of natural numbers from 1 to n, using while loop. //Input - n (n>=1) //Output - the desired sum //Logic - use while loop to iterate the loop counter i from 1 to n. Start with the running sum//as 0, and increment it by the loop counter in each iteration. <stdio.h> int main(){ int sum = 0, i = 1, n; //initially, the running sum is 0. //loop starts at 1, hence i is set to 1. //n is the input given by the user printf("Pls enter a number (>=1): "); //prompt the user to enter a number scanf("%d", &n); //get the input from the user, and save it in the variable n. while (i <= n) //need to compute sum from 1 to n { sum += i; //each time we encounter a number, we add it to our running sum i++; //and we increment the number by 1. } //at this point, the variable 'sum' contains the sum 1 + 2 + 3 + ... + n printf("The desired sum is %d \n", sum); return 0; }
sample output 1: Pls enter a number (>=1): 5The desired sum is 15sample output 2: Pls enter a number (>=1): 6The desired sum is 21
Explanation:
– We need to compute the sum 1 + 3 + 5 + … + n
– We start the while loop at i = 1. (And set the initial sum to 0.) i keeps getting incremented by 1 in each iteration of the loop. In each iteration, we add the value of i to the running sum. We exit the loop when i goes beyond n.
When we exit the loop, the running sum contains the value of 1 + 2 + 3 + .. + n.
Q.2. Compute the sum of odd numbers till n using while loop. [That is, compute the sum 1 + 3 + 5 + …. + n (or n-1).]
Answer.
//Program to compute the sum of odd numbers from 1 to n, using while loop. //Input - n (n>=1) //Output - 1 + 3 + 5 + ... + n (if n is odd) // 1 + 3 + 5 + ... + (n-1) (if n is even)//Logic - use while loop to iterate the loop counter i from 1 to n. Start with the running sum//as 0, and increment it by the loop counter in each iteration. Loop counter is incremented by // 2 in each iteration (to get to the next odd number) <stdio.h> int main(){ int sum = 0, i = 1, n; //initially, our sum is 0 //loop starts at 1, hence i is set to 1. //n is the input given by the user printf("Pls enter a number (>=1): "); //prompt the user to enter a number scanf("%d", &n); //get the input from the user, and save it in the variable n. while (i <= n) //need to compute sum from 1 to n { sum += i; //each time we encounter a number, we add it to our running sum i += 2; //and we increment the number by 2 (to get to the odd number) } //at this point, the variable 'sum' contains the sum 1 + 3 + ... + (n or n-1) printf("The desired sum is %d \n", sum); return 0; }
sample output 1: Pls enter a number (>=1): 5The desired sum is 9sample output 2: Pls enter a number (>=1): 6The desired sum is 9sample output 3: Pls enter a number (>=1): 7The desired sum is 16sample output 4: Pls enter a number (>=1): 8The desired sum is 16
Explanation –
– This is very similar to the previous program. In the previous program, we computed 1 + 2 + 3 + … + n. Here, we compute 1 + 3 + 5 + … .
– We start the while loop at i = 1 as before. The while loop goes till n as before. The initial sum is 0 as before.
– The difference is that we increment i by 2 in each iteration of the loop.
– In each iteration, we add the value of i to our running sum,
– We exit the loop when i goes beyond n.
Q.3. Compute the sum 2 + 5 + 8 + … + (till n), using while loop. (Here n>=2 is an input from the user.)
Answer.
//Program to compute the sum 2 + 5 + 8 + ... + (till n), using while loop. //Input - n (n>=2) //Output - the desired sum //Logic - Start the while loop with the loop counter as 2. The running sum is initially 0. // In each iteration, increment the running sum by the loop counter. // Increment the loop counter by 3 (to get the next number to add to our sum.)// Continue the loop till n. <stdio.h> int main(){ int sum = 0, i = 2, n; //running sum is initially 0. // loop starts at 2. Hence i is set to 2. // n is the input from the user. printf("Pls enter a number (>=2): "); //request the user to enter a number scanf("%d", &n); //get the input, save it in n. while (i <= n) //till when to continue the loop? Run the loop till i reaches n. { sum += i; //each number that we encounter, we add it to our running sum i += 3; //increment the loop counter by 3 to get the next number to add to our sum } printf("The desired sum is %d \n", sum); return 0; }
output: sample output 1:Pls enter a number (>=2): 5The desired sum is 7sample output 2: Pls enter a number (>=2): 6The desired sum is 7sample output 3: Pls enter a number (>=2): 8The desired sum is 15sample output 4: Pls enter a number (>=2): 10The desired sum is 15
Explanation –
– This is similar to the previous program. Simply the starting number of the series, and the increment value are different.
– We need to compute 2 + 5 + 8 + … + (till n).
– We start the while loop at i = 2. In each iteration of the loop, i is incremented by 3. We continue the loop till i goes beyond i.
– The running sum is initially 0.
– in each iteration of the loop, we add the value of i to our running sum.
– we exit the loop when i becomes greater than n.
Leave a comment