Knowee
Questions
Features
Study Tools

#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; }

Question

#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; }

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

Solution

The code you've written is in C language and it's used to calculate the sum of digits of a given number. However, there's a small syntax error in your code. The semicolon is missing at the end of the line where you're dividing the number by 10. Also, the printf statement at the end is not correctly formatted. Here's the corrected version of your code:

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

In this corrected version, I've added a semicolon at the end of the line number /= 10;. Also, I've stored the original number in a temporary variable temp before the while loop because the original number is getting modified in the loop. Then, I've used this temp variable in the printf statement at the end.

This problem has been solved

Similar Questions

0/0

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.