#include<iostream>
int sum_digits(int num, int base=10)
{
int sum=0;
while( num )
{
sum+=num%base;
num/=base;
}
return(sum);
}
int main()
{
int sum=sum_digits(42);
// assert( sum==6);
}
#include<stdio.h>
int sumOfDigit(int x){
int ans = 0;
while(x>0){
ans += (x%10);
x /= 10;
}
return ans;
}
int main(void){
printf("%d",sumOfDigit(12));
return 0;
}
if we have five digits then this may be the solution
#include <stdio.h>
int main(void)
{
int i,temp = 0;
printf("Enter a +ve 5 digit number :");
scanf("%d",&i);
if (( i <= 99999 ) && ( i > 9999 ))
{
printf(" %d",(i/10000) + 1);
printf(" %d",((( i/100) % 100)/10) + 1);
printf(" %d",(( i/100) % 10 ) + 1);
printf(" %d",((i % 100) / 10) + 1 );
printf(" %d",(i % 10) + 1);
}
else
printf("\n Pl.enter +ve five digit number");
return 0;
}
int main() {
int num, sum;
printf ("Enter a positive integer: ");
scanf ("%d", num);
sum = 0;
while (num>0) {
sum += num%10;
num/=10;
}
printf ("The sum of the digits is: %d\n", sum);
return 0;
}
for (digsum= 0; n!=0; digsum+=n%10, n/=10);
One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.
5
Write a program to find the number and sum of all integers from 100 to 300 that are divisible by 11
write a vb program to find the magic square
The tricky part is getting the individual digits. There are basically two ways to do this: 1) Convert the number to a string, and use string manipulation to get the individual digits. 2) Repeatedly divide the number by 10. The digit is the remainder (use the "%" operator). To actually get the highest digit, initially assume that the highest digit is zero (store this to a variable, called "maxDigit" or something similar). If you find a higher digit, replace maxDigit by that.
no thanks
One way to do this is to convert the number to a String, then use the corresponding String method to find out the length of the String.
#include <stdio.h> int main(int argc, char **argv) { if (argc<1) { printf("Usage: %s number\n",argv[0]); return -1; } int digits=1, i=atoi(argv[1]); while (i/=10) ++digits; printf("%d\n",digits); }
There are many shell programs that will find the sum of the square of individual digits of a number. A small example is: SD=3n=2, sum=2, and SD=2.
5
k
class Sum_Of_Digits { public static void printSumandnoofdigits(int n) { int temp = n; int count = 0; int sum = 0; while ( n > 0 ) { sum = sum + n % 10; n = n / 10; count ++; } System.out.println("The number is..." + temp ); System.out.println("The sum of digits is..." + sum); System.out.println("The number of digits is..." + count); } }
Shell problems are programs that can be run to find out information about numbers. The problem can help find an even or odd number, or what the sum of a cube is.
You can use the Math.sqrt() method.
Yes, do write, or if you're too lazy to your homework, use google.
Write your own prime number program and find out.
Add the digits together. The sum of the digits of 23 is 5.