Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Homework HelpRSS Feeds

PROGRAM USING WHILE LOOP TO DO SUM OF DIGITS OF THE NUMBER. NO IS - 12345 ANS IS 15

Posted By: Dirck Jansen     Category: C Programming     Views: 22587

WRITE A PROGRAM USNG WHILE LOOP TO DO SUM OF THE DIGITS OF THE NUMBER.
NO IS - 12345
ANS IS 15

Code for PROGRAM USING WHILE LOOP TO DO SUM OF DIGITS OF THE NUMBER. NO IS - 12345 ANS IS 15 in C Programming

#include <stdio.h>
#include <conio.h>
#include<math.h>


void main()
{
long n,j,sum = 0;
clrscr();
printf("Enter the No :- ");
scanf("%ld",&n);
       printf("\n");
    while(n>0)
    {
       j = n%10;
       sum += j;
       n /= 10;
    }

printf("The sum of digit is = %ld", sum);
getch();

}

/*
********
OUTPUT
********

Enter the No :- 678

The sum of digit is = 21
*/
  
Share: 



Dirck Jansen
Dirck Jansen author of PROGRAM USING WHILE LOOP TO DO SUM OF DIGITS OF THE NUMBER. NO IS - 12345 ANS IS 15 is from Amsterdam, Netherlands.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Pran Pradeep from India Comment on: Dec 13
if we give input as 3456 its showing 21 :( :(


Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim n As Integer
Dim j As Integer
Dim sum As Integer
n = TextBox1.Text

Do While n > 0

j = n Mod 10
sum += j
n /= 10

Loop
MsgBox(sum)
End Sub
End Class

View All Comments