Solved Examples · Example 32
Q.Write a C program to print the sum of the first 60 natural numbers.
The textbook presents this worked program, transcribed exactly as printed (including its printed errors):
#include <stdio.h>
void main()
{
int num, sum = 0
for (num=1; num<=60; num++)
sum=sum+num;
printf("sum=%4d\n", sum);
} /*End of main()*/
Sample output printed in the textbook:
Sum = 1830
#include <stdio.h>
void main()
{
int num, sum = 0
for (num=1; num<=60; num++)
sum=sum+num;
printf("sum=%4d\n", sum);
} /*End of main()*/
Sum = 1830
Karnataka PUCTextbookLongImportance★★★★★est
92% · 142/154 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →[!TLDR]
A for loop accumulates 1 + 2 + ... + 60 = 1830.
The for statement packs the three loop-control pieces into one header: for(initialise; test; update). Here num = 1 initialises the counter, num <= 60 is checked before each pass, and num++ advances it afterwards, so the single body statement sum = sum + num runs exactly 60 times. The closed-form check n(n+1)/2 gives 60 x 61 / 2 = 1830. …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.