# java code example

## Program to find Sum of Digits in Java

Objectives :

• Write a program to compute sum of digits of a given number.
• Write a program to find sum of digits of a given number.
• Write a program to calculate sum of digits of a given number.
• Write a program to compute sum of digits of a given integer number.
• Write a program to compute sum of digits of a number entered via Command Line.
• Write a program to compute sum of digits of a given number. Take input from Command Line.
• Write a program to compute sum of digits of a given number. Take input using Scanner class.

Following is the Java Program to compute Sum of Digits of a given integer number;

Method 1 : Java Program to find Sum of Digits when number is entered from command line

[sourcecode lang=”java”]

class SumOfDigits
{
public static void main(String args[])
{
int n;
int a=0;
int sum=0;

//taking integer number from command line and parsing the same
n=Integer.parseInt(args[0]);

while(n!=0)
{
a=n%10;
n=n/10;
sum=sum+a;
}
System.out.println("Sum of digits: " + sum);
}
}

[/sourcecode]

Steps to run above program via command line :

1. Compilation : C:\JavaPrograms>javac SumOfDigits.java
2. Interpretation : C:\JavaPrograms>java SumOfDigits 12345

Output : Sum of digits: 15

Method 2 : Java Program to find Sum of Digits if input is taken using Scanner class

[sourcecode lang=”java”]

import java.util.Scanner;

public class SumOfDigits {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n;
int a=0;
System.out.print("Enter a positive number: ");
n = in.nextInt();

if (n <= 0)
System.out.println("You have entered a negative number.");
else {
int sum = 0;

while (n != 0) {

a=n%10;
n=n/10;
sum=sum+a;
}
System.out.println("Sum of digits: " + sum);
}
}
}

[/sourcecode]

Steps to run above program via command line :

1. Compilation : C:\JavaPrograms>javac SumOfDigits.java
2. Interpretation : C:\JavaPrograms>java SumOfDigits 12345

Output :

Trial 1 : With positive number

Enter a positive number: 12345

Sum of digits: 15

Trial 2 : With negative number

Enter a positive number: -12345

You have entered a negative number.

Objectives :

• How to set TextView in Android?
• How to add TextView in Android?
• How to add TextView in Android via Java program?
• How to set Layout parameters from Java code in Android?

TextView is a one of the Views available in Android. There are two (2) ways to add TextView in your Android application as follows :

1. Using XML code
2. Using Java code

### XML Code example – Adding to a TextView in a layout xml file

[sourcecode language=”xml”]
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World" />

</LinearLayout>
[/sourcecode]

### Java Code example – Creating and adding a new TextView to a Layout

[sourcecode language=”java”]
// Creates object of TextView
TextView textView = new TextView(this);
// Set text to display in TextView on Screen
textView.setText(“Hello World”);
// Add TextView to Linear Layout with layout parameters
((LinearLayout)findViewById(R.id.mainLayout))