Write a Shell script to print given numbers sum of all digits.
Question
Write a Shell script to print given numbers sum of all digits.
Solution
Sure, here is a simple shell script that will calculate the sum of all digits of a given number:
#!/bin/bash
# Read the number
echo "Enter a number: "
read num
# Initialize sum to 0
sum=0
# Calculate sum of digits
while [ $num -gt 0 ]
do
# Get last digit
digit=$((num % 10))
# Add digit to sum
sum=$((sum + digit))
# Remove last digit
num=$((num / 10))
done
# Print the sum
echo "The sum of all digits is: $sum"
Here's how this script works:
- It first reads a number from the user.
- It initializes a variable
sumto 0. This variable will hold the sum of the digits. - It enters a while loop that continues as long as
numis greater than 0. - Inside the loop, it calculates the last digit of
numusing the modulus operator%and stores it indigit. - It adds
digittosum. - It removes the last digit from
numusing integer division by 10. - Once the loop is done, it prints the sum of the digits.
Similar Questions
#include<stdio.h> int main(){ int number,sum=0,digit; printf("Enter an integer : "); scanf("%d",&number); while (number != 0){ digit=number%10; sum += digit; number /= 10 } printf("The sum of digits of the given number "%d" = %d",number,sum); return 0; }
Problem StatementVivek wants to count the number of digits in the given integer and find the sum of the first and last digits of the number. Write a suitable program to complete the above task using a for loop.ExampleInput:1221Output:Sum = 2Digits = 4Explanation:Counting Digits: The number 1221 has 4 digits.First and Last Digits: The last digit is 1 (1221 % 10). To find the first digit, divide 1221 by 10 repeatedly until it's less than 10. The first digit is 1.The sum is 1 + 1 = 2.Note: This question helps in clearing coding technical tests for service-based companies.Input format :The input consists of an integer n.Output format :The first line prints "Sum = " followed by an integer which is the sum of the first and last digits.The second line prints "Digits = " followed by an integer which is the number of digits.Refer to the sample output for the formatting specifications.Code constraints :In this scenario, the test cases fall under the following constraints:10 ≤ n ≤ 10000Sample test cases :Input 1 :1221Output 1 :Sum = 2Digits = 4Input 2 :10Output 2 :Sum = 1Digits = 2Input 3 :10000Output 3 :Sum = 1Digits = 5
Write a C program to calculate sum of digits of a number. – using while loop
Design a program that mimics a digital calculator. Take an integer as input, use a function named sumOfDigits with a pointer to calculate the sum of its digits, and display the result. This simulates the process of manually adding up the individual digits of a number as you would on a calculator.Note: This question helps in clearing Wipro technical coding tests.Input format :The input consists of an integer N.Output format :The output prints an integer representing the sum of digits of N.Code constraints :1 ≤ N ≤ 105Sample test cases :Input 1 :123Output 1 :6Input 2 :5698Output 2 :28Note :The program will be evaluated only after the “Submit Code” is clicked.Extra spaces and new line characters in the program output will result in the failure of the test case.Marks : 10Negative Marks : 0
Write a python program that:Prompt the user for a stringExtract all the digits from the string.If there are digitsSum the collected digits together.Printout:The original stringThe digitsThe sum of the digitsIf there are no digitsPrint the original stringA message “Has no Digits”
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.