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
In the DOS enveronment, normal RAM that resides beyond the 1mb mark. a) expanded memory b) swapped memory c) Extended memory d) none
what does static variable mean?
Write a program to print fibonacci series without using recursion?
Is there any data type in c with variable size?
1.int a=10; 2.int b=20; 3. //write here 4.b=30; Write code at line 3 so that when the value of b is changed variable a should automatically change with same value as b. 5.
What does s c mean in text?
What is restrict keyword in c?
What is the correct declaration of main?
#include show(int t,va_list ptr1) { int a,x,i; a=va_arg(ptr1,int) printf(" %d",a) } display(char) { int x; listptr; va_star(otr,s); n=va_arg(ptr,int); show(x,ptr); } main() { display("hello",4,12,13,14,44); }
What is the use of bitwise operator?
What is chain pointer in c?
Given a valid 24 hour format time find the combination of the value and write a program ,do not hard the value and if any other inputs provided should work with the logic implemented Input: 11:30 Output: 13:10 Input: 18:25 Output: 21:58
How can I read a binary data file properly?
Why do we write return 0 in c?
What are register variables in c?