Question
Use C programming
Make sure everything works well only upload
Q1) write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing Input a number greater than 2: n. If the users input is less than or equal to 2, then print No prime number is smaller than kusers input .In, where users input is replaced by the number input by the user. If the user inputs a number greater than 2, then print Prime numbers smalrer than <users input Then, on the same line, print out a list of all prime numbers that are smaller than the input. In your list, each prime number must be separated by one space and a newline must be printed immediately after the last number in the list. As part of your program, you must make a function that takes an integer as an argument, returns 1 if the integer is prime, and returns 0 otherwise. You can use the following function prototype: int is Prime (int number); Example input/output pairs (excluding the prompt) are provided below: (a) Input: 6; Output: Prime numbers smaller than 6: 2 3 5 (b) Input: 23; output: Prime numbers smaller than 23: 2 3 5 7 11 13 17 19 (c) Input: 1; Output: No prime number is smaller than 1. Note: Include all of your code in one file named prime.c
1 0
Add a comment Improve this question Transcribed image text
Answer #1

program:

#include <stdio.h> #include <stdlib.h> int isPrime(int num); //function to check if given number is prime or not // Begining

printf(prime numbers smaller than %d is : , num ); // Use for loop to check the prime numbers which are smaller //than give

return 0; //funtion to check if given number is prime or not int isPrime(int num) int i, flag-e; for(i-2; inum; i++) if(num %

else return 0;

Sample Output:

Enter a number greater than 2:23 prime numbers smaller than 23 is2 3 57 11 13 17 19:

Enter a number greater than 2:10 rime numbers smaller than 10 is 2357

Enter a number greater than 2:1 No prime number is smaller than 1:

Code to copy:

#include <stdio.h>
#include <stdlib.h>
int isPrime(int num);
//function to check if given number is prime or not

// Begining of C program
int main()
{
// Declare and intialize the variables
int i,n=0, num;
  
// Prompt the user to input a number
printf("Enter a number greater than 2:");
  
// Read the number from the user
scanf("%d", &num);
  
// Use if statement to chech the given number is greatherthan or not
if(num<=2)
{
printf("No prime number is smaller than %d",num);
return 0;
}
  
printf("prime numbers smaller than %d is :",num );
  
// Use for loop to check the prime numbers which are smaller
//than given number
for(i=2; i< num; i++)
{
n= isPrime(i);
// if isPrime returns 1 then i is a prime number which is
//less than given number
  
// If statement used to print the prime numbers
if(n==1)
{
printf(" %d",i);
}
}

return 0;
}

//funtion to check if given number is prime or not.

int isPrime(int num)
{
int i, flag=0;
for(i=2; i<num; i++)
{
if(num % i == 0)
{
flag =1;
break;
}
}
  
if(flag==0)
{
return 1;
}
else
{
return 0;
}
}

Add a comment
Know the answer?
Add Answer to:
Use C programming Make sure everything works well only upload Write a program that takes an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program “hw4.c” that reads integer (less than or equal 100) from the keyboard and,...

    Write a program “hw4.c” that reads integer (less than or equal 100) from the keyboard and, on the output, writes the sum of the divisors of n (other than itself). For integers less than or equal to 1 it should print 0. For example, the input -3 0 1 4 5 6 12 should generate the output 0 0 0 3 1 6 16 Explanation of output: The input -3 is less than 1, output is 0. The input 0...

  • A positive integer is a prime number if its only positive integer divisors are itself and...

    A positive integer is a prime number if its only positive integer divisors are itself and 1. Write a program to determine whether or not a given integer is prime. The program should contain two functions: main: to ask the user for a positive integer and to print the result isPrime: to determine whether the user's input is prime by testing all possible divisors. This function should return two values: variable_1: a Boolean value indicating whether the number is prime...

  • *Please write in code in C* First, write a function called prime_check(int x) that takes an...

    *Please write in code in C* First, write a function called prime_check(int x) that takes an integer number as an input then return 1 if it is a prime number nd returns 0 if it is a composite number Then, Write a program that prompt the user to input two numbers: Print the multiplication of these two numbers if both of them are prime. Or Print " Sorry at least one of your number is composite" if otherwise

  • Write a java program to print all the prime numbers below a certain given number. A...

    Write a java program to print all the prime numbers below a certain given number. A prime number is defined as a number that can only be divided by 1 and itself. Requirements: 1. Accept the upper limit from the user as an integer. 2. You can assume the user input will be positive and smaller than INT MAX 3. Go from 1 to the number. If you happen to find a number that is prime, print it. The input...

  • Write a C program that takes a positive integer n as input and prints all the...

    Write a C program that takes a positive integer n as input and prints all the prime numbers from 1 to n. Sample Input/Output 1: Enter your n: 20 Prime number(s) from 1 to 20 : 2 3 5 7 11 13 17 19 Sample Input/Output 2: Enter your n:2Prime number(s) from 1 to 2 : 2

  • In C programming Write a program that displays the appropriate prompt to input 5 scores. Once...

    In C programming Write a program that displays the appropriate prompt to input 5 scores. Once the user inputs the five integer values, your program calculates and prints the average. You do not need to declare five different variables, think of how you can get away with a single variable. The second part of this program will print A if the average of these 5 numbers is greater than or equal to 90 but less than 100, B if greater...

  • Is Prime Number In this program, you will be using C++ programming constructs, such as functions....

    Is Prime Number In this program, you will be using C++ programming constructs, such as functions. main.cpp Write a program that asks the user to enter a positive integer, and outputs a message indicating whether the integer is a prime number. If the user enters a negative integer, output an error message. isPrime Create a function called isPrime that contains one integer parameter, and returns a boolean result. If the integer input is a prime number, then this function returns...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • USING PYTHON 1. Write a small program that asks for an integer number from the user...

    USING PYTHON 1. Write a small program that asks for an integer number from the user and print all the prime numbers (2,3,5,7,etc) less than the input number. 2. How long it takes for your program to print the prime numbers less than 100. (Use magic functions)

  • DATA PROGRAMMING: PYTHON Be sure to use sys.argv NOT input. Write a program and create a...

    DATA PROGRAMMING: PYTHON Be sure to use sys.argv NOT input. Write a program and create a function called inch2cm that takes one number in inches as a parameter, converts it to the centimeters and prints the result. The program output is shown below. Input: 14 Output: 14 inches = 35.56 centimeter Write a program and create the following functions: shapes(): takes the shape name and a number as parameters, and calls the proper function to calculate the area. areaCircle(): take...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT