Solved

Simple program

Posted on 1998-10-31
4
168 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:heyyou
  • 2
4 Comments
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1253954
------------------------
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!
0
 
LVL 84

Expert Comment

by:ozo
ID: 1253955
1 is not prime
0
 
LVL 6

Accepted Solution

by:
thresher_shark earned 100 total points
ID: 1253956
LOL.

Hehee, yes, you are right, I really have to change that.  Neither is 0 :-).

heyyou - Please take my above comment as the answer.  If you want me to adjust the program so that 1 is not considered prime, I will. :-)
0
 

Author Comment

by:heyyou
ID: 1253957
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question