Click here to Skip to main content
15,887,422 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
WAP to input a number, check and print if it a MAGIC number or not. To find out if a
number is a MAGIC number or not add its digits. Now add the digits of the sum obtained.

Repeat this process till you get a single digit number. If you get 1 in the end the given
number is a MAGIC number without using boolean, return,String[] args and return statement for the isHappyNumber() method: and isHappyNumber(number))

What I have tried:

import java.util.*;
 class b1
{ public static void main ()
    {Scanner sc= new Scanner(System.in);
        int sum = 0;
    System.out.print("Enter a Number");
    int n=sc.nextInt();
    do {
     int c=n%10;
     int s=c*c;
      sum = sum+s;
       n=n/10;
    }
    while(sum==1);
    {if(sum==1)
    System.out.print("It is a Happy Number");
    else
    System.out.print("It is not a Happy Number");
    }}
}
Posted
Updated 11-Jul-23 23:16pm
Comments
Richard MacCutchan 12-Jul-23 5:16am    
You are summing the squares of each digit, which is not what the question asks.

The below should solve your problem, see fiddle at - Computer question from java[^]

JavaScript
import java.util.Scanner;

public class Main {
    //To compile properly I added 'public static void main' as I am not sure if you have a public class with name 'Main'' 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int sum = 0;
        System.out.print("Enter a Number: ");
        int n = sc.nextInt();
        do {
            //Get the last digit of the number
            int c = n % 10; 
            //Square the digit
            int s = c * c; 
            //Add the squared digit to the sum
            sum = sum + s; 
            //Remove the last digit from the number
            n = n / 10; 
            //Repeat until the sum becomes a single-digit number
        } while (sum > 9); 
        
        if (sum == 1) {
            System.out.print("It is a Happy Number");
        } else {
            System.out.print("It is not a Happy Number");
        }
    }
}


With the above, keep in mind that the code will error if you have a number that exceeds the range of the 'int' data type in Java (i.e. 11111111111112), which is limited to the range -2,147,483,648 to 2,147,483,647. You then need to use the 'long' data type instead. To use 'long' -
JavaScript
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long sum = 0;
        System.out.print("Enter a Number: ");
        long n = sc.nextLong();
        do {            
            long c = n % 10; 
            long s = c * c; 
            sum = sum + s; 
            n = n / 10; 
        } while (sum > 9); 
        
        if (sum == 1) {
            System.out.print("It is a Happy Number");
        } else {
            System.out.print("It is not a Happy Number");
        }
    }
}
 
Share this answer
 
First off, indent you code properly: pick a style and be consistent - it makes your code more readable:
Java
import java.util.*;
class b1 {
   public static void main() {
      Scanner sc = new Scanner(System.in);
      int sum = 0;
      System.out.print("Enter a Number");
      int n = sc.nextInt();
      do {
         int c = n % 10;
         int s = c * c;
         sum = sum + s;
         n = n / 10;
      }
      while (sum == 1); {
         if (sum == 1)
            System.out.print("It is a Happy Number");
         else
            System.out.print("It is not a Happy Number");
      }
   }
}
Then look at your instructions - they tell you what to do:
1) To find out if a number is a MAGIC number or not add its digits.
2) Now add the digits of the sum obtained.
3) Repeat this process from (2) until you get a single digit number.
4) If you get 1 in the end the given number is a MAGIC number
5) It would appear this should be a method rather than direct in main.

Your code doesn't do that: It exit the loop as soon as the sum isn't one, regardless of the number of digits entered. So if I enter "11111111111112" it will process one single digit: "2" and exit the do loop. Think about how you detect you are out of digits ... it's pretty obvious!

I'd start by writing a "helper method" to take an integer parameter, and return the sum of it's digits.
Then call that in a loop until you have a single digit remaining.

Always sit down and think before you leap into code: it will save you a huge amount of wasted time. This may help you: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900