Example for count 0 count 10 count printf Enter a valuen scanf d num num num 10

Example for count 0 count 10 count printf enter a

This preview shows page 4 - 8 out of 11 pages.

Example: for (count = 0 ; count < 10 ; count++) { printf (“Enter a value\n”) ; scanf (“%d”, &num) ; num = num + 10 ; printf (“NUM = %d\n”, num) ; } In the above example the integer variable count is first assigned a value of 0 , it is initialized . Then the value of count is checked if it is less than 10 . If it is < 10 the loop body asks to enter a value for num . Then the value of count is incremented by 1 and again count is checked for less than 10 . If it is true the loop body is again executed and then the count is incremented by 1 and checked for < 10 . It keeps on doing like this until the count is >= 10 . Then the loop stops. So, here the loop is executed 10 times. Note: The increment operation is written as ++ and decrement operation is written as -- . The increment increases the value by 1 and decrement decreases the value by 1. Useful Solved Examples using different loops : Example 1: Page 4 of 11
Image of page 4
Print numbers ranging from 1 to n with their squares using for loop. The value of n is read by the program. #include<stdio.h> void main() { int num,square,limit; printf("Please Input value of limit : "); scanf("%d", &limit); for (num=1; num < =limit; ++num) { square = num*num; printf("%5d %5d\n", num, square); } // end of for } // end of main Example 2 . : /********************************************************** Program Showing a Sentinel-Controlled for loop. To Compute the sum of a list of exam scores and their aerage. ***********************************************************/ #include <stdio.h> Page 5 of 11
Image of page 5
#define SENTINEL -99 int main(void) { int sum = 0, /* sum of scores input so far */ score, /* current score */ count=0; /* to count the scores */ double average; printf("Enter first score (or %d to quit)> ", SENTINEL); for (scanf("%d", &score); score != SENTINEL; scanf("%d", &score)) { sum += score; count++; printf("Enter next score (%d to quit)> ", SENTINEL); } // end of for loop if(count>0) { average=sum/count; printf("\nSum of exam scores is %d\n", sum); printf(“the average of exam scores is %f”,average); }else printf(“no scores entered”); return (0); } // end of main Page 6 of 11
Image of page 6
Example 3 : while loop : /*Conditional loop Using while Statement */ #include<stdio.h> #define RIGHT_SIZE 10 void main() { int diameter; printf("Please Input Balloon's diameter > "); scanf("%d",&diameter); while (diameter < RIGHT_SIZE) { printf("Keep blowing ! \n"); printf("Please Input new Balloon's diameter > "); scanf("%d", &diameter); } // end of while loop printf("Stop blowing ! \n"); } // end of main Example 4 . : do-while loop : Page 7 of 11
Image of page 7
Image of page 8

You've reached the end of your free preview.

Want to read all 11 pages?

  • Spring '10
  • zaman
  • loop, Do while loop

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes