Program to find sum of individual digits of number using function

1 year ago Admin 0

Find sum of individual digits of number using function

This article illustrates the concept how to find sum of individual digits of number using function in c/c++.

find sum of individual digits of number using function

Here is the source code and output of both the programs.

C Programming language

Program


#include<stdio.h>
#include<conio.h>
void main()
{
 int num,res;
 clrscr();
 printf("Enter Number:- ");
 scanf("%d",&num);
 res=sum(num);
 printf("Sum of individual digit of number is:- %d",res);
 getch();
}
int sum(int num)
{
 int rem,sum=0;
 while(num>0)
  {
   rem=num%10;
   sum=sum+rem;
   num=num/10;
  }
 return sum;
}

After successfully compiling and executing the above C program,output is:-

find sum of individual digits of number using function


C++ Programming language

Program


#include<iostream.h>
#include<conio.h>
int sum(int);
void main()
{
 clrscr();
 int num,res;
 cout<<"Enter Number:- ";
 cin>>num;
 res=sum(num);
 cout<<"Sum of individual digit of number is:- "<<res;
 getch();
}
int sum(int num)
{
 int rem,sum=0;
 while(num>0)
  {
   rem=num%10;
   sum=sum+rem;
   num=num/10;
  }
 return sum;
}

After successfully compiling and executing the above C++ program,output is:-

find sum of individual digits of number using function


Comment below if you are facing any problem or you find any error in the above program and also subscribe to our newsletter for more updates.


Read more c/c++ Programs published by snapcout:-

Program to identify missing number in entered array using function
Program to find greatest between three numbers using function
Program to insert an element in array using function
Program to search an element in array using function
Program to find largest element in array using function


About article

Written By:- Admin

Published on:- 09/08/2018

Published By:- Snapcout


Don't forget to share the article if you like it:-