Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

why its print all negitive number

+7 votes
asked Feb 14, 2022 by Dheeraj Paswan (200 points)
#include <stdio.h>

int main()
{
    int A[10],i;
    for(i=0;i<=9;i++){
         printf("enter the element");
         scanf("%d",&A[i]);
    }
    for(i=0;i<=9;i++){
         
       if(A[i]%2==0){
            printf("p%d ",A[i]);
           }
          else(A[i]%2==1);{
          
               printf("n%d",A[i]);}
          
    }
    
    return 0;
}

1 Answer

+2 votes
answered Feb 14, 2022 by Peter Minarik (86,900 points)
edited Feb 14, 2022 by Peter Minarik
else(A[i]%2==1);{

the above line correctly:

else if (A[i] % 2 == 1) {

notice the if and no semicolon.

But to be fair, you could just write simply

else {

as at this point A[i] is already guaranteed to be odd. :)

One more note: the program does not print if a number is positive (p) or negative (n); it prints if a number is even (p) or odd (n). For this, it would be better to print "even" or "odd" and not "p" or "n".

If you need to decide if a number is positive or negative, then you should change your if condition accordingly.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...