Computer Science, asked by sushmita1233, 10 months ago

Wap to enter a number and check if it is a happy number or not.

Using java and function parameter

Don't use scanner class



Urgent​


rakeshchennupati143: there you go i wrote as you said

Answers

Answered by suneelgoli
1

Answer:

public class HappyNumber  

{    

   //isHappyNumber() will determine whether a number is happy or not  

   public static int isHappyNumber(int num){  

       int rem = 0, sum = 0;  

         

       //Calculates the sum of squares of digits  

       while(num > 0){  

           rem = num%10;  

           sum = sum + (rem*rem);  

           num = num/10;  

       }  

       return sum;  

   }  

     

   public static void main(String[] args) {  

       int num = 82;  

       int result = num;  

         

       while(result != 1 && result != 4){  

           result = isHappyNumber(result);  

       }  

         

       //Happy number always ends with 1  

       if(result == 1)  

           System.out.println(num + " is a happy number");  

       //Unhappy number ends in a cycle of repeating numbers which contains 4  

       else if(result == 4)  

           System.out.println(num + " is not a happy number");    

   }  

}  

Explanation:


amannishad0512p5zxh6: yaa
amannishad0512p5zxh6: i not know python
amannishad0512p5zxh6: see my profile
amannishad0512p5zxh6: java learner
amannishad0512p5zxh6: i want to learn python
amannishad0512p5zxh6: but bro, don't kept ego you know python
amannishad0512p5zxh6: python if i know
amannishad0512p5zxh6: i will surely tell you
amannishad0512p5zxh6: i will tell logic
amannishad0512p5zxh6: write down in python yourself
Answered by rakeshchennupati143
1

Happy number:

the number is said to be happy number if the square of sum of the products of given number is equal to 1, if not then again take sum of the number that came. for example

13 = 1² + 3² = 10 ( not equal to 1 so again )

10 = 1² + 0² = 1( so it is happy number)

for example

69 = 6²+9² = 36 + 81 = 117 (not equal to 1)

117 = 1² + 1² + 7² = 1 + 1 + 49 = 51 ( not equal to 1)

51 = 5² + 1² = 25 + 1 = 26 ( not equal to 1)

26 = 2² + 6² = 3 + 36 = 40 ( not equal to 1)

40 = 4² + 0² = 16 (not equal to 1)

16 = 1² + 6² = 37 (not equal to 1)

37 = 3² + 7² = 9 + 49 = 58( not equal to 1 )

58 = 5² + 8² = 25 + 64 = 89(not equal to 1)

89 = 8² + 9² = 64 + 81 = 145(not equal to 1)

145 = 1² + 4² + 5² = 1 + 16 + 25 = 42(not equal to 1)

42 = 4² + 2² = 16 + 4 = 20 (not equal to 1)

20 = 2² + 0² = 4 (here we got 4 so it is not a happy number)

Program:

import java.io.*;

public class Main{

     public static void main(String[] args) throws IOException {

           InputStreamReader r = new InputStreamReader(System.in);    

           BufferedReader br = new BufferedReader(r);

           System.out.println("Enter a number");

           int num = Integer.parseInt(br.readLine());

           checkHappy(num);

     }

     public static void checkHappy(int number){

           int temp = number,reminder,square = 0,check = 0;

           while( check != 1 && check != 4){

                 while(number>0){

                       reminder = number % 10;

                       square = square + (reminder * reminder);

                       number = number / 10;

                 }

                 number = square;

                 check = square;

                 square = 0;

           }

           if(check == 1){

                 System.out.println(temp+" number is a Happy number");

           }else{

                 System.out.println(temp+" number is not a Happy number");

           }

     }

}

Explanation:

  • i used parameter function to check weather the number is a Happy number or not
  • in function i used nested loop to check the number when it is not equal to 1 or not equal to 4
  • if the number is equal to 4 the loop breaks and then prints the number is not a happy number
  • as per to the definition of happy number when the number sum of the square of the number is equal to 4 then it is not a happy number
  • so the loop gets iterates and takes sum of squares until it get either 1 or 4
  • so if the square gets 1 then it is a happy number
  • if not then it is not a happy number

----Hope you liked my program and my explanation, if you liked it mark brainliest, it will really helps me   :)

Similar questions