The need:
You have seen the previous program how we added 'n' numbers. In the end of post I told you about other loops.The program below illustrates the use of 'for loop'.
The code:
--------------------------------------------
/*Addition program using for loop*/
#include<stdio.h>
main()
{
int m,n;
float i,sum=0;
printf("how many numbers do you want to add?\n");
scanf("%d",&n);
printf("Now keep on giving numbers ...\nPress ENTER after each number...\n");
for(m=0;m<n;m++) //for loop
{
scanf("%f",&i); //scanning number
sum=sum+i; // adding number to sum
}
printf("__________________\n=%f\n\n\n",sum);
}
--------------------------------------------
'for loop' is most commonly used loop when counter is used and limit to the counter is specified because the for loop body facilitate the increment of the counter. Like the previous program here also I am using variable 'm' as counter. You see in the body of for loop there are three fields separated by semicolon (;). In first field m=0 is written. This field is called initialization field. Here in our program this means that initial value(when first run of the loop is about to start) of our counter 'm' is 0. The second field which has m<n written within it is the field for condition. This tells the program to continue loop until the condition specified in that field holds good. In the third field increment or decrement of the counter takes place. Here m++ is short style of writing m=m+1. Basically it is incrementing value of m.In all other aspects this program is same as previous program. you can refer to link below:
program 12: addition of n numbers
Remarks:
a). Note that when we write m++, value of 'm' gets incremented after the corresponding run of loop finishes. This is called post increment. If we write ++m, value of 'm' gets incremented before the corresponding run of loop starts. This is called preincrement.
You have seen the previous program how we added 'n' numbers. In the end of post I told you about other loops.The program below illustrates the use of 'for loop'.
The code:
--------------------------------------------
/*Addition program using for loop*/
#include<stdio.h>
main()
{
int m,n;
float i,sum=0;
printf("how many numbers do you want to add?\n");
scanf("%d",&n);
printf("Now keep on giving numbers ...\nPress ENTER after each number...\n");
for(m=0;m<n;m++) //for loop
{
scanf("%f",&i); //scanning number
sum=sum+i; // adding number to sum
}
printf("__________________\n=%f\n\n\n",sum);
}
--------------------------------------------
'for loop' is most commonly used loop when counter is used and limit to the counter is specified because the for loop body facilitate the increment of the counter. Like the previous program here also I am using variable 'm' as counter. You see in the body of for loop there are three fields separated by semicolon (;). In first field m=0 is written. This field is called initialization field. Here in our program this means that initial value(when first run of the loop is about to start) of our counter 'm' is 0. The second field which has m<n written within it is the field for condition. This tells the program to continue loop until the condition specified in that field holds good. In the third field increment or decrement of the counter takes place. Here m++ is short style of writing m=m+1. Basically it is incrementing value of m.In all other aspects this program is same as previous program. you can refer to link below:
program 12: addition of n numbers
Remarks:
a). Note that when we write m++, value of 'm' gets incremented after the corresponding run of loop finishes. This is called post increment. If we write ++m, value of 'm' gets incremented before the corresponding run of loop starts. This is called preincrement.
No comments:
Post a Comment
feel free to ask your doubts... if any
you can post your doubts on
www.facebook.com/programsimply