Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Major help on C Programming !

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I need to make the following program.
1. I'll show example : If the user enter 6, the program have to work like this, 1x2x3x4x5x6= 720 /or, if he enter 3 : 1x2x3 = 6.
I have to use do/while, for and while loop. If the user enter 0 the program is done. The program should show the final result. Also, the user can't enter a number higher than 16.
 
That sounds like an assignment to me. That is a simple factorial program, and you don't even have to use recursion! I could easily give you the code for this, but what would you learn? Well, not much. I suggest you try it on your own. Reading chapter 2 in your C book and coding for a few minutes should solve this problem.

-bitwise
 
Well this is what i've done... but i'm stuck

#include <stdio.h>
main()
{
int number;
int countor;
int factor;

do
{
printf(&quot;Enter a number : &quot;);
scanf(&quot;%d&quot;, &number);
}
while (number<0);
countor=1;
factor=1;
while (countor<=number)
{
factor*=countor;
countor++;
}
printf (&quot;%d! = %.0d\n&quot;, number, factor);
return 0;
}
 
Whats the problem with the above code?
I dont see anything that is blaringly
obvious. Run it and see what happens.

Try it with 1, then 2, then 3, then 6
See what answers it gives you. That is
if it is compiling.



Shuten
 
#include <stdio.h>
#include <math.h>
#define N 20

int main(void) {
int i,j,intsum; float sum;
for (i=1; i <=N; ++i) {
for (j=1; j <= N; ++j) {
sum = (float)sqrt(i*i + j*j);
intsum = (int)sum; /* integer part of sum only */
/* if no fractional part, then perfect square*/

if ( (sum - intsum) == 0)
if (i<j)
if (intsum % 2 !=0)
printf(&quot;%d %d %d\n&quot;, i, j, intsum);

}
}
return 0;
}

Now it will compile without any warnings.
 
Here's how to do it...

Code:
void main(void)
{
int Number;
int Total;

do
   {
   printf(&quot;Enter a number between 1 and 16 (0 to quit) : &quot;);
   fflush(stdin); scanf(&quot;%d&quot;, &Number);

   Total = 1;

   if (Number <= 16)
      for (Number; Number > 1; Number--)
         Total *= Number;

   if (Number > 0)
      printf(&quot;Total : %d&quot;, Total);
   }
while (Number != 0);
}
 
Thank you very much lavomar1.
If i ever need more help i'll come and see you. Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top