Computer Science, asked by dawsonnfs16, 1 day ago

Calculate and print the sum of the digits in the number .Sample Input : 234 Sample Output : 9

Answers

Answered by samarthkrv
1

Answer:

java-

import java.util.*;

class digitSum

{

static int sum(int n)

{

int sum = 0;

 while(n != 0)

 {

 int last = n%10;

 sum = sum + last;

 n /= 10;

 }

return sum;

}

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

System.out.print("Enter a number:");

int n = sc.nextInt();

System.out.println("The sum of the digits of " + n + " is " + sum(n));

}

}

c++-

#include<iostream>

using namespace std;

int sum(int n)

{

int sum = 0;

while(n != 0)

{

int l = n % 10;

sum = sum + l;

n/=10;

}

return sum;

}

int main()

{

int x;

cout << "Enter a number:";

cin>>x;

cout<<"The sum of digits of " << sum(x);

return 0;

}

#include<stdio.h>

#include<conio.h>

int sum(int n)

{

int sum = 0;

 while(n != 0)

 {

 int last = n%10;

 sum = sum + last;

 n /= 10;

 }

return sum;

}

int main()

{

int n;

printf("Enter a number:");

scanf("%d" , &n);

printf("The sum of the digits of %d is %d" , n , sum(n));

return 0;

getch();

}

Explanation:

Similar questions