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:
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:
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:
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:
Figure: Flowchart for calculate sum of square C program |
No comments:
Post a Comment