Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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  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 "); else printf("Is the number even? no "); //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 "); } else { if (flag == 0) printf("Is the number even? yes "); else printf("Is the number prime? no "); } //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 "); return 0; } } printf("Is the number a perfect square? no "); //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 ", 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 ", 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 ", Count2); } return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Visual Basic Net Database Programming

Authors: Rod Stephens

1st Edition

0789726815, 978-0789726810

More Books

Students also viewed these Databases questions

Question

How much of this is in your direct control?

Answered: 1 week ago

Question

Working with other project stakeholders for support.

Answered: 1 week ago

Question

Knowledge of project management (PMI) teachings

Answered: 1 week ago