Click here to Skip to main content
15,907,874 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There will be an array of elements. Add each digit of element and print the sum of each element.    
Input:
32
11
Expected Output:
5
2
I am getting output as 7. How can i solve this?  


What I have tried:

Java
import java.util.Scanner;

public class Main

{

    public static void main(String args[])

    {

        int n, sum = 0;

        Scanner sc = new Scanner(System.in);
        int size=sc.nextInt();
        System.out.print("Enter the number:");
        int a[]= new int[size];
        for(int i=0;i<size;i++)
        {
            a[i]=sc.nextInt();
        }
        for(int i=0;i<size;i++)
        {
         for(int j=0;j<size;j++)
         {
        while(a[j] > 0)

        {

            n = a[j] % 10;

            sum = sum + n;

            a[j] = a[j] / 10;


        }
         }

        }

        System.out.println("Sum of Digits:"+sum);
        }
}
Posted
Updated 8-Oct-22 22:43pm
v2
Comments
Engineer shahbaz 9-Oct-22 6:00am    
import java.util.Scanner;

public class SumD {
public static void main(String[] args) {
int n;
int sum=0;
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int ar[]=new int[size];
for (int i = 0; i < ar.length; i++) {
ar[i]=sc.nextInt();
}
for (int i = 0; i < ar.length; i++) {
for (int j = 0; j <= i; j++) {
while (ar[j]>0) {
n=ar[j]%10;
sum=sum+n;
ar[j]=ar[j]/10;
}
}
System.out.println(sum);
sum=0;
}
}
}
//this is another solution to what i have given below

This is just a simpler version of your previous question:
Sum of a digit at even and odd places in an array[^]
 
Share this answer
 
Comments
CPallini 13-Nov-20 2:27am    
5.
Think about the logical steps you need to perform:
- read the next number
- set the sum to zero
- calculate the sum of the digits in the input number
- print the sum
- repeat for the next input
 
Share this answer
 
Comments
Devashree Sen 12-Nov-20 12:25pm    
Thank you so much.
CPallini 13-Nov-20 2:27am    
5.
Quote:
Expected Output:
5
2
I am getting output as 7. How can i solve this?

Your actual output is the sum of expected output.
Just a guess:
- you print result in wrong place.
- you forgot to reset the sum to 0.
-----
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
import java.util.Scanner;
public class Main
{
    public static void main(String args[])
    {
        int n, sum = 0;

        Scanner sc = new Scanner(System.in);
        int size=sc.nextInt();
        System.out.print("Enter the number:");
        int a[]= new int[size];
        for(int i=0;i<size;i++)
        {
            a[i]=sc.nextInt();
        }
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            {
                while(a[j] > 0)
                {
                    n = a[j] % 10;
                    sum = sum + n;
                    a[j] = a[j] / 10;
                }
            }
        }
        System.out.println("Sum of Digits:"+sum); // wrong place
    }
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
Comments
CPallini 13-Nov-20 2:27am    
5.
Patrice T 13-Nov-20 2:35am    
Thank you.
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int n, sum = 0;

Scanner sc = new Scanner(System.in);
int size=sc.nextInt();
System.out.print("Enter the number:");
int a[]= new int[size];
for(int i=0;i<size;i++)
{
="" a[i]="sc.nextInt();
" }
="" for(int="" i="0;i<size;i++)
" j="0;j<size;j++)
" while(a[j]=""> 0)
{
n = a[j] % 10;
sum = sum + n;
a[j] = a[j] / 10;
}
System.out.println("Sum of Digits:"+sum);
sum=0;
}
}

}
}
 
Share this answer
 
import java.util.Scanner;
class SumDigits {
	public static void main(String []args)
	{
	ArrayOperation1 ao=new ArrayOperation1();
	int x[]=ao.readArray();
	System.out.println("User Enterred array is");
	ao.displayArray(x);
	System.out.println();
	int []rs=ao.isSumOfDigits(x);
	System.out.println("The sum of digits of elements in an array is");
	for (int i = 0; i < rs.length; i++) {
		System.out.print(rs[i]+" ");
	}
  }
}

 class ArrayOperation1 {
	public int[] readArray() {
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter size of array");
		int n=sc.nextInt();
		int ar[]=new int[n];
		System.out.println("enter the elements");
		for (int i=0;i<ar.length;i++)
		{
		ar[i]=sc.nextInt();
		}
		return ar;	
	}
	void displayArray(int []x) {
		for (int i = 0; i < x.length; i++) {
			System.out.print(x[i]+" ");
		}
	}
	public int[] isSumOfDigits(int[] x) {
		int ar[]=new int[x.length];
		for (int i = 0; i < x.length; i++) {
			int r=update(x[i]);
			ar[i]=r;
			}
		return ar;
		}
	 int update(int y) {
		 int sum=0;
		while (y>0) {
			int re=y%10;
			sum=sum+re;
			y=y/10;
		}
		return sum;
	}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900