Ah...thank you . got it. :)
Printable View
babyboy: My feedback for you so far, remove conio.h from your includes, it is non standard which means if you move to a different compiler (which is possible) your code wont compile. When you remove it you will notice getch() doesn't work anymore, the standard version of that is getchar() (if I remember my C correctly, never actually learned it heh)
Other than that, good work thus far.
On your current problem: You should be inserting a newline character at the end of every set of 12. Also in printf, if I recall right (which also could be wrong) when you say %#d where # is a number, that is how many places that number will be displayed to, so if you use %2d and the number will take up 2 spaces, try it out.
Thank You Wraithan for your input. Will keep in mind the points you mentioned.
Also I did try the %2d . :)
All the C I know I learned on these boards, I have to admit I have never written a line of C in my life (aside from any C++ code that is valid C code), so if someone corrects my advice, take theirs over mine :D
Question :- Ask the user to enter a number, and find the multiplier(s) that make that number up. The number should be in the range 0..100.
E.g. 36 -> 2 * 18 -> 2 * 2 * 9 -> 2 * 2 * 3 * 3.
Can you please help me with this?
Here is what I think is the logic but I am not able to put it in codes.
n,i,j are integers
1. Enter number n.
2. check for n%i==0 where i starts from 2 but is less than number.
3. if n%i==0 then do j=n/2
4. Now j becomes the new n.
Am i right on the logic part?
Yes, but you need to formulate it with loops. Note that in the actual implementation you can write n /= 2 instead of introducing j.Quote:
Am i right on the logic part?
You could use an if to treat integers less than 2 as a special case. The main logic would be with nested loops.Quote:
Now here is my other question. As per you do you think an "if condition statement" will come inside this loop or is it just "nested for loops".
I think I have got something here.
But I am not able to present it properly.
Can you please take a look and tell me what I am doing wrong.
Code:#include <stdio.h>
int main()
{
int no,i,j;
printf ("Enter a Number between 1 to 100: ");
scanf ("%d",&no);
for (i=2; i<no; i++)
{
for (j=i; no%i==0; j++)
{
no/=i;
printf("%d\n",no);
}
}
getchar();
return 0;
}
Firstly, indent your code properly, e.g.,
Next, one problem is that instead of printing the factor found (i.e., i), you print the number after it has been divided by the factor.Code:#include <stdio.h>
int main()
{
int no, i, j;
printf("Enter a Number between 1 to 100: ");
scanf("%d", &no);
for (i = 2; i < no; i++)
{
for (j = i; no % i == 0; j++)
{
no /= i;
printf("%d\n", no);
}
}
getchar();
return 0;
}
Other problems include the outer loop condition and that you have j but do not use it. Remove j.
Main problem as I see it - missing comments - this code is written in a way it is hard to understand without them
I have not been to solve this problem. Can somebody please suggest how i go about it.
I think you are close to a solution. Have you tried my suggestions, and incorporated useful comments as vart suggested?
yes i have been trying so many things... but nothing seems to be happening. I could get the multipliers when I printed "i" instead of "no".
But then when i removed the "j" I got all confused and stuck.
And the outer loop, now I am beginning to wonder, if even its required or not.
What is your code now?