Knowee
Questions
Features
Study Tools

Write a C program to calculate sum of digits of a number. – using while loop

Question

Write a C program to calculate sum of digits of a number. – using while loop

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that calculates the sum of digits of a number using a while loop:

#include <stdio.h>

int main() {
    int num, sum = 0, remainder;

    printf("Enter an integer\n");
    scanf("%d", &num);

    while(num != 0) {
        remainder = num % 10;
        sum = sum + remainder;
        num = num / 10;
    }

    printf("Sum of digits of entered number = %d\n", sum);

    return 0;
}

Here's how it works:

  1. The program first asks the user to enter an integer.
  2. It then enters a while loop, which continues until the number is not equal to 0.
  3. Inside the loop, the program calculates the remainder of the number when divided by 10. This gives the last digit of the number.
  4. The program then adds this remainder to the sum.
  5. The number is then divided by 10, effectively removing the last digit.
  6. Once the number is 0, the loop ends and the program prints the sum of the digits.

This problem has been solved

Similar Questions

Write a C program to count number of digits in a number. Using do-while loop

#include<stdio.h> int main(){ int number,sum=0,digit; printf("Enter an integer : "); scanf("%d",&number); while (number != 0){ digit=number%10; sum += digit; number /= 10 } printf("The sum of digits of the given number "%d" = %d",number,sum); return 0; }

Sum of digitsA function, sumN, is defined that takes an integer n as argument and returns the sum of the integers from 1 through n. Write a C program to determine value of the expression shown below?  sumN(3456) - sumN(3455)Note: the arguments of function sumN() can be obtained from user at runtimeTestcases:Input:18041604Output:2Input:34563455Output:1

James, a mathematics teacher, is developing a programming exercise to help his students practice continuously summing the digits of a number until it becomes a single-digit integer. He wants to create a simple program using a 'while' loop that takes a positive integer input and generates the final single-digit result. Input format :The input consists of a positive integer n.Output format :The output prints "The single-digit sum of n is Y." where n is the input integer and Y is its single-digit sum.Refer to the sample output for formatting specifications.Code constraints :1 ≤ n ≤ 1010Sample test cases :Input 1 :456Output 1 :The single-digit sum of 456 is 6.Input 2 :999Output 2 :The single-digit sum of 999

#include <stdio.h>int main(){ int num; int sum=0, rev=0; printf("Enter a number"); scanf("%d", &num); while(num>0) { sum=sum + num%10; rev=rev*10 + num%10; num=num/10; } printf("sum=%d\n", sum); printf("reverse=%d\n", rev); return 0;}

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.