I have been constantly trying to figure out how to make this code work. Well here is what I'm supposed to do.
Write a function named isPrime which accepts two integers say x and y.
As long as y > x, the program will print all primes between x and y inclusive.
Write a function named findPrime which accepts two integer parameters say a
and b. This function will print all prime numbers from a to b by calling the
function isPrime. Have your program cal this function using the user input
x and y.
Enhance the above step, so that your program will also print all perfect
square numbers between x and y inclusive. You may use the sqrt() function included in
<math.h> header file.
Example: If 3 and 30 are entered for x and y, your program should produce the
following output.
Prime: 3 5 7 11 13 17 19 29
(Prime count =9)
Square: 4 9 16 25
(Square count=4)
(Sum of all primes and squares: 181)
Okay, so I have been trying to figure out only how to do the Prime part on my own for so long, and I can't figure it out. I have to write functions, and I cannot do it any other way. I was never taught how to write a function, my instructor just threw this assignement at me.
Here is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
// Project 4 IsPrime.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <math.h>
#define TRUE 1
#define FALSE 0
void main(void)
{
int x;
int y;
printf ( "Please enter two positive integers-->");
scanf ( "%d %d" , &x, &y );
printf ( "\nPRIME: ");
while ( scanf( "%d %d" , &x,&y)==2 )
{
findprime(x,y);
printf ( "Enter two positive integers-->" );
scanf ( "%d %d", &x, &y );
}
}
void findprime( int a, int b) /* finds the prime number*/
{ /* checks to see if the numbers between x and y are prime */
int p; //number of primes
int i; //divisor
{
for (i=2; a<b; i++)
{ if (isprime(i))
printf ( "\t%d" , i)
}
}
}
int isprime(int k)
{
if ((a%i!==0)) // makes it prime?
p++;
return TRUE;
else
return FALSE;
}
|
If someone could help me out on this, it would be great! If someone could figure out a code for this so I can understand how each function is called, that would be beter! But anything is greatly appreciated!
Thank you,
-Adot2the