Link to home
Start Free TrialLog in
Avatar of heyyou
heyyou

asked on

Simple program

Hello,

I am new to C and was wondering how to make a simple program that for example counts to 100?  I am using Turbo C++ for DOS (3.0).

Thanks for any assistance.
Avatar of thresher_shark
thresher_shark

------------------------
Counts from 1 to 100:
------------------------
#include <stdio.h>
#include <conio.h>

void main (void)
{ int i;
  for (i = 1; i < 100; i++)
  { printf ("%d\r\n", i); }

  getch ();
  return;
}

-------------------------
Counts from 1 to 100000
-------------------------
#include <stdio.h>
#include <conio.h>

void main (void)
{ unsigned long i;
  for (i = 1; i < 100000; i++)
  { printf ("%ld\r\n", i); }

  getch ();
  return;
}

---------------------------
Shows some prime numbers
---------------------------
#include <stdio.h>
#include <conio.h>

int is_prime (unsigned long);

void main (void)
{ unsigned long i;
  for (i = 0; i < 10000; i++)
  { if (is_prime (i) == 1)
    { printf ("%ld", i); }
  }

  getch ();
  return;
}

int is_prime (unsigned long orig)
{ unsigned long num, den, quo;

  num = orig;
  quo = orig;

  for (den = 2; den < quo;)
  { quo = num / den;        // Lose some precision if it's even.
    if (quo * den == num)
    { return 0; }
    else
    { if (den & 0x1)
      { den += 2; }
      else
      { den++; }
    }
  }

  return 1;
}

----------------
If you have any questions about the above programs, please don't hesitate to ask.  Thanks!
Avatar of ozo
1 is not prime
ASKER CERTIFIED SOLUTION
Avatar of thresher_shark
thresher_shark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of heyyou

ASKER

Thanks a lot!!  These have really helped me.  Now I can see how the if statement works and how to make a variable.  I really need to get a book though...

Thanks also ozo for pointing out that it shows that '1' is a prime number.  Hehe, oh well.  It doesn't matter.