adsense

Hii, welcome to my site. My name is Om prakash kartik. This blog helps you to learn programming languages concepts.

Program in C and C++ to find sum of digits of a given number using recursive function

Program in C and C++ to find sum of digits of a given number using recursive function


C Program
#include<stdio.h>
 // Sum of all digits of a given number
 int sumOfDigits(int n){
    if(n % 10 == 0)
        return 0;
    else
        return (n % 10 + sumOfDigits(n / 10));
 }
 int main()
  {
    int n;
    printf("Enter a number :- ");
    scanf("%d",&n);
    printf("Sum of all digits : %d",sumOfDigits(n));
    return 0;
  }
C++ Program
#include<iostream>
 using namespace std;
 // Sum of all digits of a given number
 int sumOfDigits(int n){
    if(n % 10 == 0)
        return 0;
    else
        return (n % 10 + sumOfDigits(n / 10));
  }
 int main()
  {
    int n;
    cout<<"Enter a number :- ";
    cin>>n;
    cout<<"Sum of all digits : "<<sumOfDigits(n);
    return 0;
  }
Output :-










Related Programs

Post a Comment

0 Comments