Learning C++: What is "for loop" and "array" in C language ? (Not for my C++ readers)

Pages

Wednesday, August 10, 2011

What is "for loop" and "array" in C language ? (Not for my C++ readers)

Hi ! I am here putting simple C program to demonstrate Loops and array in C language. Code is only for my C programmer friends,not for my C++ reader please wait for my next post regarding C++. This post is just because it urgently required by my C programmer friends.

Problem Definition: Program to count the number of student who has  pass in a particular subject in a given class and count Passing percentage

void main()
   {
    /* Variable Declaration */
    int student[100]; 
    // Declare integer array assuming max students are 100
    int nCount=0,i=0,passCounter=0;
    float passPercentage=0;
    clrscr();

    printf("\nEnter Number of Students: "); // Printing Message
    scanf("%d",&nCount); // Get count of number of students

    //Save marks for each student
    printf("\nEnter marks of student in sequence\n");
    for(i=0;i<nCount;i++)
    {
        printf("\nEnter mark for student number %d :",i+1); 
        // i+1 because for loop starts from 0
        scanf("%d",&student[i]); 
  // Dont write i+1 here because it saves number from 0 in Array
    }

    // Calculating count of pass students, here criteria of Passing is 40

    for(i=0;i<nCount;i++)
    {
         //checking mark of each student starting
         if(student[i]>40)
         {
        passCounter++; // Incrementing counter of Passing student
         }

    }

    // Calculating Percentage of Passing student

    passPercentage=(passCounter/nCount)*100;

    //Displaying result

    printf("\nCount of Pass Students are %d",passCounter);

    printf("\nPercentage of Pass students are%f",passPercentage);

    getch();
   } 

Comment me for any query regarding program.

No comments:

Post a Comment