U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display...

20
66 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI) A loop refers to a group of statements or even a single statement within the program which will be repeatedly executed again and again. There are 3 looping statements 1. while() 2. for() and 3. do..while() Whenever a group of statements are to be used again and again we write these statements in a loop. while() loop In the while loop the condition is checked first. If this condition is satisfied, the statements within the loop are executed. This continues till the condition is not satisfied, at which point the loop is terminated. e.g. i=1; while(condition) while (i<10) { { program statement; i++; program statement; } } Program 36 : Write a program in C to display the integers from 1 to 5 /*using the while loop*/ #include<stdio.h> void main() { int x=1; while(x<=5) { printf(“%2d”,x); x++; } } Program output: b1b2b3b4b5 (where b denotes a blank space, each integer is displayed in 2 columns as we have used % 2d). U n i t Loops II – 8 –

Transcript of U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display...

Page 1: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

66 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI)

A loop refers to a group of statements or even a single statement within the program which will be repeatedly executed again and again. There are 3 looping statements

1. while()

2. for() and

3. do..while()

Whenever a group of statements are to be used again and again we write these statements in a loop.

while() loop

In the while loop the condition is checked first. If this condition is satisfied, the statements within the loop are executed. This continues till the condition is not satisfied, at which point the loop is terminated.

e.g. i=1;

while(condition) while (i<10)

{ {

program statement; i++;

program statement;

} }

Program 36 : Write a program in C to display the integers from 1 to 5

/*using the while loop*/

#include<stdio.h>

void main()

{ int x=1;

while(x<=5)

{

printf(“%2d”,x);

x++;

}

}

Program output:

b1b2b3b4b5 (where b denotes a blank space, each integer is displayed in 2 columns as we have used % 2d).

Unit

Loops

II

– 8 –

Page 2: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

Loops 67

Program 37

Write program in C to display all odd integers from 21 to 36

Solution:

/*printing all odd integers from 21-36*/

#include<stdio.h>

void main()

{ int i=21;

while(i<=36)

{ printf(“%d\n”,i);

i=i+2;

}

}

Output :

21

23

25

27

29

31

33

35

The for() loop

The for( ) loop is the most popular loop statement in C.

Its general form is

for(initial value; condition; increment/decrement) e.g. for( i=1; i<=6; i++)

{ {

statements; printf(“%2d”, i);

} }

Output : b1b2b3b4b5b6

There are three statements inside the for loop separated by semicolons. The first statement i = 1 gives the initial value 1 to i. This statement is executed only once. The third or last statement i++ increases the value of i by 1, and it is executed again and again at every repetition, except for the first time. The second or middle statement i<=6 is the condition and it controls the loop. This is checked repeatedly. The statements inside the loop are executed repeatedly as long as the condition i<=6 is satisfied. When this condition is not satisfied the loop is terminated and control is given out of the loop.

Program 38

Write a program in C using a ‘for()’ loop to display all the positive integers from 1 to 100 that are exactly divisible by 11.

/*Nos divisible by 11*/

# include <stdio.h>

void main()

Page 3: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

68 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI)

{int i;

for(i=1;i<=100;i++)

{

if(i%11==0)

printf(“%d ”,i);

}

}

The Comma operator.

The comma operator can be introduced in the for() statement as per the following syntax :

for(expression1a,expression1b;expression2;expression3a,expression3b)

e.g.

# include <stdio.h>

main()

{int x, y;

for(x = 3, y = 8; x < 6; x + +, y --)

printf(“%d %d\n”, x, y); }

will display

3 8

4 7

5 6

do..while() Loop

In this loop the statements within the loop are executed first and then the condition is checked, as the condition is written at the end of the loop. If the condition is satisfied the loop is repeated, if the condition is not satisfied the loop terminates. The do loop is simply a transposition of the while loop.

do

program statement

while(expression);

Note : There is a semicolon after while.

This statement is most often used when it is desirable to execute a loop at least one time.

e.g. # include <stdio.h>

main()

{ int i=1;

do

{

printf(“%d ”,i);

i=i+1;

}

while(i<=5); }

will display 1b2b3b4b5 (where b denotes a blank space)

Page 4: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

Loops 69

Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop.

#include<stdio.h>

main()

{ int i=1;

do

{

printf(“APPLE\n”);

i++;

}

while(i<=5);

}

The do loop is widely used in programs which display menus on the screen, as these Menus have to be displayed at least once.

PROGRAMS WITH REPETITIONS

Program 40. Write a program in C to input for 5 persons their purchases and calculate and display the discount and net amount of each person where the discount is 1.5% of the purchases upto 3000 and 2% on the excess.

/* discount of 5 persons with for() loop*/ | /* discount of 5 persons with while() loop*/

#include<stdio.h> | #include<stdio.h>

void main() | void main()

{ int i; | { int i=1;

float p,d,na; | float p,d,na;

for(i=1;i<=5;i++) | while(i<=5)

{ printf(“Enter Purchases “); | { printf(“Enter Purchases “);

scanf(“%f”,&p); | scanf(“%f”,&p);

if(p<=3000) | if(p<=3000)

d=p*.015; | d=p*.015;

else | else

d=3000*.015 + (p-3000)*.02; | d=3000*.015 + (p-3000)*.02;

na = p-d; | na = p-d;

printf(“Discount = %.2f \t”,d); | printf(“Discount = %.2f \t”,d);

printf(“Net Amount = %.2f \n”,na); | printf(“Net Amount = %.2f\n”,na);

} } | i++; } }

PROGRAMS ON SUM OF SERIES

Program 41

Write a program in C to calculate and display the sum of 1+2+3+…..+100

Solution :

/*Printing sum of first 100 integers*/

#include<stdio.h> #include<stdio.h>

void main() void main()

Page 5: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

70 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI)

{ {

int i=1, s=0; or int i, s=0;

while(i<=100) for(i=1; i<=100; i++)

{ {

s+=i; s += i; /*curly brackets are optional here*/

i++; }

}

printf(“sum=%d\n”,s); printf(“sum=%d\n”,s);

} }

Program 42

Write a program in C to calculate and display the sum of 2+5+8+..+302

Solution :

/*sum*/

#include<stdio.h> #include<stdio.h>

void main() void main()

{ {

int i=2,s=0; or int i, s=0;

while(i<=302) for(i=2; i<=302; i += 3)

{ {

s+=i; s += i;

i+=3;

} }

printf(“sum=%d\n”,s); printf(“sum=%d\n”,s);

} }

Program 43

Write a program in C to calculate and display 2/5+4/8+..202/305

Solution :

/*SUM OF FRACTIONS 2/5+4/8+..202/305 */

#include<stdio.h> #include<stdio.h>

void main() void main()

{ {

float i=2,x=5,s=0; or float i, x = 5, s = 0;

while(i<=202) for(i = 2; i <= 202; i += 2)

{ {

s+=i/x; s += i/x;

i+=2; x += 3;

x+=3;

} }

printf(“sum=%f\n”,s); printf(“sum=%f\n”,s);

} }

Page 6: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

Loops 71

Program 44

Write a program in C to find and display sum of (2 × 3)/(5 × 6)+(4 × 5)/ (8 × 9)+ ...(202 × 203)/(305 × 306)

Solution :

#include<stdio.h> #include<stdio.h>

void main() void main()

{ {

float i=2,x=5,s=0; or float i, x = 5, s = 0;

while(i<=202) for(i = 2; i <=202; i +=2)

{ {

s+= (i*(i+1))/(x*(x+1)); s+= (i*(i+1))/(x*(x+1));

i+=2; x+=3;

x+=3;

} }

printf(“sum=%f\n”,s); printf(“sum=%f\n”,s);

} }

Program 45

Write a program in C to find and display sum of 22/5 + 32/6 + 42/7 + ….. + 322/35

#include<stdio.h> #include<stdio.h>

void main() void main()

{ {

float i=2,x=5,s=0; or float i, x = 5, s = 0;

while(i<=32) for(i = 2; i <= 32; i ++)

{ {

s+=(i*i)/x; s+=(i*i)/x;

i++; x ++;

x++;

} }

printf(“sum=%f\n”,s); printf(“sum=%f\n”,s);

} }

Program 46

Write program in C to find and display sum of 22/52 + 32/62 + 42/72 + ….. + 322/352

#include<stdio.h> #include<stdio.h>

void main() void main()

{ {

float i=2,x=5,s=0; or float i, x = 5, s = 0;

while(i<=32) for(i = 2; i <= 32; i ++)

{ {

s+=(i*i)/(x*x); s+=(i*i)/(x*x);

i++; x ++;

x++;

Page 7: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

72 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI)

} }

printf(“sum=%f\n”,s); printf(“sum=%f\n”,s);

} }

Program 47

Write a program in C to find and display sum of the Fibonacci sequence 1+1+2+3+5+….+55

#include<stdio.h>

void main()

{ int a=1,b=1,c,s=0;

while(a<=55)

{ c = a+b;

s += a; a = b; b = c; }

printf(“Sum = %d\n”,s);

}

PROGRAMS ON DIVISIBILITY OF NUMBERS

Program 48

Write a program in C to input a positive integer and display whether it is divisible by 3 or not.

#include<stdio.h>

void main()

{ int x;

printf(“Enter a positive integer \n”);

scanf(“%d”,&x);

if(x%3==0) /* if the remainder is 0 then it is divisible*/

printf(“%d is divisible by 3\n”,x);

else

printf(“%d is not divisible by 3\n”,x);

}

Program 49

Write a program in C to input 10 positive integers and display whether they are divisible by 3 or not

#include<stdio.h>

void main()

{ int x, i;

for(i=1; i<=10; i++)

{ printf(“Enter a positive integer \n”);

scanf(“%d”,&x);

if(x%3==0)

Page 8: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

Loops 73

printf(“%d is divisible by 3\n”,x);

else

printf(“%d is not divisible by 3\n”,x);

}

}

Program 50

Write a program in C to display all the numbers between 32 and 76 which are divisible by 8 horizontally and then print their sum.

/* Nos between 32 and 76 divisible by 8*/

#include<stdio.h>

void main()

{int s=0, i=33;

while(i<=75)

{ if(i%8==0)

{ printf(“%d ”,i);

s+=i;}

i++;

}

printf(“\n sum if the numbers divisible by 8=%d\n”,s);

}

PROGRAMS ON PRINTING NUMBERS

Program 51

Write a program in C to display the integers from 100 to 1, 10 numbers per line.

#include<stdio.h>

void main()

{ int x;

for(x=100;x>=1;x--)

printf(“%8d”,x);

}

Program 52

Write a program in C to display all the odd integers from 17 to 85 , 8 numbers per line.

#include<stdio.h>

void main()

{ int x;

for(x=17; x<=85;x+=2)

printf(“%10d”,x);

}

Page 9: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

74 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI)

Program 53

Write a program in C to display the multiplication table from 5 × 1=5.....5 × 10=50

# include <stdio.h>

void main()

{ int i;

for(i=1;i<=10;i ++)

printf(“5x%d = %d \n”,i,5*i);

}

Program 54

Write a program in C to input an integer and display it in reverse. E.g 453 printed as 354.

# include <stdio.h>

void main()

{ int n,d;

printf(“Enter an integer ”);

scanf(“%d”, &n);

do

{ d=n%10;

printf(“%d”,d);

n=n/10; }

while(n!=0);

}

PROGRAMS ON SIMPLE AND COMPOUND INTEREST

Compound Interest of each year tabular output

Program 55

Write a program in C to input principal amount(p) number of years(n) and rate(r) and display the compound interest table by the Iteration method showing the years the compound interest in each year in the following format :

Year Compound Interest

1

2

3

#include <stdio.h>

void main()

{ int i,n;

float p,r,ci;

printf(“Enter Principal amount, rate of interest and number of years ”);

scanf(“%f %f %d”,&p,&r,&n);

printf(“Year\tCompound Interest\n”);

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

{ ci=p*r/100;

Page 10: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

Loops 75

p=p+ci; /* adding the interest back to the Principal Amount*/

printf(“%4d\t%17.2f\n”,i,ci); /* 4 and 17 are the size of the headings*/

}

}

Simple and Compound Interest of each year tabular output

Program 56

Write a program in C to input principal amount(p) number of years(n) and rate(r) and display the simple and compound interest table showing the years, the simple, compound interest in each year in the following format :

Year Simple Interest Compound Interest

1 2 3

#include <stdio.h>

void main()

{ int i,n;

float p,r,si,ci;

printf(“Enter Principal amount, rate of interest and number of years ”);

scanf(“%f %f %d”,&p,&r,&n);

printf(“Year\tSimple Interest\tCompound Interest\n”);

si = p*r/100;

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

{ ci=p*r/100;

p=p+ci; /* adding the interest back to the Principal Amount*/

printf(“%4d\t%15.2f\t%17.2f\n”,i,si,ci); /* 4 , 15 and 17 are the size of the headings*/

}

}

Compound Interest and Maturity amount of each year.

Program 57

Write a program in C to input principal amount(p) number of years(n) and rate(r) and display for each year the compound interest and the maturity value.

#include <stdio.h>

void main()

{ int i,n;

float p,r,ci;

printf(“Enter Principal amount, rate of interest and number of years ”);

scanf(“%f %f %d”,&p,&r,&n);

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

{ ci=p*r/100;

Page 11: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

76 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI)

p=p+ci; /* adding the interest back to the Principal Amount*/

printf(“Year %d Compound int %.2f Maturity Val. %.2f\n”,i,ci,p);

}

}

Note : The output of the above program will not be tabular as in above Programs 55 and 56.

PROGRAMS ON DEPRECIATION

Depreciation by reducing balance method and W.D.V. tabular output.

Program 58

Write a program in C to input the Cost of an item(c) and Rate of depreciation(r) and display a table showing year(y), depreciation(d) and the written down value for the first 10 years where the depreciation is calculated using reducing balance method.

#include<stdio.h>

void main()

{ int y;

float cost,rate,depr;

printf(“Enter Cost and Rate of depreciation ”);

scanf(“%f %f”, &cost, &rate);

printf(“Year\tDepreciation\tWritten down Value\n”);

for(y=1;y<=10;y++)

{ depr = cost*rate/100;

cost = cost - depr;

printf(“%4d\t%12.2f\t%18.2f\n”,y,depr,cost); } /*4, 12 and 18 are the size of the headings*/

}

Depreciation by St. Line and Reducing balance methods.

Program 59

Write a program in C to input the Cost of an item(c) and Rate of depreciation(r) and display the year(y), depreciation using St. line method and Depreciation using reducing balance method for each of the first 10 years.

#include<stdio.h>

void main()

{ int y;

float cost,rate,depsl,deprb;

printf(“Enter Cost and Rate of depreciation ”);

scanf(“%f %f”, &cost, &rate);

depsl = cost*rate/100;

for(y=1;y<=10;y++)

{ deprb = cost*rate/100;

Page 12: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

Loops 77

cost = cost - deprb;

printf(“Year %d Depr St. Line = %.2f Depr. Reduc. Bal. = %.2f\n”, y, depsl, deprb); }

}

Depreciation by St. Line method and Depreciated value

Program 60

Write a program in C to input the Cost of an item(c) and Rate of depreciation(r) and display the year(y) , depreciation using St. line method and the depreciated value for each of the first 8 years.

#include<stdio.h>

void main()

{ int y;

float cost,rate,depsl;

printf(“Enter Cost and Rate of depreciation ”);

scanf(“%f %f”, &cost, &rate);

depsl = cost*rate/100;

for(y=1;y<=8;y++)

{ cost = cost - depsl;

printf(“Year %d Depr St. Line = %.2f Depr. value = %.2f\n”, y, depsl, cost);}

}

Output Programs

Output programs based on while() loop

Program 61. Give the output of the following program

void main() { int a=1,b=1; while(a<100000) {printf("%d\n",b); a*=10; b+=a;} }

output

1

11

111

1111

11111

Program 62. Give the output of the following program. void main() { int n=1; while(n<10) printf(“%d “,++n); } output 2 b3b4b5b6b7b8b9b10 where b denotes a blank space.

Page 13: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

78 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI)

Program 63. Give the output of the following program.

void main()

{ int n=254,d,t=0;

while(n!=0)

{d=n%10;

t=t*10+d;

n/=10;}

printf("%d\n",t);

}

output

452

Program 64. Give the output of the following program.

main() {int x=1,t=0; while(x<6) { if(x%2==0) t+=2*x; else t+=2; printf("%d %d\n",x,t); x++;} }

output

1b2

2b6

3b8

4b16

5b18

Program 65.

Give the output of the following program :

void main()

{

int i,j;

i=j=0;

while(j<5) ++j; i+=j;

printf(“i=%d\t j=%d\n”,i,j);

}

output: i=5 j=5

Note : Since brackets are not given after while, only the first statement ending with a semi-colon will be executed repeatedly. Therefore only ++j; comes within the loop.

Page 14: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

Loops 79

Output programs based on for() loop

Program 66. Give the output of the following program.

void main()

{char n[]="karishma";

int j;

for(j=8;j>=0;j--)

putchar(n[j]);

}

output

amhsirak

Program 67. Give the output of the following program.

void main() { int j=0,x=0; for(j=1;j<10;j++) { if(j%2 == 1) x+=j; else x--; printf("%d ",x); break; }

printf("x=%d\n",x);

}

output

1 x = 1

Program 68. Give the output of the following program.

void main() {int x=1,y=5,t; for(t=1;t<=5;t++) {x+=15; y*=2; printf("%d %d\n",x,y); if(x>50) break;} }

output

16b10 where b denotes a blank space.

31b20

46b40

61b80

76b160

Program 69 : Give the output of the following program.

main() { int x,y; for(x=1,y=5;x<6;x++,y+=2)

Page 15: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

80 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI)

printf("%d %d\n",x,y); }

output

1b5

2b7

3b9

4b11

5b13 where b denotes a blank space.

Program 70 : What does the following program print?

main()

{int k=0,t=0;

for(k=1;k<=10;k+=3)

{ if(k%2==1) t+=k;elses t--;

printf(“%3d”,t);}}

output

bb1bb0bb7bb6b19 where b denotes a blank space.

Nested for() loops

Program 71 : Give the output of the following program.

main() { int i, j, x=0; for(i=0;i<5;i++) {

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

x=x+(i+j-1);

printf("%d ",x);

break;}

printf("\nx=%d\n",x);

}

output

0

x=0

Program 72 : Give the output of the following program.

main() { int a,b,c;

for(a = 5; a < 10; a += 5)

{ for(b = 3; b < 6; b += 3)

c = a * b;

printf(“ %d %d %d \n“, a, b, c);

} }

output

5b6b15

Output programs based on do….while() loop

Page 16: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

Loops 81

Program 73 : Give the output of the following program.

main()

{ int x=0,s=0;

do

{x++;

printf("%d\n",x);

s+=x*x;}

while(x<5);

printf("%d\n",s);

}

output

1

2

3

4

5

55

QUESTIONS

1. Write a program in C language to input 10 integers and display a suitable message for each, whether they are divisible by 13 or not.

2. Write a program in C to input for 5 products their opening balance (o), the total daily sales(s), and purchases(p). Calculate and display closing balance(b) for each product, given that b = o + p – s.

3. Input the amount of loan taken by a firm and calculate and display the simple interest on the same for 10 years.

The rate of interest varies depending on the amount of the loan and is as follows.

Amount of Loan ` : Upto 1,00,000 100,001-5,00,000 5,00,001 and above Rate of interest p.a. : 5% 7% 7.5% 4. Write a program in C to input for 25 persons the Income tax no(numeric) and their

taxable income and calculate and display the Income tax using the following schedule.

First 1,00,000 NIL

next 1,50,000 10%

next 2,50,000 20%

excess 30%

5. Write a program in C to input the gross income for 12 persons. The rate of income tax is as per following schedule :

Gross Income Rate of Income Tax Less than 30,000 nil

Next 50,000 13% Next 50,000 17% Above 1,30,000 19%

Calculate and display income tax and net income with suitable message where, Net income = Gross income - Income tax. 6. Write a program in C to input for 25 persons their purchase amount and calculate

the discount which is 1% on purchases if the purchases are 5000 or less and 2% on

Page 17: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

82 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI)

the excess. Display the discount and net amount for each person and also print the total discount for all the 25 persons.

7. Write a program in C to input the employee number and the basic pay of the employees and calculate and display their total salary which is the sum of basic + da + hra, where da is 40% of the basic salary or ` 10000 whichever is less, hra is 13% of the basic or ` 3000 whichever is more.

8. Write programs in C to obtain and display the sum of the following series : i) 1+3+5+7+9+…………+99 ii) 2+8+14+……………..+602

iii) 1/2+2/3+3/4+………+99/100 iv) 2/5+4/9+…………….+202/405 v) 12 + 32 +52 +…………+292

vi) 1 × 2 × 3 + 3 × 4 × 5 + 5 × 6 × 7 + ………….+ 21 × 22 × 23

vii) 22 .5 + 32 .6 + 42 .7 + …….. + 322 .35 (Here the dot(.) denotes multiplication) viii) 2/32 + 4/52 + 6/72 + …….. + 20/212 ix) 1/122 + 3/142 + 5/162 + …….. + 19/302

x) 5 × 22 + 7 × 52 + 9 × 82 + …………+ 23 × 292

xi) 1 × 20 + 2 × 18 + 3 × 16 + ……….. + 9 × 4

x) 1/(2 × 3) + 2/(3 × 4) + 3/(4 × 5) +……………+ 10/(11 × 12)

xi) (1 × 2)/(3 × 4) + (3 × 4)/(5 × 6) + (5 × 6)/(7 × 8) +……+ (19 × 20)/(21 × 22)

9. Give the output of the following program : #include<stdio.h> void main() { int k=1,t=1; while(k<20) { if(k%2==0)t+=5; else t*=3; printf("%3d%3d\n",k,t);

if(t>25) break; k+=3;} }

10. Give the output of the following program : #include<stdio.h> void main() {int x=1,y=3; while(x<20) {y++; x+=++y; printf("%d %d\n",x,y);} } 11. Give the output of the following program :

#include<stdio.h> main() { int i=1, s=0; while(i<10) { if(i%2 == 0)

s += i; else printf(“\ns = %d”,s);

++i; }} 12. Give the output of the following program : #include<stdio.h> void main()

Page 18: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

Loops 83

{int p=4,q=10,r; for(r=1;r<5;r++) {p+=q;

q*=2; printf("%d %d\n",p,q);} }

13. Give the output of the following program : #include<stdio.h> void main() { int i=0,x=0; for(i=1;i<10;i++) { if(i%2==1) x=x+i; else x--; printf("%d ",x); break; } printf("x=%d\n",x); } 14. Give the output of the following program : #include<stdio.h> void main() { int a=2, s=0, j=1; for(;a<10;a+=3,j++)

{ s+= a*a*a; printf(“\na=%d j = %d”,a,j); } printf(“\nj = %d a = %d s = %d”,j,a,s);}

15. Rewrite the following C program using while() loop instead of for() loop. #include <stdio.h> void main()

{ int x,g=0; for(x=1;x<15;x+=3) {g=++x;

printf(“%d\n”,g);} } 16. Give the output of the following program :

#include<stdio.h> void main() {int i,j;

for(i=1,j=2;i<6;j+=3,i+=2) {j+=i; printf(“%4d”,j);}}

17. Give the output of the following program : #include<stdio.h> void main() { int i,x=0,s=5,y=3; for(i=1;i<2;i++) {s+=5;y+=s;x+=y; x%=y; printf("%d ",x);} printf("\ny=%d",y); } 18. Give the output of the following program :

#include<stdio.h> void main()

Page 19: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

84 Computer Systems & Applications (T. Y. B. Com. – Sem. – VI)

{ int x,y,z; for(x=10;x<15;x+=4)

for(y=2;y<5;y+=2) {z=x*y; printf("%d %d %d\n",x,y,z);}

} 19. Give the output of the following program : #include<stdio.h>

main() {int i, j, k=0; for(i=1; i<=5; i++)

{for(j=1; j<=4; ++j) {k= k + i – j; if(i>j) break;

printf(“%d”,k);}} printf(“\n k = %d”, k);} 20. Rewrite the following program using do/while loop.

#include<stdio.h> main() { int a=0,n;

for(n=10;n<22;n+=3) {a+=n; printf(“%d\n”,a);}

} 21. Give the output of the following program : #include <stdio.h>

void main() { int i = 1;

while (i < = 100) printf("%d \n",i+ +); }

22. Give the output of the following program : #include <stdio.h> void main()

{ int i = 6; do {

printf("%d \n",i); i = -2; }

while (i> 0); }

OBJECTIVE QUESTIONS

Select the appropriate option from the following : 1. Break statement takes the control out of the _______.

(a) program (b) loop (c) brackets (d) C language

Page 20: U n it II – 8 – Loops€¦ · 02/06/2020  · Loops 69 Program 39. Write a program to display “APPLE” one below the other 5 times using do-loop. #include main()

Loops 85

2. The function pow(x, y) is used to indicate _________.

(a) x/y (b) x*y (c) x%y (d) xy

3. The _______ loop checks the condition of the loop at the end of the loop.

(a) do…while (b) for( ) (c) while( ) (d) switch( ) 4. The ______ loop check the condition of the loop at the beginning of the loop. (a) while() and for() (b) do..while() and for ()

(c) while and do..while() (d) for() and switch() 5. In a for( ) loop _______ brackets are not required if it is followed by only one

statement. (a) Curly (b) round

(c) square (d) oblong 6. The continue statement takes the control of the program to the ____ of the loop. (a) end (b) middle

(c) out (d) start 7. The ___ loop may not be executed even once. (a) do..while() (b) many()

(c) for() (d) start()

ANSWERS

[(1 – b), (2 – d), (3 – a), (4 – a), (5 – a), (6 – d), (7 – c)]