Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Numerical AnalysisRSS Feeds

Program to calculate anybody’s single digit lucky number

Posted By: Easy Tutor     Category: C++ Programming     Views: 6237

Write a program to calculate anybody’s single digit lucky number using the following scenario;

- Replace all alphabets of anybody’s name with number of that alphabet e.g. replace ‘a’ with 1, ‘b’ with 2 and ‘c’ with 3.
- Add all numbers until it becomes a single digit e.g.

For name Annu = 1+14+14+21
= 50
= 5+0
= 5
so, Annu’s lucky number is 5.

Note:
Print invalid if the name contains a character other than alphabets e.g. Space, Dot, Numbers, etc. The input will contain multiple test cases and will end on the line that contains 0.

Code for Program to calculate anybody’s single digit lucky number in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>
 # include <ctype.h>

 int main( )
    {
       clrscr( );

       fstream File("CP-07.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"Unable to open the input file."<<endl;
         cout<<"Press any key to exit."<<endl;

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[100]={NULL};

       do
      {
         strset(Data,NULL);

         File.getline(Data,80,'\n');

         if(strcmpi(Data,"0")==0)
        break;

         int flag=0;
         int name_length=strlen(Data);

         int code_array[100]={0};

         for(int count_1=0;count_1<name_length;count_1++)
        {
           if(!isalpha(Data[count_1]))
              {
             flag=1;

             break;
              }

           else
              {
             Data[count_1]=toupper(Data[count_1]);

             code_array[count_1]=(int(Data[count_1])-64);
              }
        }

         if(!flag)
        {
           int lucky_number=0;

           for(int count_2=0;count_2<name_length;count_2++)
              lucky_number+=code_array[count_2];

           char Lucky_number[100]={NULL};

           while(lucky_number>9)
              {
             strset(Lucky_number,NULL);
             itoa(lucky_number,Lucky_number,10);

             lucky_number=0;

             int number_length=strlen(Lucky_number);

             char Digit[5]={NULL};

             for(int count_3=0;count_3<number_length;count_3++)
                {
                   Digit[0]=Lucky_number[count_3];

                   lucky_number+=atoi(Digit);
                }
              }

           cout<<lucky_number<<endl;
        }

         else
        cout<<"invalid"<<endl;
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program to calculate anybody’s single digit lucky number is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!