Write a C program to calculate sum of digits of a given input number.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum; //n=number, r=remainder, sum=sum of digits
//accept number from user
printf("\nEnter number: ");
scanf("%d",&n);
sum=0; //initialize value of sum=0
//separate each digit from number and add it in sum variable
while(n!=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
//printing result
printf("sum is : %d",sum);
getch();
}
-
Next Accept two numbers from user and write a menu driven program to perform the following operations 1. swap the values of two variables 2. calculate arithmetic mean and harmonic mean of two numbers
-
Previous Write a program to accept a decimal number and convert it to binary, octal and hexadecimal number.
0 Comments