Chargebee
Company
Programming
Program
Write a prgm to add sum of the digits of a number,then again sum that and print the output.
Example:
Sample input:9786456790
Sample output:7
Read Solution (Total 17)
-
- Program in JAVA :
import java.io.*
class solninjava // class name
{
public void main( ) throws IOException //function main used for input
{
BufferedReader std = new BufferedReader(new InputStreamReader(System.in)); //input from console
System.out.print("Enter the Number : ");
int n = Integer.parseInt(std.readLine());
fun(n); // value n send to function fun
}
public void fun(int a) // function fun
{
int s=0; //to store sum of digit
int d;
while(a>0) // it runs till a>0
{
d=n%10;
s=s+d;
a=a/10;
}
if(s>10) // condition if sum is greater then 10
{
fun(s) //then that sum is again send to funtion fun as new number
}
else
{
System.out.println(" Output : "+s); //else print sum as output
}
}
}
Program in PYTHON 2.7.9
"""I have excuted the python program"""
def fun(n):
s=0
while n>0:
d=n%10
s=s+d
n=n/10
if s>10:
fun(s)
else:
print s
no=integer(raw_input("Enter the number : "))
fun(no)
- 9 years agoHelpfull: Yes(4) No(0)
- Python have different syntax as here curly bracket is presented by white space .... As white space are automatically removed in m4maths solution that why whitespace has been removed from python which plays important role ... If u know python then u know where u need to add white spaces ..... Java program do not need changes ... Logic is same for program in both
- 9 years agoHelpfull: Yes(1) No(0)
- In java program please correct one thing ...in function fun ...in while loop ....correct d = a%10 ...do like it thankx
- 9 years agoHelpfull: Yes(0) No(0)
- import java.util.Scanner;
public class Demo4 {
public static void main(String args[])
{
int[] a=new int[10];
int b=0,i,c,d,e;
Scanner sc=new Scanner(System.in);
for(i=0;i - 9 years agoHelpfull: Yes(0) No(1)
- import java.util.Scanner;
public class Demo4 {
public static void main(String args[])
{
int[] a=new int[10];
int b=0,i,c,d,e;
Scanner sc=new Scanner(System.in);
for(i=0;i - 9 years agoHelpfull: Yes(0) No(1)
- #include
int main()
{
long long int n,count,m;
printf("Enter ");
scanf("%lld",&n);
count=n;
while(count%10!=count)
{
n=count;
m=0;
while(n!=0)
{
m=m+n%10;
n=n/10;
}
count=m;
}
printf("%d",m);
return 0;
} - 9 years agoHelpfull: Yes(0) No(0)
- #include
using namespace std;
int func(int n);
int main()
{
int p,f,i=0,sum=0,k;
coutp;
f=func(p);
if(f>10)
{
f=func(f);
cout - 9 years agoHelpfull: Yes(0) No(0)
- Program in Java:
import java.util.Scanner;
public class SumOfNumbers {
private static Scanner scan;
public static void main(String[] args) {
scan = new Scanner(System.in);
System.out.println("Enter the number to be added");
String num = scan.nextLine(); //get input from user as string
long result=0;
while (num.length() > 1) //loop goes till length becomes 1
{
long number=Long.parseLong(num); //converting string to long
long a = 0;
long b=0;
while (number >= 1) //loop goes till number >=1
{
b = number % 10;
a = a + b; //adding digits in given number
number = number / 10;
}
num=Long.toString(a); //again converting it to string to find the length of the digits
result=a; //
}
System.out.println("the sum of numbers is " + result); // printing the result
}
}
- 9 years agoHelpfull: Yes(0) No(0)
- #include
int main()
{
long long int n,sum=0,s=0,r,a;
printf("enter the number");
scanf("%lld",&n);
while(sum)
{ r=n%10;
n=n/10;
sum=sum+r;}
while(sum){
a=sum%10;
sum=sum/10;
s=s+a;}
printf("the sum is %lld",s);
return 0;
}
- 9 years agoHelpfull: Yes(0) No(1)
- int main(n){
s=0;
while(n>0){
t=n;
while(t>0){
t=t%10;
s=s+t;t=t/10;
}n=s;
}
return(s) - 9 years agoHelpfull: Yes(0) No(0)
- #include
#include
void main()
{
long int sum=0,no;
cin>>no;
getch();
cout - 9 years agoHelpfull: Yes(0) No(1)
- Logic :
Result =number%9
I have done this program in Python
num=int(input())
print(num%9)
- 9 years agoHelpfull: Yes(0) No(0)
- Sorry,I have done a mistake in previous solution.
SOLUTION WITH CORRECT LOGIC
num=int(input())
if num > 0 and num % 9 ==0:
print(9)
else:
print(num % 9)
SAMPLE OUTPUT:
INPUT:9999999999
OUTPUT:9
(In previous logic the output will be 0 which is wrong) - 9 years agoHelpfull: Yes(0) No(1)
- class Main{
public void main(String args[]{
int num=9786456790;
int result=num%9;
if(result) System.out.println(result);
else System.out.println(9);
}
} - 9 years agoHelpfull: Yes(0) No(0)
- #include
int n,sum,reminder;
int func(int):
void main()
{
int temp;
printf("enter the any integer number");
scanf("%dt",&n);
temp=n;
func(temp)
}
int func(int t)
{
int j;
while(temp!=0)
{
reminder=temp%10;
sum=sum +reminder;
temp=temp/10;
}
j=sum;
func(j)
printf("sample output:%d",sum)
} - 8 years agoHelpfull: Yes(0) No(0)
- import java.util.Scanner;
public class Summ {
public static long sum(long n)
{
int rem,sum=0;
while(n!=0)
{
rem=(int) (n%10);
sum=sum+rem;
n=n/10;
}
return sum;
}
public static void main(String args[]){
Scanner s=new Scanner(System.in);
long num=s.nextLong();
int sum=(int) sum(num);
if(sum9)
{
sum=(int) sum(sum);
}
System.out.println(sum);
}
}
}
- 8 years agoHelpfull: Yes(0) No(0)
- It's not possible to get such a large value as an integer.
so we should use some other data to get the input.
import java.util.Scanner;
import java.lang.String;
public class MainClass {
public static int digitSum(int num )
{
int sum=0;
while(num>0)
{
sum+=num%10;
num/=10;
}
return sum;
}
public static void main(String []args)
{
String num;
Integer sum=0;
Scanner sc=new Scanner(System.in);
num=sc.next();
for(int i=0;i - 7 years agoHelpfull: Yes(0) No(0)
Chargebee Other Question