Java Count, Sum, Power & Round

  1. Count digits of Given number in Java
  2. Sum digits of Given number in Java
  3. Power of Given number in Java
  4. Round the value of Given number in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Count digits of Given number in Java


CSP_Ex1.java

import java.util.Scanner;
 
public class CSP_Ex1 {
	
	Scanner scan;
	int num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		
		System.out.println("Count the digits of given Number");
		
		System.out.println("\nEnter the number");		
		num = Integer.parseInt(scan.nextLine());
	}
	
	void display() {
		
		int temp = num;
		int count = 0;
 
		while(temp != 0) {
			
			temp = temp / 10;
			count ++;
		}
		
		System.out.println("The Digit Count is : " + count);		
	}
}
 
class MainClass {
	
	public static void main(String args[]){
		
		CSP_Ex1 obj = new CSP_Ex1();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

Count the digits of given Number

Enter the number
589461256
The Digit Count is : 9




B. Sum digits of Given number in Java


CSP_Ex2.java

import java.util.Scanner;
 
public class CSP_Ex2 {
	
	Scanner scan;
	int num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		
		System.out.println("Sum the digits of given Number");
		
		System.out.println("\nEnter the number");		
		num = Integer.parseInt(scan.nextLine());
	}
	
	void display() {
		
		int temp = num;
		int sum = 0;
		int mod = 0;
 
		while(temp != 0) {
			
			mod = temp % 10;
			sum = sum + mod;
			temp = temp / 10;
		}
 
		System.out.println("The Digit Count is : " + sum);		
	}
}
 
class MainClass {
	
	public static void main(String args[]){
		
		CSP_Ex2 obj = new CSP_Ex2();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

Sum the digits of given Number

Enter the number
123456
The Digit Count is : 21




C. Power of Given number in Java


CSP_Ex3.java

import java.util.Scanner;
 
public class CSP_Ex3 {
	
	Scanner scan;
	int num, power;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		
		System.out.println("Power of given Number");
		
		System.out.println("\nEnter the number");		
		num = Integer.parseInt(scan.nextLine());
		
		System.out.println("Power of :");		
		power = Integer.parseInt(scan.nextLine());
	}
	
	void display() {
		
		int sum = 0;
		
		sum = (int) Math.pow(num, power);
		
		System.out.println("The Digit Count is : " + sum);		
	}
}
 
class MainClass {
	
	public static void main(String args[]){
		
		CSP_Ex3 obj = new CSP_Ex3();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

Power of given Number

Enter the number
25
Power of :
2
The Digit Count is : 625




D. Round the value of Given number in Java


CSP_Ex4.java

import java.util.Scanner;
 
public class CSP_Ex4 {
	
	Scanner scan;
	float num;
	
	void getVal() {
		
		scan = new Scanner(System.in);
		
		System.out.println("Round of given Number");
		
		System.out.println("\nEnter the number");		
		num = Float.parseFloat(scan.nextLine());
	} 
	
	void display() {
		
		float round = 0;
		
		round = Math.round(num);
		
		System.out.println("The Digit Count is : " + round);		
	}
}
 
class MainClass {
	
	public static void main(String args[]){
		
		CSP_Ex4 obj = new CSP_Ex4();
		
		obj.getVal();
		obj.display();
	}
}

Sample Output

Round of given Number

Enter the number
11.49
The Digit Count is : 11.0
Round of given Number

Enter the number
11.50
The Digit Count is : 12.0






SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com