Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<iostream.h>
#include<conio.h>
void happy_no()
{
	int sum=0,num,i,d,c=0;
	cout<<"enter a number: ";	cin>>num;
	for(i=1;i<=10;i++)
	{	
		d=num%10;
		sum+=(d*d);
		num/=10;
		if(sum==1)
		{
			c++;
			break;
		}
		num=sum;
		sum=0;
	}
	if(c==1)
		cout<<"HAPPY NUMBER";
	else
		cout<<"NOT A HAPPY NUMBER";
}
void main()
{clrscr();
happy_no();
getch();
}


What I have tried:

A number is called a happy number, if you repeat the process, of squaring the sum of the digits, till the value 1 is obtained. But, this continues summing and checking for sum = 1 should happen before the 10th iteration.

Further more, when I type this code on to visual studio, this code works, and when I do the same in visual studio, it doesn't. IT will really mean alot if you could tell me the correction and the reason.
Posted
Updated 30-Jul-17 7:04am
v2

Your code is not doing what your question describes. The correct sequence is:
Input a number
DO
    Split the number into its constituent digits
    Add those digits together to get the sum
    Calculate the square of the sum
    If the answer is equal to 1 break from the loop
    If completed 10 iterations break from the loop
    set number equal to sum
    repeat the process
END
 
Share this answer
 
Comments
Deepak Kumar Choudhary 30-Jul-17 11:01am    
@Richard MacCutchan, Thank you very much for your response. Just to clarify, sir, my question was "Why I am unable to execute this code in Visual Basic while I am able to on Turbo C.
Looking forward to your reply.
Thank You.
Richard MacCutchan 31-Jul-17 2:28am    
Are you serious?
There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
-----
Advice:
- Learn one or more analyze methods, E.W. Djikstra top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900