Sum of digits program

Sum of digits in c

``` #include<stdio.h> #include<conio.h> void main() { int input,sum=0,reminder; printf("Enter your number \n"); scanf("%d",&input); while(input!=0) { reminder=input%10; sum=sum+reminder; input=input/10; } printf("Sum is = %d",sum); getch(); } ```

Output

```Enter your number 12345 Sum is = 15 ```

Sum of digits in c++

``` #include<iostream> #include<conio.h> using namespace std; class SumOfDigits{ public: void Sum(int input) { int sum=0,reminder; while(input!=0) { reminder=input%10; sum=sum+reminder; input=input/10; } cout<<"Sum is = "<<sum; } }; void main() { int input; SumOfDigits s; cout<<"Enter your number \n"; cin>>input; s.Sum(input); getch(); } ```

Output

```Enter your number 123456 Sum is = 21 ```

Sum of digits in c#

``` using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SumOfDigitsProject { class SumOfDigits { public void Sum(int input) { int reminder, sum = 0; while (input != 0) { reminder = input % 10; sum = sum + reminder; input = input / 10; } Console.WriteLine("Sum is = " + sum.ToString()); } static void Main(string[] args) { SumOfDigits sod = new SumOfDigits(); Console.WriteLine("Enter your number"); int input = Convert.ToInt32(Console.ReadLine()); sod.Sum(input); Console.ReadLine(); } } } ```

Output

```Enter your number 1234 Sum is = 10 ```

Sum of digits in java

``` import java.util.*; class SumOfDigits { public void Sum(int input) { int reminder,sum=0; while(input!=0) { reminder=input%10; sum+=reminder; input/=10; } System.out.print("Sum is = "+sum); } } class examples { public static void main(String[] args) { SumOfDigits sod=new SumOfDigits(); Scanner sc=new Scanner(System.in); System.out.println("Enter the number"); int input=sc.nextInt(); sod.Sum(input); } } ```

Output

```Enter the number 123 Sum is = 6 ```

Write a comment

Name Email Message