Thread: Problem with part of program

  1. #1
    Registered User
    Join Date
    May 2021
    Posts
    1

    Problem with part of program

    Hello!
    I need from help for this program:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        int N;
        int a;
        int mas[100];
        int rem;
        int product=1;
            printf("Enter numbers: ");
         scanf("%d", &N);
         if (N <= 100)
        {
           for (a = 0; a < N; a++)
           {
          printf("Enter number: ");
          scanf("%d", &mas[a]);
           }
           for (a = 0; a < N; a++)
           {
            printf("The numbers are: %d,\t", mas[a]);
           }
           for (a = 0; a < N; a++)
           {
               if (mas[a] != 0)
               {
                   rem = a % 10;
                   product = product * rem;
                   a = a / 10;
               }
               printf("%d", mas[a]);
           }
        }
        return 0;
    }
    How to find the element of the array that has the largest product of its digits? This is the part of the code that has a problem, but I don't know what it is:
    Code:
    for (a = 0; a < N; a++)       {
               if (mas[a] != 0)
               {
                   rem = a % 10;
                   product = product * rem;
                   a = a / 10;
               }
               printf("%d", mas[a]);
           }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well start with a separate test program like this.
    Code:
    int product_of_digits(int n) {
      int product = 1;
      // do something here
      return product;
    }
    int main ( ) {
      int n = 1234;
      printf("The digit product of %d = %d\n", n, product_of_digits(n));
    }
    Test it with a few other numbers also.


    WHEN you've made that work to your satisfaction, then you can do this in your original program.
    Code:
           for (a = 0; a < N; a++)
           {
                 printf("The digit product of %d = %d\n", mas[a], product_of_digits(mas[a]));
           }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2021
    Posts
    138
    Also, you need to have a tracking variable, to scan through the loop and remember which was the greatest sum. Something like this:

    Code:
    int tracking_variable = 0;
    for (... loop ...) {
        result = operation(inputs);
        if (result > tracking_variable) {
            tracking_variable = important_input;
        }
    }
    printf("The value with the greatest result was %d\n", tracking_variable);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help on last part of program
    By coder121 in forum C Programming
    Replies: 2
    Last Post: 01-30-2017, 07:25 PM
  2. Help with part of program
    By mytrademark in forum C Programming
    Replies: 3
    Last Post: 10-13-2011, 03:24 PM
  3. why the control goes to else part in this program
    By vapanchamukhi in forum C Programming
    Replies: 2
    Last Post: 01-13-2009, 07:09 AM
  4. Problem with part of program
    By ammochck21 in forum C++ Programming
    Replies: 8
    Last Post: 11-09-2006, 06:45 AM
  5. How do you branch to another part of the program?
    By gcn_zelda in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2003, 12:27 PM

Tags for this Thread