Question

In: Computer Science

Write a C function boolean isPrime (int n), that would take a positive integer n as...

Write a C function boolean isPrime (int n), that would take a positive integer n as a parameter and return true or false whether the number is a prime number. You should check the range of n and print proper message (For example, if n is a genitive number, you should print out an error message).

Solutions

Expert Solution

#include <stdio.h>
#define bool int
#define true 1
#define false 0

bool isPrime(int number)
{
int flag = true;
for(int i=2;i<=number/2;i++)
{
if(number%i == 0) // divisible by any number between 2 to number -1
flag = false;
  
}
return flag;
}
int main(void) {
  
   int num;
  
   printf("Enter the number to be checked if its prime : ");
   scanf("%d",&num);
   if(num < 0)

   printf("\nError : The entered number is negative ");
   if(num == 1 || num ==2 || num ==3)
   printf("Error : ");
  
   else if(isPrime(num) == true)
   printf("\nNumber %d is prime",num);
   else
   printf("\nNumber %d is not prime",num);
  
   return 0;
}

Output:

Enter the number to be checked if its prime : 
Number 13 is prime

Do ask if any doubt. Please upvote.


Related Solutions

In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted...
Write function boolean isSorted(int a[], int size). The function returns true if array a is sorted in either ascend order or descend order; false otherwise. c++
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the...
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the corresponding binary number. Do the conversion by repeatedly dividing the number n by 2 using integer division, keepting track of the remainders, until the number is reduced to 0. The remainders written in reverse order form the binary number string. Do integer division of 5 by 2, so that N//2 is 2 with remainder 1. Now divide 2 by 2 to get 1 with...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie Germain prime that is greater or equal to n.
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Write code in C please. #1 Write a function multiples() which will take an integer input...
Write code in C please. #1 Write a function multiples() which will take an integer input and it will print out all the multiples of this number starting from 2 but not including itself. For example, multiples(10) will print 2, 5 and multiples(100) will print 2, 4, 5, 10, 20, 25, 50 #2 Write and test a Fibonacci() function that uses a loop instead of recursion to calculate Fibonacci numbers.
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT