Computer Science, asked by gshfgh7152, 11 months ago

C program to find sum of digits of a number until a single digit is obtained

Answers

Answered by sushiladevi4418
1

Answer:

C program to find sum of digits of a number until a single digit is obtained

Explanation:

 // C# program to find sum of

// digits of a number until

// sum becomes single a digit.

using System;  

 

class GFG {  

     

   static int digSum(int n)  

   {  

       int sum = 0;  

 

       // Loop to do sum while  

       // sum is not less than  

       // or equal to 9  

       while (n > 0 || sum > 9)  

       {  

           if (n == 0)  

           {  

               n = sum;  

               sum = 0;  

           }  

           sum += n % 10;  

           n /= 10;  

       }  

       return sum;  

   }  

     

   // Driver code  

   public static void Main()  

   {  

       int n = 1234;  

       Console.Write(digSum(n));  

   }  

}

// code

using System;  

 

class GFG  

{  

   static int digSum(int n)  

   {  

       if (n == 0)  

       return 0;  

       return (n % 9 == 0) ? 9 : (n % 9);  

   }  

     

   // Driver Code  

   public static void Main ()  

   {  

       int n = 9999;  

       Console.Write(digSum(n));  

     

   }  

}  

 

Answered by anagasatyasri710
1

Answer:

#include<stdio.h>  

int digSum(int n)  

{  

int sum = 0;  

while(n > 0 || sum > 9)  

{  

if(n == 0)  

{  

n = sum;  

sum = 0;  

}  

sum += n % 10;  

n /= 10;  

}  

return sum;  

}  

int main()  

{  

int n;

scanf("%d",&n);

printf("%d",digSum(n));  

return 0;  

}

Explanation:

Similar questions