Solved

Simple program

Posted on 1998-10-31
4
164 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
Comment Utility
------------------------
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
Comment Utility
1 is not prime
0
 
LVL 6

Accepted Solution

by:
thresher_shark earned 100 total points
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now