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 3: Divide 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 6: Printf 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

Calculate the sum of elements in an array.

  HOW TO CALCULATE THE SUM OF THE ELEMENT IN A GIVEN ARRAY: - We will learn how to find the sum of elements in an array is an easy task when you know how to iterate through array elements. In this problem, we will explain your C approach to find the sum of array elements inputted by the user. Here is the solution to this query in C Languageguage.   ALGORITHMS: STEP 1:-   Input the size of the array and store the value in m and arr[m]. STEP 2 : - To store the sum of array elements, initialize a variable sum = 0. STEP 3: -To find the sum of all elements, iterate through each element  and add the current element to the sum. STEP 4: - Inside the loop add the current array element to sum i.e.                 sum = sum + arr[i] or sum += arr[i]. CODE:- #include <stdio.h> int  main() {      int  i,num,a[ 100 ],sum= 0 ;     printf( "Enter the value of number:...

How to find the Tringle is Valid or Not in C

 How to find the Tringle is Valid or Not in C:- Code:- #include <stdio.h> int  main() {        int  side_1,side_2,side_3;        int  a,b,c;       printf( "enter the value of sides:" ); scanf( "%d\n %d\n %d" ,&side_1,&side_2,&side_3);       a= ((side_1*side_1)==((side_2*side_2)+(side_3*side_3)));       b= ((side_2*side_2)==((side_1*side_1)+(side_3*side_3)));       c= ((side_3*side_3)==((side_1*side_1)+(side_2*side_2)));       if  (a || b || c)      {         printf( "The triangle is valid" );      }       else      {...

How to find Power of a number in C

  How to find Power of a number in C: - In this program we will calculate the power of a number using C programming. We want to calculate the power of any given number so you need to multiply that particular number power of time. Ex:- Let suppose number is 2 4  so we need to multiply with 4 times of 2. That is 2*2*2*2=16. ALGORITHMS:- Step 1 – Enter the base number, the number in which you just want to find the power of the number. Step 2 – Enter the exponential, the power of the number. Step 3 – Initialize while loop, while the exponential is not equal to zero.                  (i)  do temp*number and store it in the temp.                   (ii)  now reduce the exponential with -1.                   Here you got the power of the number. Step 4 – Now print the temp variable. Step 5-  Stop. CODE: - #include <stdio.h> int ...