Odd/Even and Prime: Check 819877966 Numbers

  • MHB
  • Thread starter jasonsmith206
  • Start date
  • Tags
    Prime
In summary, the code you have for part 1 works, but you need to figure out how to read odd/even. The code for part 2 doesn't make much sense.
  • #1
jasonsmith206
10
0
Hi everyone, been a busy week and I've got midterm cal 2 on monday so i need to get this done as fast as possible so i can focus on my math.

I've got two assignments each requiring me to input 819877966 as a user number and its two parts

1.) is to break the usernumber into individual numbers and tell if they are odd or even
example:
n odd/even
-- ---------
8 even
1 odd
9 odd
. ...
. ...

the code i have for that so far is this:

Code:
#include<stdio.h>

int main()
{

   int num,temp,factor=1;

  printf("Enter a number: ");
  scanf("%d",&num);

  temp=num;

    while(temp){
      temp=temp/10;
      factor = factor*10;
  }
  printf("Each digits of given number are:\n");
  while(factor>1){
      factor = factor/10;
      printf("%d\n",num/factor);
      num = num % factor;
  }
  return 0;
}

I can get it to break up but i can't figure out how or where for my program to read odd/even.

2.)
the same usernumber will be used to read prime numbers so

if it was to read

819877966

it would out put certain numbers saying what its divisable
example:
81 wouldn't be a prime number and would be broke up to its facortrization until it reaches a prime number.

I hate these exerecises because we are only a month into our programming class and our teacher gives us problems like these. Idk what he's thinking but these kinds of questiosn are crazy for first time programmers so any help given will be greatly appreciated!

Thank you
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
This is probably late, but I'll respond anyway. The code you have for part 1 makes sense and it works.

jasonsmith206 said:
I can get it to break up but i can't figure out how or where for my program to read odd/even.
I am not sure what you mean by "read odd/even". Your program is supposed to print whether each digit is even or odd. You computed the digits; surely finding if they are even is easy.

Here is my program.
Code:
int main()
{
  unsigned int num;
  /* unsigned int takes <= 32 bits, which amounts to 10 decimal digits */
  char digits[10]; /* One byte is enough to store a decimal digit */
  int i, j;

  printf("Enter a number: ");
  scanf("%d", &num);
  /* Writing digits of num into the array digits in reverse order.
     Rightmost digit goes to the beginning of the array. */
  for (i = 0; num > 0; i++, num /= 10)
    digits[i] = num % 10;
  printf("n odd/even\n");
  printf("- --------\n");
  for (j = i - 1; j >= 0; j--)
    printf("%d %s\n", digits[j], (digits[j] % 2 == 0)? "even" : "odd");
  
  return 0;
}

jasonsmith206 said:
2.)
the same usernumber will be used to read prime numbers so

if it was to read

819877966

it would out put certain numbers saying what its divisable
example:
81 wouldn't be a prime number and would be broke up to its facortrization until it reaches a prime number.
"Output certain numbers" is not specific enough. There are many numbers that can be formed from the digits of your given number. Which of them do you need to check for being prime?
 
  • #3
Evgeny.Makarov said:
This is probably late, but I'll respond anyway. The code you have for part 1 makes sense and it works.

I am not sure what you mean by "read odd/even". Your program is supposed to print whether each digit is even or odd. You computed the digits; surely finding if they are even is easy.

Here is my program.
Code:
int main()
{
  unsigned int num;
  /* unsigned int takes <= 32 bits, which amounts to 10 decimal digits */
  char digits[10]; /* One byte is enough to store a decimal digit */
  int i, j;

  printf("Enter a number: ");
  scanf("%d", &num);
  /* Writing digits of num into the array digits in reverse order.
     Rightmost digit goes to the beginning of the array. */
  for (i = 0; num > 0; i++, num /= 10)
    digits[i] = num % 10;
  printf("n odd/even\n");
  printf("- --------\n");
  for (j = i - 1; j >= 0; j--)
    printf("%d %s\n", digits[j], (digits[j] % 2 == 0)? "even" : "odd");
  
  return 0;
}

"Output certain numbers" is not specific enough. There are many numbers that can be formed from the digits of your given number. Which of them do you need to check for being prime?
you're right "output certain numbers" was exactly what he gave us for instructions. He's a teacher who likes the fact that we should learn this on our own and that we should and i quote "fumble around, flop and flip before we understand what we're doing" which i get that i understand completely but don't do something like.

Ok class today's lesson is 1+1=2. Ok now that you understand that.. for homework tonight you're to find the answer to this equation \int sin^2xcos^7xdx

Thats how i feel this class is ran but I'm just complaining now, thank you for your help on number one. Like i said before it was easy to split the numbers but couldn't find a way to use the loop to nest in another one to find odd or even.
 

FAQ: Odd/Even and Prime: Check 819877966 Numbers

What is the difference between odd and even numbers?

The main difference between odd and even numbers is that odd numbers cannot be divided evenly by 2, while even numbers can. Odd numbers always have a remainder of 1 when divided by 2, while even numbers have a remainder of 0.

How do you determine if a number is odd or even?

To determine if a number is odd or even, you can divide the number by 2. If the remainder is 1, the number is odd. If the remainder is 0, the number is even.

What is a prime number?

A prime number is a positive integer that is only divisible by 1 and itself. In other words, a prime number has exactly two factors: 1 and the number itself. Examples of prime numbers include 2, 3, 5, 7, 11, and so on.

How do you check if a number is prime?

To check if a number is prime, you can try dividing it by all the numbers from 2 to the square root of the number. If none of these divisions result in a whole number, then the number is prime. Another method is to use the Sieve of Eratosthenes, which is an algorithm for finding all prime numbers up to a given number.

Can a number be both odd and prime?

No, a number cannot be both odd and prime. As mentioned earlier, odd numbers have a remainder of 1 when divided by 2. Since prime numbers only have two factors (1 and the number itself), they cannot be divided by 2 and therefore cannot have a remainder of 1. Therefore, all prime numbers are either even or odd, but not both.

Back
Top