Write a java program to display the histro pair of the given number. For a
number N, its histro pair is made by adding the cube of the odd digits.
Example:
N= 43558 then its histro pair is 33+53+53 = 27+125+125 = 277
Answers
Answer:
import java.util.*;
class histro //class name
{ //program starts
public static void main (String args[])
{ //main method starts
int num , num1 , digit = 0 ; //variable declaration
double histro = 0.0 ; //variable declaration
Scanner sc = new Scanner(System.in); //creating object for input
System.out.println("Please enter a natural number for printing its Histro pair"); //Asking user for input
System.out.println("Histro pair is the sum of the cube of the odd digits of the number "); //Telling user if the user is not aware of Histro pair
num = sc.nextInt(); //taking number input
num1 = num; //transferring original number in another variable
while(num1!=0) //loop runs till the number is not equal to 0
{ //while loop block
digit = num1%10; //taking out one digit at a time
if(digit%2!=0) //block executed when digit is odd
{ //if block starts
histro = histro + Math.pow(digit,3); //Sum of cube of odd digits and previous value of histro
} //if block ends
num1=num1/10; //removing digits from the number so that loop doesn't runs infinitely
} //while loop ends
System.out.println("Histro pair of "+num+" = "+histro); //printing the histro pair of the given number
} //main method ends
} //program ends