C-Program to print the sum of square of first N natural numbers

#include <stdio.h>
 
int main() {
	int num, square, sum = 0, i = 1;
 
	/* get the input value for n from user */
	printf("Enter the value for n:");
	scanf("%d", &num);
 
	/* calculate the square of 1st n natural numbers */
	while (i <= num) {
		square = i * i;
		sum = sum + square;
		i++;
	}
 
	/* print the sum of square of first n natural nos */
	printf("Sum of squares of first %d natural numbers is %d\n", num, sum);
 
	return 0;
}

Capture.PNG
Output
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s