Computer Science, asked by magesWayunityaK, 1 year ago

Program that Prompts the User to Enter a Number
Write a program that prompts the user to enter a number. It then tells the user whether his/her number is a. Positive or negative b. Odd or even c. Prime or not (remember that a prime number is a number that is not divisible by any other number except 1 and itself, i.e 1,3,5,7,11,13,17, and you can use the % operator to check if one number is divisible by another.)

Answers

Answered by kvnmurty
1
#include <stdio.h>
#include <math.h>   // for square root...
#include <stdlib.h>

void main (int argc , char * argv[])  {

    int k=1, n , m , bool = TRUE; // TRUE=1
 
       printf("Enter a number: ") ;
       scanf("%d", &n);

       if (n > 0)  printf(" Positive  ");
       else if (n < 0) printf("Negative  ");
       else printf(" zero  ");
 
       if (n % 2 == 0)  printf(" even");
        else printf("  odd ");

       n < 2  &&  bool = FALSE ;   // 0
       for (k = 2, m = sqrt(n) ;  k <= m && bool ; k ++)
             if ( n % k == 0)   bool = FALSE ;
            
       bool ? printf("  prime \n")  : printf(" not prime \n")  ;
}

Similar questions