Computer Science, asked by vishwass833, 7 months ago

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

Answered by Anonymous
9

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

\\\\

Variables Used and its usage:

\\\\

\begin{array}{|c|c|c|} \cline{1-3} \sf Variable\; n ame & \sf Data \;type & \sf Function\\ \cline{1-3} \sf num & \sf integer & \sf For\; storing\; the\; number \\ && \sf \;given \;by \;user.\\ \cline{1-3} \sf num1 & \sf integer &  \sf Storing \; the \; original \\ && \sf\; number\; for \;calculating\; the \\ && \sf \;histro\; pair\; of\; the\; given \;number \\ \cline{1-3} \sf digit & \sf integer & \sf for \;storing \;the\; digits \\ && \sf of \;the \;given\; number \;for\; \\ && \sf calculating \;the \;Histro\; pair\\ \cline{1-3} \sf Histro & \sf Double & \sf For \;calculating\; the\; histro \\ && \sf \; pair \;of\; given \;number\\ \cline{1-3}\end{array}

Similar questions