Computer Science, asked by varungaur6886, 11 months ago

Write a program to compute the sum of the digits of a given integer number

Answers

Answered by ayaan12396
1

Answer:

Please become my follower so that I can solve all your doubts........

Explanation:

import java.util.*;

public class Sum_Of_Digits

{

public static void main()

{

Scanner in= new Scanner (System.in);

int d,sum=0;

System.out.println ("Enter a number whose sum of digits is to be calculated");

int num= in.nextInt();

do

{

d= num/10;

sum=sum+d;

num=num/10;

}

while(num!=0);

System.outprintln("The sum of the digits="+" "+sum);

}

}

Answered by Anonymous
0

Answer:

Explanation:

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

}

If you wish you can modify the input variable (n) and without using an additional variable (t) but it isn't recommended.

Similar questions