MHB Odd/Even and Prime: Check 819877966 Numbers

  • Thread starter Thread starter jasonsmith206
  • Start date Start date
  • Tags Tags
    Prime
AI Thread Summary
The discussion revolves around two programming assignments involving the user number 819877966. The first assignment requires breaking the number into individual digits and determining if each digit is odd or even. The original code provided successfully splits the number but lacks the logic to check for odd/even status. Suggestions include using a simple modulus operation to determine the parity of each digit after extraction.The second assignment involves checking which combinations of the digits are prime numbers and requires further clarification on which specific numbers to evaluate for primality. There is frustration expressed regarding the difficulty of these assignments for beginners, with a comparison made to teaching methods that seem overly complex for the current level of understanding. Overall, the conversation highlights the challenges faced by novice programmers in grasping fundamental concepts while navigating complex tasks.
jasonsmith206
Messages
10
Reaction score
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
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?
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top