Skip to main content

How to Find the Sum of Digits of a Number without loop in c programming

C Program to Find Sum of Digits of a Number without loop:-

 In this program, we will calculate the sum of the number inserted by the user or in an inserted integer. The program is taken as an input and stored in the variable number, denoted as no. Initially, the sum of the variable is zero, and then it is divided by 10 to obtain the result or output.

  

ALGORITHMS:-

Step 1: Start 

Step 2: Ask the user to insert an integer as an input. 

Step 3Divide the integer by 10 in order to obtain the quotient and remainder. 

Step 4: Increase the new variable with the remainder received in the above step.

Step 5: Repeat the above steps with the quotient till the value of the quotient becomes zero. 

Step 6Printf the output or sum.

Step 7: Stop.

CODE: - 

#include<stdio.h>

int main()
{
    int n,d1,d2,d3,d4,d5,sum;
    printf("enter any num");
    scanf("%d",&n);
    d1=n%10;
    n=n/10;
    d2=n%10;
    n=n/10;
    d3=n%10;
    n=n/10;
    d4=n%10;
    n=n/10;
    d5=n%10;
    sum= d1+d2+d3+d4+d5;
    printf("%d\n",sum);
    return 0;
}





OUTPUT:-








FOLLOW THIS WEBPAGE FOR MORE NEW PROGRAMS AND CODING SOLUTIONS.

Comments

Popular posts from this blog

Data Structure Multiple Choice Questions and Answers

  Data Structure Multiple Choice Questions and Answers Our 1000+ multiple choice questions and answers (MCQs) on "Data Structure - I" (along with 1000+ MCQs on "Data Structure - II (Algorithms)") focus on all areas of Data Structure covering 200+ topics. One can read MCQs on Data Structure - II (Algorithms)                 Array and Array Operations This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Array and Array Operations”. 1. Which of these best describes an array? a) A data structure that shows a hierarchical behavior b) Container of objects of similar types c) Arrays are immutable once initialised d) Array is not a data structure View Answer Answer: b Explanation: Array contains elements only of the same type. 2. How do you initialize an array in C? a) int arr[3] = (1,2,3); b) int arr(3) = {1,2,3}; c) int arr[3] = {1,2,3}; d) int arr(3) = (1,2,3); View Answer Answer: c Explanation: This is the syntax ...

Write a C program to find number is Abundant number or not

  Write a C program to find the number is an Abundant number or not:- In this program to find a number is an Abundant number or not. A number n is said to be an Abundant Number to follow these condition the sum of its proper di visors is greater than the number itself. And the difference between these two values is called abundance. Ex:-  Abundant number  12 having a proper divisor is 1,2,3,4,6 the sum of these factors is 16 it is greater than 12 so it is an Abundant number. Some other abundant numbers     18, 20, 24, 30, 36, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100, 102, 104, 108, 112, 114, 120.. ALGORITHMS:- Step 1 - Enter the number, to find the Abundant number. Step 2 - Initialize the loop with c=1 to c<=number and follow the following calculation      (i) check if whether the number is divisible with c and c got a result zero.      (ii) now sum=sum+c, add a digit into a sum and store it in the sum. Step 3 . then the...

sWAP cASE

  sWAP cASE: You are given a string and your task is to  swap cases . In other words, convert all lowercase letters to uppercase letters and vice versa. For Example: Www.HackerRank.com → wWW.hACKERrANK.COM Pythonist 2 → pYTHONIST 2 Function Description Complete the  swap_case  function in the editor below. swap_case  has the following parameters: string s:  the string to modify Returns string:  the modified string Input Format A single line containing a string  . Sample Input 0 HackerRank.com presents "Pythonist 2". Sample Output 0 hACKERrANK.COM PRESENTS "pYTHONIST 2". def   swap_case ( s ):      result =  s . swapcase ()      return ( result ) if  __ name__  ==  '__main__' :      s  =  input ()      result  =  swap_case ( s )      print ( result )