Write a program to calculate the sum of digit of a three digit number
Answers
Answered by
1
If it's question from java then the answer is the following :-
public class
{
public void main(int a)
{
int temp = a;
int sum = 0;
while (temp > 0)
{
int d = temp % 10;
sum = sum + d;
temp = temp / 10;
}
System.out.println(sum);
}
}
.
.
.
Please use the codings as I have done , no digits or symbols should be changed in order to get correct output.
Answered by
22
Program to calculate the sum of digit of a three digit number.
#include <stdio.h>
int main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}
Explanation:
I have Given the C program to Calculate Sun of 3 digit numbers. We know that, At stating of C program, we write '#include <stdio.h>'
So, we write '#include <stdio.h>' at the starting. After that:
int main()
{
int n, t, sum = 0, remainder;
We will have to write Sum = 0. Because we have to find sum of three digit number.
Then, write:
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
Now, Main point. It is Printf. write Printf code after the given program:
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}
▪️ Here is a program for without Modulus operator:
#include <stdio.h>
int main()
{
int c, sum, t;
char n[1000];
printf("Input an integer\n");
scanf("%s", n);
sum = c = 0;
while (n[c] != '\0') {
t = n[c] - '0'; // Converting character to integer
sum = sum + t;
c++;
}
printf("Sum of digits of %s = %d\n", n, sum);
return 0;
}
#include <stdio.h>
int main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}
Explanation:
I have Given the C program to Calculate Sun of 3 digit numbers. We know that, At stating of C program, we write '#include <stdio.h>'
So, we write '#include <stdio.h>' at the starting. After that:
int main()
{
int n, t, sum = 0, remainder;
We will have to write Sum = 0. Because we have to find sum of three digit number.
Then, write:
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
Now, Main point. It is Printf. write Printf code after the given program:
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}
▪️ Here is a program for without Modulus operator:
#include <stdio.h>
int main()
{
int c, sum, t;
char n[1000];
printf("Input an integer\n");
scanf("%s", n);
sum = c = 0;
while (n[c] != '\0') {
t = n[c] - '0'; // Converting character to integer
sum = sum + t;
c++;
}
printf("Sum of digits of %s = %d\n", n, sum);
return 0;
}
rishijadwivedip912eh:
wow ! you have explained it very well.
Similar questions
Social Sciences,
7 months ago
Social Sciences,
7 months ago
Computer Science,
7 months ago
English,
1 year ago
Geography,
1 year ago
Social Sciences,
1 year ago