C Program To Calculate Sum of Digits Using Recursion

preview_player
Показать описание

A 5-digit positive integer is entered through the keyboard, write a recursive and a non-recursive function to calculate sum of digits of the 5-digit number.

Analyze The Problem Statement
1. User enters a 5-digit number.
2. We need to write 2 functions to calculate sum of digits of user entered number.
3. One function should use recursive logic and the other should calculate sum of digits without using recursion.

C Programming Interview / Viva Q&A List

C Programming: Beginner To Advance To Expert
Рекомендации по теме
Комментарии
Автор

Thank you so much it was really good 👍

MohammadaminBijnavand
Автор

Bro nice, I want to print Star pattern 'C', big C in star pattern please tell me how to do

ty_b__ramkrushna_karnika
Автор

One more question is 1234354 ?
How to print repeated numbers with out using array with the help of nested for loop please tell me

ty_b__ramkrushna_karnika
Автор

#include<stdio.h>
int d=0, res=0;
int sum(int n){
if(n == 0)
return 0;
else
{
d = n % 10;
res = d + sum(n/10); this should also work right but is not working and giving different output
return res;
}
}
int main( ){
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Sum of digits are %d", sum(n));
return 0;
}

the above logic is not working, please help

kunsangmoktan