This blog is under construction

Sunday, 23 June 2013

C program to find whether the given number is even, prime or odd

Write a C program to check whether the given number is even, prime or odd.


  #include <stdio.h>
  int main () {
        int data, i, flag = 0;
        printf("Enter ur value for data:");
        scanf("%d", &data);

        /* if data divisible by two - even number */
        if (data % 2 == 0) {
                printf("%d is an even number\n", data);
        } else {
                for(i = 2; i <= data - 1; i++) {
                        /* if the data divisible by itself and 1 - prime no */
                        if (data % i == 0) {
                                flag = 1;
                                break;
                        }
                }
                if (flag)
                        printf("%d is an odd number\n", data);
                else
                        printf("%d is a prime number\n", data);
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter ur value for data:115
  115 is an odd number



No comments:

Post a Comment