0% found this document useful (0 votes)
32 views

Day 12 Coding Solutions PDF

The document provides code examples in C, C++, Java, and Python to find the sum of digits of a given number. It explains that the program takes a number as input, uses a while loop to extract the last digit by modulo 10, adds it to the running sum, and divides the number by 10 to remove the last digit. This continues until the number becomes 0, at which point the final sum is printed as output. Methods 1 and 2 in C use a while loop and recursive approach respectively to find the sum of digits.

Uploaded by

sajeed malagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Day 12 Coding Solutions PDF

The document provides code examples in C, C++, Java, and Python to find the sum of digits of a given number. It explains that the program takes a number as input, uses a while loop to extract the last digit by modulo 10, adds it to the running sum, and divides the number by 10 to remove the last digit. This continues until the number becomes 0, at which point the final sum is printed as output. Methods 1 and 2 in C use a while loop and recursive approach respectively to find the sum of digits.

Uploaded by

sajeed malagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Talent Battle 100 Days Coding Series

Write a program to find Sum of digits of a number

Description

Get a number from user and then find the sum of the digits in the given number.

E.g. let the number = 341

Sum of digits is 3+4+1= 8

Input

4521

Output

12

C Program

Method 1

#include <stdio.h>

int main()

int num, sum = 0;

printf("Enter a number: ");

scanf("%d",&num);

while(num!=0)

sum =sum+ num % 10;

num = num / 10;

printf("%d",sum);

return 0;

}
Talent Battle 100 Days Coding Series

Method 2

#include <stdio.h>

int SumOfDigits(int n, int sum)

if (n==0)

return sum;

else

sum=sum+n%10;

return SumOfDigits(n/10,sum);

int main()

int num, sum = 0;

printf("Enter a number: ");

scanf("%d",&num);

int result = SumOfDigits(num,sum);

printf("%d",result);

return 0;

}
Talent Battle 100 Days Coding Series

C++ Program

#include <iostream>

using namespace std;

int main()

int num, sum = 0;

cout<<"Enter a number: ";

cin>>num;

while(num!=0)

sum =sum+ num % 10;

num = num / 10;

cout<<sum;

return 0;

}
Talent Battle 100 Days Coding Series

Java

import java.util.Scanner;

public class Main

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

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

int num = sc.nextInt();

int sum= 0;

while(num!=0)

sum = sum + num%10;

num = num / 10;

System.out.print(sum);

}
Talent Battle 100 Days Coding Series

Python

num=int(input("Enter a number: "))

Sum=0

while num!=0:

Sum=Sum+num%10

num=num//10

print(Sum)

You might also like