Beginner to C.

0 Milburn camilo · September 9, 2015
I am a beginer to C programing. /images/forum/upload/2015-09-09/a578da632e7ca52b04ffc9d66cb7aa1b.PNGWhen i run the below program it is giving error.


Sum of individual digits
Write a C program to find the sum of individual digits of a positive
integer.
Objective: The objective of this exercise is to find the sum of individual
digits of a positive integer.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int num, k=1, sum=0;
clrscr();
printf("Enter the number whose digits are to be added:");
scanf("%d",&num);
while(num!=0)
{
k=num%10;
sum=sum+k;
k=num/10;
num=k;
}
printf("Sum of the digits : %d", sum);
getch();
}
Expected Output:
Enter the number whose digits are to be added
2 3
Output
5

Post a Reply

Replies

Oldest  Newest  Rating
0 Dara Shorten · September 9, 2015
Change int to float, int only deals with whole numbers that's why you don't get 2.3. int can only deal with numbers like 1 2 3 can't deal with 1.2 or in your case 2.3
0 Jon Z. · September 10, 2015
change void to int and make it return 0 at the end. that's the standard practice.
int main()
{
//...
return 0;
}
0 Sandra T · September 10, 2015
#include <stdio.h>
#include <stdlib.h>

int main()
{
int num, k=1, sum=0;
printf("Enter the number whose digits are to be added:");
scanf("%d",&num);
while(num!=0)
{
k=num%10;
sum=sum+k;
num=num/10;
}
printf("Sum of the digits : %d", sum);
return 0;
}


MAKE THE CHANGES THAT I'VE MADE AND YOUR PROGRAM WILL WORK!
0 Milburn camilo · September 30, 2015
Thank you all. 
  • 1

C

107,245 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator