C programming Language : Array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ashiela
    New Member
    • Apr 2010
    • 7

    C programming Language : Array

    how can i find negative even number using an array

    i've tried one but couldnt get the output.. can anyone correct my mistake:

    Code:
    main();
    {
    
    	int a[100], n, i;
    	printf("Enter the number of elements\n");
    	scanf("%d", &n);
    	for(i=a; i<n; i++);
    
    	{
    		scanf("%d", &a[i]);
    	}
    
    	for(i=0; i<n; i++);
    
    	{
    		if(a[i]%2==0)
    
    		{
    			printf("The even number %d \n", a[i]);
    		}
    
    	}
    
    }
    Last edited by Banfa; May 7 '10, 08:41 AM. Reason: Added [code]...[/code] tags
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    Code:
    for(i=a; i<n; i++);
    Is not this should be
    Code:
    for(i=0; i<n; i++);
    Regards
    Dheeraj Joshi

    Comment

    • ashiela
      New Member
      • Apr 2010
      • 7

      #3
      i'v correct the mistake but still wont work.

      Comment

      • Karlen
        New Member
        • May 2010
        • 5

        #4
        Originally posted by ashiela
        how can i find negative even number using an array

        i've tried one but couldnt get the output.. can anyone correct my mistake:

        Code:
        main();
        {
        
        	int a[100], n, i;
        	printf("Enter the number of elements\n");
        	scanf("%d", &n);
        	for(i=a; i<n; i++);
        
        	{
        		scanf("%d", &a[i]);
        	}
        
        	for(i=0; i<n; i++);
        
        	{
        		if(a[i]%2==0)
        
        		{
        			printf("The even number %d \n", a[i]);
        		}
        
        	}
        
        }
        instead of "if(a[i]%2==0)", try "if((a[i]%2==0) && (a[i]<0))".

        Comment

        • vinayvaka
          New Member
          • Nov 2007
          • 13

          #5
          i have changed the lineno 7 and 13. it will work.. check

          main();
          {

          int a[100], n, i;
          printf("Enter the number of elements\n");
          scanf("%d", &n);
          for(i=0; i<n; i++)

          {
          scanf("%d", &a[i]);
          }

          for(i=0; i<n; i++)

          {
          if(a[i]%2==0)

          {
          printf("The even number %d \n", a[i]);
          }

          }

          }

          Comment

          • Joseph Martell
            Recognized Expert New Member
            • Jan 2010
            • 198

            #6
            As your code is posted you have a ';' immediately after main. If your compiler compiles this (mine wouldn't) then you have an empty body on your main function and an un-named function body defined by your curly braces.

            Comment

            Working...