Coding Practice

I like odd numbers a lot! I know it's your exam but you have to do me a little favor! I will give...

Write necessary C code for doing the job

I like odd numbers a lot! I know it's your exam but you have to do me a little favor! I will give you a random integer number and you have to make it a Strongly Odd Number and check if it is a prime number or not. A strongly Odd Number is a number where every digit is an odd number. And Prime Number is a number that is divisible only by itself and 1. Write necessary C code for doing the job.

Sample Input:    Sample Output:
6594                    59. Prime
9805789              9579. Not Prime
Sample Output
6594
59. Prime
Process returned 0 (0x0)   execution time : 3.859 s
Press any key to continue.
                    
Source Code
#include<stdio.h>

int main()
{
    int i, k = 0, arr[20], num, new_num = 0, digit, isPrime = 1;

    scanf("%d", &num);

    while(num > 0)
    {
        digit = num % 10;
        num = num / 10;

        if(digit % 2 != 0)
        {
            arr[k] = digit;
            k++;
        }
    }

    for(i = k - 1; i >= 0; i--)
    {
        new_num = new_num*10 + arr[i];
    }
    for(i = 2; i < new_num; i++)
    {
        if(new_num % i == 0)
        {
            isPrime = 0;
        }
    }
    if(isPrime == 1)
    {
        printf("%d. Prime", new_num);
    }
    else
    {
        printf("%d. Not Prime", new_num);
    }
    return 0;
}
Sample Output
9805789
9579. Not Prime
Process returned 0 (0x0)   execution time : 2.070 s
Press any key to continue.
                

No comments:

Post a Comment

Change Theme
X