Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSC 118: Programming Fundarmentals Lab 6: Programming assignment Programming ass

ID: 3739375 • Letter: C

Question

CSC 118: Programming Fundarmentals Lab 6: Programming assignment Programming assignment I (Count-Controlled Loop) Exercise l Examine the following shell program SumDigits. When completod, program SumDigits prompts for and reads a one-digit number, then adds the nuimber from zeto to the mamber, inclusive, and print the sum // Program SumDigits prompts for and reads a one-digit nunber /I Values betveen and the digit (Inclusive) are suned. include using nanespace std int main int counter: / Loop-control variable int sum; int digit; // Running sun cout

Explanation / Answer

ANS:-

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{
    int counter;
    int sum;
    int digit;
    cout<<"Enter a one-digit number"<<endl;
    cin>>digit;
    counter = 0;
    sum = 0;
    int d = digit;
    while(d>0){
        sum = sum+d;
        d--;
    }
    cout<<"Sum of digits between 0 and "<<digit<<" is "<<sum<<endl;
    return 0;
}


OUTPUT:

Answer for 0: 0

Answer for 3: 6

Answer for 7: 28

Answer for 9: 45