Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the problem is ,I used the to write it with while but i couldn't

What I have tried:

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

int main()
{
	int a,b,c;
	printf("enter the first number\n");
	scanf("%d",&a);
	printf("enter the second number\n");
	scanf("%d",&b);
	//-------------------------------------------------------------------------
	for(a=0;a<=b;a*a){
		printf("%d\n",a);
		a*a;
		
	}
Posted
Updated 23-Dec-17 11:25am

See what you do with a after reading from user.
Your code is non sense, you should learn to use debugger, as soon as possible, to see what it does.

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.
 
Share this answer
 
C
for(a=0;a<=b;a*a){
   printf("%d\n",a);
   a*a;	
}

The first issue is in the for loop declaration, you just throw away the a variable freshly obtained from user, and set it to zero. Maybe you intended to use the c variable instead?

The second issue is that, when you write the expression a * a, you actually do not do anything to the variable itself. You just put the value of a onto the stack, then do it a second time, then call the multiplication which pops the values from the stack and returns the result to the stack. At this point, nothing has changed for a itself, which still holds its initial value. If you want it to remember the new value, you have to assign the value which is on the stack to the variable, by using a = a * a; for example, or a *= a; in its shorter version.

The third issue is with the number of times your loop will be executed. With your implementation, if specified power is 6, you would execute the loop 7 times, for example (provided you fix the first issue).
- Either you start the counter at 0, and then you execute the loop until the counter is (power - 1),
- or you start at 1 and then you execute the loop until the counter is power.
Common standard in C language families is to use zero-based indices, so it's probably best to stick with it.
In clear:
C
for (counter = 0; counter < total; counter++) {
   /* Zero-based iteration */
}
for (counter = 1; counter <= total; counter++) {
   /* One-based iteration */
}

will both execute the loop the desired number of times (note all differences between both, especially the comparator in the test part: the first is a less-than operator, while the second is a less-than-or-equal one).

This smells like homework, so I will not write the code for you. I just hope these will guide you through your issues.
 
Share this answer
 
v2

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