23.12.19

Sum of individual digit of a positive integer program in c


#include<stdio.h>
void main()
{
 int n,s=0,d;
 printf("Enter Any Positive Number(Integer): ");       
 scanf("%d",&n);             
 while(n!=0)
 {
  d=n%10;     
  s=s+d;     
  n=n/10;     
 }
 printf("The sum of the digits of given Number is %d\n",s);
}

Output1:
Enter Any Positive Number(Integer):145
The sum of the digits of given Number is 10
Output2:
Enter Any Positive Number(Integer):34567
The sum of the digits of given Number is 25

No comments:

Post a Comment

JAVA program pattern 2

import java.util.Scanner; public class pattern3 { public static void main(String args[]) { int i,j; Scanner sc=new Scanner(System.in); Syste...