Solved

Simple program

Posted on 1998-10-31
4
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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

Suggested Solutions

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Suggested Courses

752 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