hmwk1_sol_ch - ); scanf ( "%d"...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/* ============================================================================ Problem Statement: Find the number of twin primes specified by the user. ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <math.h> int main ( void ) { int userinput; //declared and assigned in the same sentence. int twinprime = 0; //This keeps track of how many primes you have discovered. double testnumbr = 2; // This will be the number in question if it is a prime number. double startdivdenom = 2; // This is the first number that will be divided through to test for prime. double enddivdenom; // This is the last number that will be divided through to test for prime. double result; // result of testnumbr/i double leftofdec, rightofdec; int i; if ( setvbuf (stdout, NULL, _IONBF, 0)) { perror ( "failed to change the buffer of stdout" ); return EXIT_FAILURE; } printf ( "\n How many twin primes do you want? "
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ); scanf ( &quot;%d&quot; ,&amp;userinput); double previous = 3; while ( twinprime &lt;= userinput - 1) //loop until desired prime numbers have been achieved in this case 5 { enddivdenom = sqrt (testnumbr); enddivdenom = round (enddivdenom); if (enddivdenom &lt; 2) // This is for cases were sqrt result is less than starting number. { enddivdenom = 2; } for ( i = startdivdenom ; i &lt;= enddivdenom ; i++) { result = testnumbr/i; rightofdec = modf (result , &amp;leftofdec); if (rightofdec ==0) // if it equal zero this is not a prime number { testnumbr++; startdivdenom = 2; break ; } else if (i == enddivdenom) // we found a prime number! { if (testnumbr - previous == 2) { printf ( &quot;%d\n&quot; , ( int )testnumbr); twinprime++; } startdivdenom = 2; // now we are going on to test the next number in the chain. previous = testnumbr; testnumbr++; } } } return 0; }...
View Full Document

This note was uploaded on 06/07/2011 for the course COMPE 271 taught by Professor Staff during the Summer '08 term at San Diego State.

Page1 / 2

hmwk1_sol_ch - ); scanf ( &amp;amp;quot;%d&amp;amp;quot;...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online