number 2 plssssss help !!....using array.. turbo c..

create a program that will accept a number and determine if
it is a happy number or an unhappy number..

example:

enter a number : 7



7*7=49

then 4 and 9

4*4 and 9*9== 16 + 18 gives you 97

then 9 and 7

9*9 and 7*7 == 81 + 49 gives you 130

then 1 and 3

1*1 and 3*3 == 1 + 9 gives you 10

1*1 gives you 1



sample output:



7= 49= 16+81= 97= 81+49=130 =1+9=10 =1


"7 is a happy number"





. if the last number is 2 then the number being inputed is
not a happy number.

Answer Posted / swapnil chhajer

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int happyNumber(int n)
{
char temp[10];
itoa(n,temp,10);
int len=strlen(temp);
int ret,sum=0,i;

if(n==1)
return 1;
else if(n==4)
return 0;
else
{
for(i=0;i<len;i++)
sum += (temp[i]-48)*(temp[i]-48);
ret = happyNumber(sum);
}

return ret;
}


int main()
{
int n;
printf("Enter the number : ");
scanf("%d",&n);
if(happyNumber(n) == 1)
printf("\n\n%d is a HAPPY NUMBER",n);
else
printf("\n\n%d is NOT A HAPPY NUMBER",n);

fflush(stdin);
getchar();
return 0;
}

Is This Answer Correct ?    6 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

console I/O functions means a) the I/O operations done on disk b) the I/O operations done in all parts c) the input given through keyboard is displayed VDU screen d) none of the above

645


What is wild pointer in c?

594


What are header files and explain what are its uses in c programming?

599


Describe the order of precedence with regards to operators in C.

627


int i=10; printf("%d %d %d", i, i=20, i);

999






What is calloc() function?

614


How do you list files in a directory?

553


What functions are used in dynamic memory allocation in c?

583


How a string is stored in c?

574


What is the heap in c?

635


How can I dynamically allocate arrays?

581


What are the advantages of c language?

655


What is the data segment that is followed by c?

600


How can I remove the leading spaces from a string?

623


C program to find all possible outcomes of a dice?

1843