Sunday, August 2, 2009

Plz help me with this C programme.?

Add proper comments to this C programme





#include%26lt;stdio.h%26gt;


void main()


{





int arr[10],i,odd,even,neg;


clrscr();


i=odd=even=neg=0;


printf("Enter 10 numbers\n");


for (i=0;i%26lt;10;i++)


{


scanf("%d",%26amp;arr[i]);


}





for (i=0;i%26lt;10;i++)


{


if (arr[i]%26lt;0)


{


neg=neg+1;


}


else if ((arr[i]%2==0) %26amp;%26amp; (arr[i]%26gt;=0))


{


even=even+1;


}


else


{


odd=odd+1;


}


printf("\n\nNo. of Negative numbers entered are: %d", neg);


printf("\n\nNo. of Even numbers entered are: %d", even);


printf("\n\nNo. of ODD numbers entered are: %d", odd);


getch();


}

Plz help me with this C programme.?
#include%26lt;stdio.h%26gt;// Include the standard I/O header file into the


//current file


void main()// The procedure that is invoked first when executed.


//The function does not return anything back to the caller (void)


{// Starting of the function, i.e. beginning of the scope for main()





int arr[10],i,odd,even,neg; // Declaring an integer array consisting of 10 locations starting @ index 0 and ending @ 9.


// i.e. arr[0] to arr[9]. i, odd, even, neg are integer variables used to


//store integer values


clrscr(); // clrscr() is a function defined in conio.h %26amp; used to clear the output screen. similar to


//cls in DOS


i=odd=even=neg=0;// Initialize the variables to 0


printf("Enter 10 numbers\n"); // Prints the string on the console


for (i=0;i%26lt;10;i++) // loopin construct, syntax : for(inital value; termination condition; step increments)


{


scanf("%d",%26amp;arr[i]); // take 10 inputs from user n store it in the array indexed from arr[0..9]


}





for (i=0;i%26lt;10;i++) // Check each to the element in the array for some condition given below


{


if (arr[i]%26lt;0) // If arr[index value i.e. i] %26lt; 0


{


neg=neg+1; // Increment the neg variable indicating that u found a negative no.


}


else if ((arr[i]%2==0) %26amp;%26amp; (arr[i]%26gt;=0))// Else, if arr[i] divided by 2 gives a remainder of 0 and the element arr[i]


//itself is greater than or equal to 0, then the no. is even


{


even=even+1;// Increment the even variable by 1


}


else


{


odd=odd+1;// otherwise, the no. has to be odd, so increment the odd variable


}


// Print the computed output on screen using printf() statements


printf("\n\nNo. of Negative numbers entered are: %d", neg);


printf("\n\nNo. of Even numbers entered are: %d", even);


printf("\n\nNo. of ODD numbers entered are: %d", odd);


getch(); // Keep the output screen active until user presses any key


}// Once a key is pressed, the program terminates!











Hope this helped!
Reply:First of you wrongly spelled "Programme". In case of programming the spelling is Program.





Will you please forward your program with the actual problem that means what did you want to get as output by the program. Just send me your "problem" in chandra.kuntal@gmail.com. I'll confirmly try to solve then, just now I don't have any more time. So forward it %26amp; wait for a maximum of 24 hours. Alright?
Reply:/* check one bracket that is missing in ur programme





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{





int arr[10],i,odd,even,neg;


clrscr();


i=odd=even=neg=0;


printf("Enter 10 numbers\n");


for (i=0;i%26lt;10;i++)


{


scanf("%d",%26amp;arr[i]);


}





for (i=0;i%26lt;10;i++)


{


if (arr[i]%26lt;0)


{


neg=neg+1;


}


else if ((arr[i]%2==0) %26amp;%26amp; (arr[i]%26gt;=0))


{


even=even+1;


}


else


{


odd=odd+1;


}





} /* this bracket was not here before





printf("\n\nNo. of Negative numbers entered are: %d", neg);


printf("\n\nNo. of Even numbers entered are: %d", even);


printf("\n\nNo. of ODD numbers entered are: %d", odd);


getch();


}
Reply:This is the way i understood ur pgm:-


You want to get 10 numbers from user and wants to display how many even,odd and negative numbers in those.


So here goes my suggestion.





You code is correct.


Only one thing u have to change.





inside for loop:-


At first itself u are checking whether its %26lt;0, so while dealing with even number why are u checking again arr[i]%26gt;=0 so its waste know.


So u can omit that condition. the rest are correct. u can go head.
Reply:First of all in the program u didn't close the for loop.





before printf() statements u close the for loop brace }.





ok.





u want explanation of this prog?





1)PROGRAM START AT MAIN().


2)WE ARE DECLARING ONE INTEGER ARRAY , AND 3 INTEGERS ODD,EVEN AND NEG TO STORE THE COUNT OF EVEN, ODD AND NEGATIVE NUMBERS.


3)CLRSCR() USED TO CLEAR THE SCREEN.





4)PRINTF STATEMENT PRINT "ENTER 10 NUMBERS ON THE SCREEN. /n FOR NEW LINE.


5)the for loop is used to input 10 values into the ARRAY[];


6)In the next FOR loop we are checking the given 10 no's whethter they are even,odd or negative.


7)1st if condition check for -ve no's ( no's less than 0 are -ve no's), if the no is %26lt;0 so incrementing the count of NEG Variable by 1.


8)if the no is not -ve next ELSE condition check for even no.


for even no, the no is divisible by 2 and it is+ve no if this condition is satisfied increment the EVEN variable by 1.


9)the no is not even and -ve no automatically the no is odd no.





10)finally we print all those countings by using PRINTF statements.


11)getch() is used to wait to press any key from a keyboard.


12)#include%26lt;stdio.h%26gt;includes all the header files for predefined functions like printf, scanf().





here no means NUMBER





NOTE: no need to check for arr[i]%26gt;=0 in else if statement.


why because 0 is not even, odd and -ve no. but no problem at all with this.








OK.


ALL THE BEST
Reply:I addition to what mentioned above; when talking about computers we say "program" NOT "programme".





Thanks.


No comments:

Post a Comment