Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Fill in the function to test if a number is prime. You can assume the input is a

ID: 3725900 • Letter: F

Question

Fill in the function to test if a number is prime.

You can assume the input is a non-negative number.

Reminder: 0 is not a prime, and 1 is not a prime.

Fill in the leap year test.

You can assume the year value is non-negative.

A year is usually a leap year if it is a multiple of 4.

However, a year is NOT a leap year if it is divisible by 100 UNLESS it is also divisible by 400.

Therefore, 2000 is leap year, 2100 is not leap year.

Complete both functions (with correct logic) to receive partial credits.

Full Credits: (1.5)

Fill in the main function to take inputs and decide which function to call.

The function decider will simply take in one upper case letter. P for testing prime, Y for testing year.

For example:

input:

P 13

should give you the output:

1

The input simply means test if 13 is a prime, and the output says true/yes.

input:

Y 2100

should give you the output

0

This input means test if year 2000 is a leap year, and the output says false/no.

Do not waste your time and try to "fancy up" the prompt such as asking "which function would you like to call" and/or changing the output to "The year 2000 is a leap year". You will not get extra credit for doing so, and actually fail the test cases.

Extra Credits: (2.0)

Optimize your prime testing function to pass the last test case.

#include <iostream>
using namespace std;

bool isPrime(int p) {
//fill this in
}

bool isLeapYear(int y) {
//fill this in
}


int main() {
char cmd;
cin >> cmd;
int i;
cin >> i;
//For full credits, fill this in...
}

Explanation / Answer

#include <iostream>

using namespace std;
bool isPrime(int p) {
  
    int m=p/2;
    bool prime= true;
for(i = 2; i <= m; i++)
{
      if(n % i == 0)
      {
           prime = false;
          break;
      }
}
return prime;
}

bool isLeapYear(int y) {
    bool leapYear=false;
if((yr%4==0) && (yr%100!=0))
   {
       leapYear=true;
   }
   else if(yr%100==0)
   {
       leapYear=false;
   }
   else if(yr%400==0)
   {
       leapYear=true;
   }
   else
   {
       leapYear=false;
   }
   return leapYear;
}

int main()
{
char cmd;
cin >> cmd;
int i;
   cin >> i;

   bool prime=isPrime(i);
   if(prime)
   {
       cout<<"Number is prime";
   }
   else
   {
       cout<<"Number is not prime";
   }

   int year;
   cin >> year;
   bool leapYear=isLeapYear(year);
   if(leapYear)
   {
     cout<<"Leap Year";
   }
   else
   {
       cout<<"Not a Leap year";
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote