Question

Write a program in C that asks for an integer n, if n is greater than...

Write a program in C that asks for an integer n, if n is greater than 100 or if it is less than 1, output an error message. If n is in the range {1, . . . , 100} then if n is either 2,3,5, or 7, output “YES”; or if n is not divisible into 2,3,5 or 7 also output “YES” if n 6= 1; otherwise, output “NO”. Yes, you’re right, a very simple program that checks if a number up to one hundred is prime (implement the program only with if statements)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question,

Here is the complete program in C language using only If condition, not even else of else if

Let me know for any doubts or question.

===========================================================================

#include<stdio.h>

int main(){
  
   int num;
  
   printf("Enter a number between 1 and 100 (both inclusive): ");
   scanf("%d",&num);
  
   if(1<=num && num<=100){
      
       if(num==2 || num==3 || num==5 || num==7 ){
           printf("YES\n");
       }
      
       if(!(num%2==0) && !(num%3==0) && !(num%5==0) && !(num%7==0)) {
           printf("YES\n");
       }
      
       if((num!=2 && num!=3 && num!=5 && num!=7) &&( num) &&(num%2==0 ||
           num%3==0 || num%5==0 || num%7==0
       )){
           printf("NO\n");
       }
      
      
      
   }
  
   if(num<1 || num>100)   {
      
       printf("Error: The number was not between 1 and 100.\n");
   }
}

========================================================================

Add a comment
Answer #2

Code :-


#include <stdio.h>


int main() {

    int n;

    

    // Ask for input

    printf("Enter an integer between 1 and 100: ");

    scanf("%d", &n);

    

    // Check if input is out of range

    if (n < 1 || n > 100) {

        printf("Error: Number must be between 1 and 100.\n");

        return 1;

    }

    

    // Check for prime numbers

    if (n == 2 || n == 3 || n == 5 || n == 7) {

        printf("YES\n");  // Output for primes 2,3,5,7

    } else if (n != 1 && n % 2 != 0 && n % 3 != 0 && n % 5 != 0 && n % 7 != 0) {

        printf("YES\n");  // Output for other primes

    } else {

        printf("NO\n");   // Output for non-primes and 1

    }

    

    return 0;

}




 clude <stdio.h>int main() {
source: Self
answered by: Harshwardhan kunal
Add a comment
Know the answer?
Add Answer to:
Write a program in C that asks for an integer n, if n is greater than...
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 in c++ that prompts for an integer greater than one that represents the...

    Write a program in c++ that prompts for an integer greater than one that represents the low end of a range and a second integer greater than or equal to the first integer that represents the high end of a range. The program will print the factors (starting from 1) of integers in this range. For example, if the user enters 2 for the low end of the range and 6 for the high end of the range, then the...

  • 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...

  • use simple C++ please Exercise 3: Write a program that reads a positive number and checks...

    use simple C++ please Exercise 3: Write a program that reads a positive number and checks if the number can be expressed as a sum of two prime numbers. If yes, write all possible ways of expressing the number as sum of primes. Note: n is a prime if it is divisible only by 1 and itself, hence not divisible by any integer in the range from 2 to sqrt(n), both inclusive. Write a function is Prime that take a...

  • 4. Write a program that asks the user for a positive integer no greater than 15....

    4. Write a program that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character 'X'. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following: XXXXX XXXXX XXXXX XXXXX XXXXX c++

  • Write a program that reads a positive integer N, where N is greater than 1 and...

    Write a program that reads a positive integer N, where N is greater than 1 and prints the value of the floating-point value SUM where SUM is the results of the following series. SUM = 1/1! + 2/2! + 3/3! + 4/4! + ... N/N! Note that your program should print the message “Sorry, bad N”” if N is <=1, and keep reading user input until the user enters a valid value for N. Also, note that all division operations...

  • This is for Program in C Write a program that asks the user for an integer...

    This is for Program in C Write a program that asks the user for an integer with the prompt "Please enter an integer" using the puts function. Write the definition of a function isSenior, which receives an integer parameter and returns true if the parameter's value is greater or equal to 65, and false otherwise. So if the parameter's value is 7 or 64 or 12 the function returns false. But if the parameter's value is 69 or 83 or...

  • Exercise 9.2 Write a Python program that collects from the user a single integer. Have the...

    Exercise 9.2 Write a Python program that collects from the user a single integer. Have the program print “EVEN” if the integer is divisible by 2, and “ODD” if the integer is not divisible by two. [Use the % operator to compute the two’s modulus (remainder of division by two)] Exercise 9.3 Write a Python program that collects from the user two integers. Have the program print “Yes” if the two integers are BOTH greater than 10. Do nothing if...

  • Write a method that determines if a given non-negative integer is a prime number, use the...

    Write a method that determines if a given non-negative integer is a prime number, use the following method header: public static boolean isprime(int n) Prime numbers are integers that are divisible only by 1 and themselves; for example, 2, 5, 7, and 13 are prime numbers, while 15, 16, and 20 are not. By definition, 0 and 1 are not prime numbers. To test your method, write a simple program that asks the user to enter a number and then...

  • Write a program using M.I.P.S that asks user “how many positive number that is devisable by...

    Write a program using M.I.P.S that asks user “how many positive number that is devisable by 6 you want to add?” .Then your loopcounter would be the user input. If the user enters a positive number between 1 and 100 that is devisable by 6, you increment your loop counter and add it to the sum.. You need to decide if the positive number entered by the user is divisible by 6 or not. Your program should print an error...

  • Write a C program that accepts a single integer N. Your code must then calculate the...

    Write a C program that accepts a single integer N. Your code must then calculate the average of all positive integers less than N that are divisible by either 5 or 7, but not both. When you print your average, truncate the result to three decimal places. Do not print anything else to the screen. Example input: 19 Example output: 10.200

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