CODING JOY

The stunning potpourri of coding and mundane life

ANGEL NUMBER IN C++ LANGUAGE



AMRUHA AHMED
2nd 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 C++ program checks whether the given number is an angel number or not:


METHOD-1:

                
                    #include<iostream>
                    #include<cstring>
                    using namespace std;
                    int main()
                    {
                        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
                        cout<<"Enter a number:";
                        cin>>n;
                        string s=to_string(n);//converting number to string 
                        int l=s.length();//length of the string
                        cout<<l;
                        while(i<l-1)
                        {
                            c1=s.at(i);
                            c2=s.at(i+1);
                            
                            if(c1!=c2)
                            {
                                flag=1;
                                break;
                            }
                            i++;
                        }
                        if(flag==1)
                        cout<<endl<<n<<" is not an Angel Number";
                        else
                        cout<<endl<<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:

                       
                        #include<iostream>
                        using namespace std;
                        int main()
                        {
                            int n;//accepted number
                            int a[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
                            cout<<"Enter a number:";
                            cin>>n;
                            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)
                            cout<<d<<" is not an Angel Number";
                            else
                            cout<<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