Showing posts with label sum of square algorithm. Show all posts
Showing posts with label sum of square algorithm. Show all posts

10.07.2012

Sum of square program, Algorithm and Flowchart

Q. Write a C program to accept a number from user and print the sum of square. Also write down the algorithm and draw flowchart.

Ans.

/*c program for accept number and calculate sum of square*/
#include<stdio.h>
int main()
{
 int num,sum=0,i;
 printf("Enter any number : ");
 scanf("%d", &num);
 for(i=1; i<=num; i++)
    sum = sum + (i*i);
 printf("Sum of square of %d = %d",num,sum);
 return 0;
}

The output of above program would be:
Output of sum of square of any number C program
Figure: Screen shot for calculate sum of
square of number C program



Algorithm for accept a number from user and calculate sum of square:

[Sum of square procedure:accept number num from user, and set sum=0 and calculate sum of square.]
Step 1. Start
Step 2. Read number num
Step 3. [Initialize]
        sum=0, i=1
Step 4. Repeat step 4 through 6 until i<=num
Step 5. sum=sum+(i*i)
Step 6. i=i+1
Step 7. print the sum of square
Step 8. stop
[end of loop step 4]
[end of sum of square procedure]


Flowchart for sum of square C program as following:
flowchart for accept number and calculate sum of square
Figure: Flowchart for calculate sum of square C program