CCNumbersListRef CreateNumbersList() { if (g_oddValueLookup == NULL){ g_oddValueLookup = malloc(sizeof(int) * 10); int i; for (i = 0; i < 10; i++){ g_oddValueLookup[i] = SumOfDigits(i * 2); } } CCNumbersListRef list = malloc(sizeof(struct CCNumbersList)); int i; CCNumberNodeRef lastNode = NULL; for (i = 0; i < MAX_NUMBERS; i++){ CCNumberNodeRef node = malloc(sizeof(struct CCNumberNode)); node->nextNode = NULL; if (lastNode == NULL){ list->rootNode = node; }else{ lastNode->nextNode = node; } lastNode = node; } list->length = 0; list->lastNode = lastNode; list->currentNode = list->rootNode; list->nonMaskedNode = NULL; return list; }
bool isHappy(int n) { int count; if(n == 1) { return true; } while(1) { count = CountDigits(n); n = SumOfDigits(n,count); if(n == 1) { return true; } else if(n == 0) { return false; } else { do nothing } } }
void main() { char* num, ch; int i,j = 0,res1 = 0; printf("enter a num:"); //allocating memory dynamically and reading a number as string num = (char*)malloc(sizeof(int)*2000); for (i = 0; (ch = getchar()) != '\n'; i++) num[i] = ch; num[i] = '\0'; //converting char array to integer while (num[j] != '\0') { res1 = res1 * 10 + (num[j] - '0'); j++; } //calling the function to add the digits in the number int res = SumOfDigits(res1); printf("%d", res); _getch(); }
char * CalculateDiscId(const char * directory) { char * * files = GetFilesFromDirectory(directory); char * * file_ptr = files; char offsets[1500], temp_arr[1500]; char * str_ptr = offsets; int total_time = 0; int total_ofset = 2; int som_of_digits = 0; int number_of_tracks = 0; while(*file_ptr != 0) { TagLib_File * file = taglib_file_new(*file_ptr); const TagLib_AudioProperties * audio = taglib_file_audioproperties(file); int length = taglib_audioproperties_length(audio); //printf("Song length: %i -- offset %i \n", length, total_ofset * 75); int chars_printed = sprintf(str_ptr, "%i ", total_ofset * 75); str_ptr = str_ptr + chars_printed; som_of_digits = (SumOfDigits(total_ofset) + som_of_digits) % 255; total_time += length; total_ofset += length; taglib_file_free(file); file_ptr++; number_of_tracks++; } FreeFileList(files); /*printf("Som of digits is: %i\n", som_of_digits); printf("Total time: %i\n", total_time); printf("Number of tracks: %i\n", number_of_tracks); printf("offsets: %s\n", offsets); printf("Total disc length: %i\n", total_time + 2);*/ int total_string_length = sprintf(temp_arr, "%02x%04x%02x %i %s%i\n", som_of_digits, total_time, number_of_tracks, number_of_tracks, offsets, total_time + 2) + 1; char * ret = (char *) malloc (sizeof(char) * total_string_length); strcpy(ret, temp_arr); return ret; }