View Single Post
Old Jan 19th, 2006, 8:15 PM   #1
sixstringartist
Programmer
 
Join Date: Jun 2005
Posts: 68
Rep Power: 4 sixstringartist is on a distinguished road
Creating a program to test a program

After a long absence from these boards and some more programming under my belt, I have finnally dived into C because I have my first college C class this semester. I just finished writing my first C program other than the classic "hello world". It is a program that asks for user input of an integer and decided whether or not the integer is a prime number. This program is no where near efficient and I plan on narrowing down its testing phase to only include numbers that could possibly be a prime (i.e. nothing higer than the root of the input) I would like to write a program that will test this program for each interger up to a stopping point. Also any other efficiency suggestions would be apprecieated.


/****************************************************
*

*  Programmer and Purdue Email Address:

*       1.Chris Cadwallader

*

*  Program Description: This program asks for user

*      input of an integer an outputs whether the

*      integer is a prime number or not.

*

*
***************************************************/


#include <stdio.h>


int main()

{

  /* Local Definitions */

  
  int testval;    // Integer to be tested

  int denom;      // Denominator used in calculations

  int testresult; // Calculation result

  int counter;    // Loop iteration counter


  
  /* Statements */



  /* Requesting an integer */


  printf("\n\nWelcome to the Prime number testing program.");

  printf("\n\nPlease Enter an integer to be tested:  ");

  scanf("%d", &testval);



  /* Verifying that the input is an integer greater than 1 */


  while (testval < 1)

   {

     printf("\nThe test value cannot be smaller than 1.\n\n");

     printf("\n\nPlease enter an integer to be tested: ");

     scanf("%d", &testval);

   } /* while */



  if (testval == 2)

     printf("The value entered is a prime number.\n\n");


  
else
 
   {

      denom = 2;

      testresult = testval % denom;

      counter = 1;


  
    /* Testing integer for prime status */


      while (denom < testval)

       {

          testresult = testval % denom;

          if (testresult == 0)

     
          denom = testval;

           else

              counter++;

               denom++;

       } /* while */


  if (testresult != 0)

        printf("The value entered is a prime number.\n\n");


  else

        printf("The value entered is not a prime number.\n\n");

    } /* else */


  printf("This program looped %d times to complete the problem.\n\n",counter);


  return 0;


} /* main */

thanks


ps..... my posts seem to have dissappeared.... now I only have 2..... thats strange.
sixstringartist is offline   Reply With Quote