-3

Write c program where the user takes an array with n elements and finds the elements whose value is a multiple of the number 5.These elements are displays on screen along with their value like this : V[i]=a ,V[j] = b,.... My code :

#include <stdio.h>
int main ()

    {
        int n,i,sh=0;
        int v[100];
        printf ("Please write n:");
        scanf("%d",&n);
        for (i=0;i<n;i++)
        { printf ("\n Write the element %d",i);
        scanf("%d",&v[i]);
        }
        if (v[i] %5)
        printf("The element is a multiple of 5",&sh);

        return 0;
    }

It compiles perfectly,but when I run this and I write the elements,it does nothing..where am I wrong?

EDIT :

Yes,here it is :
Please write n: 4
Enter value 0: 10
Enter value 1 : 9
Enter value 2 : 20
Enter value 3:14

V[0]=10,V[2}=20
user2685334
  • 13
  • 1
  • 7

5 Answers5

2

This:

if (v[i] %5)
    printf("The element is a multiple of 5",&sh);

After the first loop your i is equal to n+1. You don't reset it neither check your elements in a loop.
Also your format string doesn't have any %p in it, why do you pass %sh?

Do something like:

 for (i=0;i<n;i++)
    if (v[i] %5) // not divided by 5
       // Correct prtinf() statement 
       // printf("The element v[%d] = %d is NOT multiple of 5", i, v[i]);
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
aragaer
  • 17,238
  • 6
  • 47
  • 49
  • To whoever edited my post - not necessarily index-out-of-bounds at this point. The loop goes up to n, which can be more, less or equal than array size (which is 100). – aragaer Aug 15 '13 at 09:15
2

You have three errors in your code.

  • First if ( v[i] % 5 ) is outside of the loop at the end, it will just try this once for i = n+1. You will have the out-of-bound problem.

  • And you search a multiple of 5 so the condition should be if ( v[i] % 5 == 0 ).

  • After, your printf is also wrong.

    printf("The element is a multiple of 5",&sh);
    //                                     ^^^^
    

What is sh ? Why do you want to use it in printf ? You format string does not seem to want an argument.

Your code should look like this :

for ( i=0; i < n; i++ )
{
    printf( "\n Write the element %d", i );
    scanf( "%d", &v[i] );
    if ( v[i] % 5 == 0 )
        printf( "The element is a multiple of 5" );
}

EDIT :

With the out put example, maybe you should do another loop :

#include <stdio.h>

int main ()
{
    int n, i, sh = 0; // I still don't for what sh is used ...
    int v[100];

    printf ("Please write n:");
    scanf("%d",&n);

    // Get the values
    for ( i=0; i < n; i++ )
    {
        printf( "\n Write the element %d", i );
        scanf( "%d", &v[i] );
    }

    // Print the values multiple of 5
    for ( i=0; i < n; i++ )
    {
        if ( v[i] % 5 == 0 )
            printf( "V[%d]=%d\n", i, v[i] );
    }

    return 0;
}

HERE is the example working.

Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
2
  1. The IF statement isn't in the loop
  2. printf("The element is a multiple of 5",&sh); - am I missing something? (why the &sh?)
  3. The IF statement isn't totally correct

How would I edit it:

 1|    #include <stdio.h>
 2|
 3|    int main ()        
 4|    {
 5|        int n,i,sh=0;
 6|        int v[100];
 7|        printf ("Please write n:");
 8|        scanf("%d",&n);
 9|        for (i=0;i<n;i++)
10|        { 
11|             printf ("\n Write the element %d",i);
12|             scanf("%d",&v[i]);
13|             
14|             if ((v[i] % 5) == 0)
15|                 printf("The %d. element %d is a multiple of 5", i+1, v[i]);
16|        }
17|
18|        return 0;
19|    }

Just wondering what is the purpose of variable sh? Or for what did the author wanted to use it?

Joe DF
  • 5,438
  • 6
  • 41
  • 63
Ms. Nobody
  • 1,219
  • 3
  • 14
  • 34
0
{
    int n,i,sh=0;
    int v[100];
    printf ("Please write n:");
    scanf("%d",&n);
    for (i=0;i<n;i++)
    { printf ("\n Write the element %d",i);
    scanf("%d",&v[i]);
    }

    /// now go back through the array
    //
    for( i = 0; i < n; i++ )
       if( v[i] % 5 == 0 )
           printf( "The element at %d is %d, divisible by 5\n", i, v[i] );

    return 0;
}
Dru
  • 1,398
  • 9
  • 6
0

int n,i,sh=0;

//array to hold the numbers
int N[100];

//count of numbers
printf ("Enter the NUMBER count :");
scanf("%d",&n);

//get the input in an array for (i=0;i

//check for the divisibility
for( i = 0; i < n; i++ )
   if( N[i] % 5 == 0 )
       printf( "The element at %d is %d, divisible by 5\n", i, N[i] );

return 0;

}

Anonymous
  • 55
  • 7