Write algorithm to calculate the sum of numbers between 1 to 100
Answers
Answered by
15
Explanation:
STEP 1: START
STEP 2: INITIALISE A=0,B=1;
STEP 3: WHILE (B<=100)
STEP 4: GO TO STEP 5 OTHERWISE GO TO STEP 9
STEP 5: A=A+B;
STEP 6: B++;
STEP 7: REPEAT STEP 3 TO 6 UNTIL CONDITION IS TRUE.
STEP 8 : PRINT THE VALUE OF A.
STEP 9 : EXIT
Answered by
0
C++ program:
#include<bits/stdc++.h>
using namespace std;
int sumOfDigits(int );
int sumOfDigitsFrom1ToN(int n)
{
int result = 0;
for (int x = 1; x <= n; x++)
result += sumOfDigits(x);
return result;
}
int sumOfDigits(int x)
{
int sum = 0;
while (x != 0)
{
sum += x %10;
x = x /10;
}
return sum;
}
int main()
{
int n = 100;
cout << "Sum of digits in numbers from 1 to " << n << " is "
<< sumOfDigitsFrom1ToN(n);
return 0;
}
Similar questions