CODING JOY

The stunning potpourri of coding and mundane life

ANGEL NUMBER IN JAVA LANGUAGE



AMRUHA AHMED
5th October,2022.



An angel number is a number in which there is an uncanny repitition of digits, giving a feeling of a divine message for some people.

blog1

The following Java program checks whether the given number is an angel number or not:


METHOD-1:

                
                    import java.util.*;
                    public class angelnumber
                    {
                        
                        public static void main(String args[])
                        {
                            Scanner ob=new Scanner(System.in);
                            int n;//accepted number
                            int d;//dummy variable
                            int flag=0;//flag variable for checking equality of digits
                            char c1;//gives current character
                            char c2;//gives succeeding character
                            int i=0;//loop counter
                            System.out.print("Enter a number:");
                            n=ob.nextInt();
                            String s=n+"";
                            int l=s.length();
                            while(i<l-1)
                            {
                                c1=s.charAt(i);
                                c2=s.charAt(i+1);
                                if(c1!=c2)
                                {
                                    flag=1;
                                    break;
                                }
                                i++;
                            }
                            if(flag==1)
                            System.out.println(n+" is not an Angel Number");
                            else
                            System.out.println(n+" is an Angel Number");
                            
                    
                            
                        }
                    }


             
         
CODE COPIED

DRY RUN:


Supposing n is given as 777. Then string s will look as follows:
blog1
Then the process occuring in the while loop looks as follows:
blog1
After this the while loop ceases.Since flag is still 0, 777 is said to be an angel number.



OUTPUT:

Enter a number:777

777 is an Angel Number


OUTPUT:

Enter a number:232

232 is not an Angel Number


METHOD-2:

                       
import java.util.*;
public class angelnumber
{
    
    public static void main(String args[])
    {
        Scanner ob=new Scanner(System.in);
        
        int n;//accepted number
        int[] a = new int[20];//array to store digits of the number
        int i=0;//counter to store digit in the array
        int j;//loop counter for checking
        int d;//dummy variable
        int flag=0;//flag to check if the consecutive digits of the array are equal to each other or not
        System.out.print("Enter a number:");
        n=ob.nextInt();
        d=n;
        while(n!=0)
        {
            a[i++]=n%10;
            n=n/10;
        }
        for(j=0;j<i-1;j++)
        {
            if(a[j]==a[j+1])
            continue;
            else
            {
                flag=1;
                break;
            }
        
        }
        if(flag==1)
        System.out.println(d+" is not an Angel Number");
        else
        System.out.println(d+" is an Angel Number");
    }
}


                    
                

DRY RUN:


Supposing the input n is provided as 232.After the while loop,variable i will have the value 3 and the contents of array a[] are as follows:
blog1
The process taking place in the for loop is as follows:
blog1 After this, the for loop ceases.Since flag is 1, 232 is not an angel number.



OUTPUT:

Enter a number:777

777 is an Angel Number


OUTPUT:

Enter a number:232

232 is not an Angel Number