1. Engineering
  2. Computer Science
  3. can someone help me with this c program im trying...

Question: can someone help me with this c program im trying...

Question details

Can someone help me with this C program? I'm trying to confirm if an positive integer is even, prime, a perfect square, find the total bits it has, find the number of decimal digits it has, and the number of one bits it has. So far this is what I have, when I enter 21, everything runs except I get the number of decimal digits instead of the number of total bits also the program doesn't loop again to ask for another integer. When I enter 4, it tells me if it's prime, even, and a perfect square but that's it, and it wont loop back to ask for another integer. If you can, please explain where I am going wrong? (also,I cant use global variables, sqrt, or goto)

-------------------------------------------------------------------

#include <stdio.h>

int main() {
    int number = 1;
    int count = 0;
    int Count = 0;
    int Count2 = 0;

    //This loop will continue unless the entered number is 0
    while (0 < number) {
        printf("Enter an integer: ");
        scanf("%d", &number);

// EVEN: This statement is true if the number is divisible by 2
        if (number % 2 == 0)
            printf("Is the number even?  yes\n");
        else
            printf("Is the number even?  no\n");

//PRIME: This section checks to see if a number is divisible by 1 and itself
        int i, flag = 0;

        for (
                i = 2;
                i <= number / 2; ++i) {
// condition for nonprime number
            if (number % i == 0) {
                flag = 1;
                break;
            }
        }
//1 is not prime or composite
        if (number == 1) {
            printf("Is the number prime?  no\n");
        } else {
            if (flag == 0)
                printf("Is the number even?  yes\n");
            else
                printf("Is the number prime?  no\n");
        }
//PERFECT SQUARE: This section checks for perfect squares
        for (i = 0; i <= number; i++) {
            if (number == i * i) {
                printf("Is the number a perfect square?  yes\n");
                return 0;
            }
        }
        printf("Is the number a perfect square?  no\n");
//ALL BITS: This section counts the total number of bits
        while (number != 0) {
            if ((number & 0) == 0)
                Count++;
            number = number >> 1;
        }
        printf("The total number of bits is %d\n", Count);

//1 BITS: This section counts the number of one bits
        while (number != 0) {
            if ((number & 1) == 1)
                count++;
            number = number >> 1;
        }
        printf("The number one's are %d\n", count);
    //DECIMAL DIGITS: This section counts the number of digits entered
        while (number != 1) {
            if ((number & 0) == 0)
                Count2++;
            number = number >> 1;
        }
        printf("The total number of digits is %d\n", Count2);

    }
    return 0;
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution