Advertisements

Aatul Palandurkar

Posts Tagged ‘Best Corporate Trainer for 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


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);
}
}

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


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);
}
}
}

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.

Advertisements
CDAC-GTU-BISAG WIMC Batch

BISAG-CDAC-GTU WIMC Batch

Conducted Java Training at CDAC for M.Tech. students at Bhaskaracharya Institute of Space Applications and Geo-Informatics (BISAG), Info City, Gandhinagar, Gujrat under Gujrat Technical University (GTU). Seen a great collaboration between BISAG, CDAC and GTU, never seen such program for students where are students getting chance to work on research projects and all.

During the training covered Java, Java EE, Struts, Hibernate, Spring, EJB, RMI, Networking, etc. all in 5 days daily 8 hours. It helps a lot to cover that much if you use NetBeans for training Java and other Java related technologies like JavaEE, Struts, Hibernate, Spring, HTML5, etc. Students were good.

I feel honored after receiving Diwali Gift and Dinner party from students. I would also like to thanks them for Akshardham visit and really love the fountain and water show at Akshardham. There are only 4 water shows like that all over the world. I really enjoyed a lot guys thanks a lot. And I would like to share the thing that you are doing for physically and mentally challenged people, I have never seen this from students.

Objectives :

  • Why Java is platform independent?
  • How java program executes?
  • What is execution flow of Java Program?
  • What are the steps to execute a Java Program?
  • What are the steps to run a Java Program?

Advertisements

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 13,210 other followers

Tweets

YouTube

Archives

Advertisements
%d bloggers like this: