Write a program to sum of all digits of a number.

Write a C program to sum of all digits of a number. or Write a program to  sum of all digits of a number  in C

Program in C

Code:

/*Write a C program to the sum of all digits of a number. or Write a program to sum of all digits of a number Using C*/

#include<stdio.h>
int main()
{
    int n;
    printf("Enter a number:");
    scanf("%d",&n);
    int sum=0;
    while(n!=0)
    {
        int rem=n%10;
        sum=sum+rem;
        n=n/10;
    }
      printf("The sum of digits of the number is %d",sum);
}

Input/Output:
Enter a number:12345
The sum of digits of the number is 15

Write a C++ program to sum of all digits of a number. or Write a program to sum of all digits of a number in C++.

Program in C++

Code:

/*Write a C++ program to the sum of all digits of a number. or Write a program to sum of all digits of a number using C++*/

#include<iostream>
using namespace std;
int main()
{
    int n;
    cout<<"Enter a number:";
    cin>>n;
    int sum=0;
    while(n!=0)
    {
        int rem=n%10;
        sum=sum+rem;
        n=n/10;
    }
      cout<<"The sum of digits of the number is "<<sum;
}

Input/Output:
Enter a number:879456
The sum of digits of the number is 39

Write a JAVA program to the sum of all digits of a number. or Write a program to sum of all digits of a number in Java.

Program in Java

Code:

/*Write a JAVA program to the sum of all digits of a number. or Write a program to sum of all digits of a number using Java*/

import java.util.Scanner;
public class sumofdigits {

public static void main(String[] args) {
     Scanner cs=new Scanner(System.in);
     int n;
     System.out.println("Enter a number:");
     n=cs.nextInt();
     int sum=0;
     while(n!=0)
     {
         int rem=n%10;
         sum=sum+rem;
         n=n/10;
     }
       System.out.println("The sum of digits of the number is "+sum);
     cs.close();
}
}

Input/Output:
Enter a number:
45879
The sum of digits of the number is 33

Write a PYTHON to the sum of all digits of a number. or Write a program to sum of all digits of a number in Python.

Program in Python

Code:

'''Write a Python program to the sum of all digits of a number. or 
   Write a program to the sum of all digits of a number using Python '''

n=int(input("Enter a number:"))
sum=0
while n>0:
   rem=n%10
   sum=sum+rem
   n=int(n/10)
print("The sum of digits of number is:", sum) 

Input/Output:
Enter a number:45564
The sum of digits of number is: 24





Post a Comment

0 Comments