0% found this document useful (0 votes)
42 views

Import Public Class Public Static Void New Int

This Java program takes a number as input and performs one of three operations on the number based on user selection: 1) Sum of the digits, 2) Number of digits, or 3) Number of unique digits. It uses a switch statement to call the appropriate method - Sum_of_Digits(), No_of_Digits(), or No_of_Unique_Digits() - to perform the selected operation and output the result. Each method uses a while loop to iterate through the digits of the number in order to calculate the requested value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Import Public Class Public Static Void New Int

This Java program takes a number as input and performs one of three operations on the number based on user selection: 1) Sum of the digits, 2) Number of digits, or 3) Number of unique digits. It uses a switch statement to call the appropriate method - Sum_of_Digits(), No_of_Digits(), or No_of_Unique_Digits() - to perform the selected operation and output the result. Each method uses a while loop to iterate through the digits of the number in order to calculate the requested value.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

Scanner;

public class digit {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);


System.out.println("Enter a number..");
int num=sc.nextInt();
System.out.println("What operation to perform..1-Sum of
Digits.. 2-No. of Digits..3-No. of Unique Digits");
int ch=sc.nextInt();

switch(ch)
{
case 1: Sum_of_Digits(num);
break;

case 2: No_of_Digits(num);
break;

case 3: No_of_Unique_Digits(num);
break;

default: System.out.println("Invalid Choice..");

private static void No_of_Unique_Digits(int num) {


int count=0,i=0;
int temp;
int arr[]=new int[12];
while(num>0)
{

arr[i]=num%10;
i++;
num=num/10;
}

for(int j=0;j<i;j++)
{
for(int k=0;k<(i-1);k++)
{
if(arr[k]>=arr[k+1])
{
temp=arr[k];
arr[k]=arr[k+1];
arr[k+1]=temp;
}
}
}

count=0;
for(int k=0;k<i;k++)
{
if(arr[k]!=arr[k+1])
{
count++;
}
}

System.out.println("No. of Unique Digits="+count);

private static void No_of_Digits(int num) {


int count=0;
while(num!=0)
{
count++;
num=num/10;
}
System.out.println("No. of Digits="+count);

private static void Sum_of_Digits(int num) {


int sum=0;
while(num!=0)
{
int rem=num%10;
sum+=rem;
num=num/10;
}
System.out.println("Sum="+sum);

}
}

You might also like