Solved

Simple program

Posted on 1998-10-31
4
167 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 170
Arduino EDI - Programming language 3 103
What is sub-make ? 2 71
windows 10 pro lost profile. 10 43
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

839 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