Skip to main content

Java Programs For Various Operations On Numbers | Counting, Sum, Comparision

 Java Programs From Various Operations On Numbers | Counting, Sum, Comparision

===========================================================================
1) Count Number Of Digits In a Number.
---------------------------------------------------

import java.util.Scanner;

public class CountNumberOfDigits {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Number:");
        Integer num = sc.nextInt();
        Integer count = 0;
        while (num > 0) {
            num = num / 10;
            count++;
        }
        System.out.println("The Number Of Digits In a Number Are:" + count);

    }

}

2) Java Program for doing Sum Of Digit's In a Number.
----------------------------------------------------------

import java.util.Scanner;

public class SumOfDigitsInNumber {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Number:");
        Integer num = sc.nextInt(); // 123
        Integer sum = 0;

        while (num > 0) {
            sum = sum + num % 10;
            num = num / 10;

        }
        System.out.println("The Sum Of Digits In A Number:" + sum);

    }

}

3)Java Program To Count Number Of Even and Odd Digits In A Number
-----------------------------------------------------------------------
import java.util.Scanner;

public class CountEvenAndOddDigits {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the Number");
        Integer num = sc.nextInt(); // Read the User Input
        Integer evenCount = 0; // To Count the Even Digits From A Number
        Integer oddCount = 0; // To Count the Odd Digits From A Number
        while (num > 0) {
            int remainder = num % 10;
            if (remainder % 2 == 0) {
                evenCount++;
            } else {
                oddCount++;
            }
            num = num / 10;

        }

        System.out.println("The Number Of Even Digits In Number:" + evenCount);
        System.out.println("The Number Of Odd Digits In a Number:" + oddCount);

    }

}


4) Find Out Largest Of Three Numbers In Java (Approach 1 - Using Comparision Operator)
--------------------------------------------------------------------------------------
import java.util.Scanner;

public class LargestOfThreeNumbers1 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the First Number:");
        Integer a = sc.nextInt();

        System.out.println("Enter the Second Number:");
        Integer b = sc.nextInt();

        System.out.println("Enter the Third Number:");
        Integer c = sc.nextInt();

        if (a > b && a > c) {
            System.out.println(a + " Number is the Greatest Of All Three");

        } else if (b > a && b > c) {
            System.out.println(b + " Number is the Greatest Of All Three");
        } else {
            System.out.println(c + " Number is the Greatest Of All Three");
        }

    }

}


5) Find Out Largest Of Three Numbers In Java (Approach 1 - Using Ternary Operator)
------------------------------------------------------------------------------------

import java.util.Scanner;

public class LargestOfThreeNumbers2 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("Enter the First Number:");
        Integer a = sc.nextInt();

        System.out.println("Enter the Second Number:");
        Integer b = sc.nextInt();

        System.out.println("Enter the Third Number:");
        Integer c = sc.nextInt();
        /*
       
         * Integer largest1=a > b ? a:b;
         * Integer largest2 =c > largest1 ? c: largest1;
         * System.out.println("The Largest Of Three Number is:" + largest2);
         */

        Integer largest = c > (a > b ? a : b) ? c : (a > b ? a : b);
        System.out.println("The largest Of Three Numbers is :" + largest);

    }

}













Comments

Popular posts from this blog

Java Programs Asked In An Interview | Java 8 | Core Java

 Q1) // Write a Java Program To Find Out the Character Occurnaces In A String. import java . util . HashMap ; import java . util . Map ; public class CharOccurnacesInString {     // Write a Java Program To Find Out the Character Occurnaces In A String.     public static void main ( String [] args ) {         String str = "I am Sandeep Aswar" ;         str = str . replaceAll ( " " , "" ). toLowerCase ();         char [] ch = str . toCharArray ();         Map < Character , Integer > charMap = new HashMap <>();         for ( char c : ch ) {             if ( charMap . containsKey ( c )) {                 charMap . put ( c , charMap . get ( c ) + 1 );             } else {                 charMap . pu...

Java 8 Programs Asked In Interview For Experienced Professionals | Java 8 Coding Interview Questions Asked In The Interview.

 Q1) // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different. package J ava C oncept O f D ay ; import java . util . stream . Collectors ; import java . util . stream . Stream ; public class AnagramStrings {     // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different.     public static void main ( String [] args ) {         String str1 = "Listen" ;         String str2 = "Silent" ;         str1 = Stream . of ( str1 . split ( "" )). map ( String :: toLowerCase ). sorted (). collect ( Collectors . joining ());...

How many primitive data types are there in Java?

Java includes a set of fundamental data types known as primitive data types. These data types are the essential components for storing and managing various kinds of data, including numbers, characters, and boolean values. Java encompasses a total of eight primitive data types, and this article aims to provide an in-depth explanation of each type in a simplified manner. 1.      byte: The byte data type is designed for storing small integer values, with a range spanning from -128 to 127. 2.      short: Slightly more accommodating than byte , the short data type can be employed for storing relatively larger integer values, ranging from -32,768 to 32,767. 3.      int: Among the most commonly used primitive data types, int can hold larger integer values, with a range extending to approximately -2 billion to 2 billion. 4.      long: When dealing with very large integer values, the long data t...