Lucky number though your name (Just for Fun) in C++.

//Create a lucky number of your name using ASCII values.
//E.g.: HELLO:  H=8, E=5, L=12, L=12, O=15.
//Sum of 8 + 5 + 12 + 12 + 15 = 52 .
//Sum of 5 + 2 = 7. Lucky Number is 7.

#include<iostream>
#include<string.h>
#include<ctype.h>

using namespace std;

int getSum(char[],int);

int main()
{
char name[40];
int num, i, len;
cout<<"=================================================\n";
cout<<"  LUCKY NUMBER THROUGH YOUR NAME (JUST FOR FUN)\n";
cout<<"=================================================\n";
cout<<"Enter your name: ";
cin.getline(name,40);
len=strlen(name);
num=getSum(name,len);
cout<<"\nYour Single Digit Lucky Number is "<<num<<endl;
cout<<"=================================================\n";
return 0;
}

int getSum(char nm[], int n)
{
int i, s=0, temp;
for(i=0;nm[i]!='\0';i++)
{
if(isalpha(nm[i]))
{
nm[i]=toupper(nm[i]);
cout<<nm[i]<<" = "<<(int)nm[i]-64<<"\t";
s=s+(int)nm[i]-64;
}
}
cout<<"\nTotal Sum of Your Name is: "<<s<<endl;

summarize:
i=0;
while(s!=0)
{
temp=s%10;
i=temp+i;
s=s/10;
}

if (i>=10)
{
s=i;
goto summarize;
}
return i;
}


No comments

Post your comments

Powered by Blogger.