LOOP: (TURBOc++)
Prg16: Program that accepts a number then output the sum of the squares from 1 to n.
Enter a number : 4
1^2 + 2^2 + 3^2 + 4^2 = 30
PLEASE USE:
#include<stdio.h>
#include<conio.h>
scanf
printf
example:
#include<stdio.h>
#include<conio.h>
main()
{
int n=1, x=20;
clrscr();
printf("While Loop Example");
while (n<=5)
{
gotoxy(x, 3);printf(" %d", n);
n++;x=x+5;
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()
{
printf("Enter a number");
int n;
scanf("%d", &n);
int squares = 0;
int i = 1;
while (i<=n)
{
squares += (i*i);
i++;
}
gotoxy(squares, 3); printf(" %d", squares);
getch();
}
Comments
Leave a comment