Вы находитесь на странице: 1из 6

Chapter 5 – Appendix 5/18/2008 Page 1 of 6

1) Write a program that produces this output:


0 1
1 2
2 4
3 8
4 16
5 32 Or double
6 64

Note:
The prototype of pow() function is: double pow(double, int) or float pow(float, int)

Solution 1: Using pow() function.


Using for
#include<stdio.h>
#include<math.h>
int main(void)
{
int i;
double x=2.0,y;
for(i=0; i<7; i++)
{
//printf("%d %3.0f\n",i,pow(x,i));
y = pow(x,i);
printf("%d %3.0f\n",i,y);
}
return(0);
}

Solution 2: without using the function pow().


Using while Using for
#include<stdio.h> #include<stdio.h>
int main(void) int main(void)
{ {
int i,product; int i, product=1;
i=0;
product=1; for(i=0; i<=6; ++i)
{
while(i<=6) printf("%d%3d\n",i,product);
{ product*=2;
printf("%d %3d\n",i,product); }
i=i+1; return(0);
product *=2; }
}
return (0);
}
Chapter 5 – Appendix 5/18/2008 Page 2 of 6
2) The program uses loop structure to sum and count all even integers from 2 to 100.

Solution:
Using for Using while Using do-while
#include<stdio.h> #include<stdio.h> #include<stdio.h>
int main(void) int main(void) int main(void)
{ { {
int ev, sum=0, count=0; int ev,sum,count; int ev,sum,count;
ev=2; ev=2;
for(ev=2; ev<=100; ev += 2) sum=0; sum=0;
{ count=0; count=0;
sum += ev;
count++; while(ev<=100) do
} { {
sum = sum + ev; sum += ev;
printf("%d numbers were ++count; ++count;
added.\n",count); ev = ev+2; ev = ev+2;
} } while(ev<=100);
printf("Sum of the even
numbers is %d\n", sum); printf("%d numbers were printf("%d numbers were
added.\n",count); added.\n",count);
return(0);
} printf("Sum of the even printf("Sum of the even
numbers is %d\n", sum); numbers is %d\n", sum);
return(0); return(0);
} }

Output:
Chapter 5 – Appendix 5/18/2008 Page 3 of 6
3) Write a program that calculate grade average for 10 students ?

Note: average = (double) total/10.0;


- if total is declated as int, you need to use double total to convert the value to real.
- Another solution is to declare total as double.

Solution:
Using for Using while Using do-while
#include<stdio.h> #include<stdio.h> #include<stdio.h>

int main(void) int main(void) int main(void)


{ { {
int counter, total, grade; int counter, total, grade; int counter, total, grade;
double average; double average; double average;
total=0; total=0; total=0;
counter=1; counter=1;
for(counter=1; counter<=10;
counter += 1) while(counter <= 10) do
{ { {
printf("Enter grade %2d: ", printf("Enter grade %2d: ", printf("Enter grade %2d: ",
counter); counter); counter);
scanf("%d",&grade); scanf("%d",&grade); scanf("%d",&grade);
total += grade; total += grade; total += grade;
} counter++; counter++;
} } while(counter <= 10);
average = (double) total/10;
printf("Class average is average = (double) total/10; average = (double) total/10;
%.2f\n", average); printf("Class average is printf("Class average is
%.2f\n", average); %.2f\n", average);
return(0);
} return(0); return(0);
} }

Output:
Chapter 5 – Appendix 5/18/2008 Page 4 of 6
4) Write a program that calculate grade average until entering -1 to exit the loop ?

Note 1: Do not use for if you use scanf to update the loop control variable.
Note 2: If you use the loop as a counter, it will be much better to use for and not while.

Solution:
Using while Using do-while
#include<stdio.h> #include<stdio.h>
int main(void) int main(void)
{ {
int counter, total, grade; int total; // sum of grades
double average; int counter; // number of grades entered
total=0; int grade; // one grade
counter=0; double average;

// don't know number of grades /* initialization */


printf("Enter grade, -1 to end: "); total=0;
scanf("%d",&grade); counter=0;

//stop the loop if grade=-1 // don'n know number of grades


while(grade != -1) do
{ {
total += grade; printf("Enter grade, -1 to end: ");
counter++; scanf("%d",&grade);
printf("Enter grade, -1 to end: "); if(grade == -1) break;
scanf("%d",&grade); total += grade;
} counter++;
} while(grade != -1); //stop the loop if grade=-1
if(counter != 0 )
{ if(counter != 0)
average = (double) total/counter; {
printf("counter = %d\n",counter); average = (double) total/counter;
printf("Class average is %.2f\n", average); printf("counter = %d\n",counter);
} printf("Class average is %.2f\n", average);
else }
printf("No grades were entered\n"); else
printf("No grades were entered\n");
return(0);
} return(0);
}

Output:
Chapter 5 – Appendix 5/18/2008 Page 5 of 6
5) Write nests of loops that cause the following output to be displayed:
Inner loop
123456
1 *
**
Outer loop

2
3 ***
****
4 *****
5 ******
6
Solution:
Using for Using for and while Using while Using do-while
(nested loop) (nested loop) (nested loop) (nested loop)
#include<stdio.h> #include<stdio.h> #include<stdio.h> #include<stdio.h>

int main(void) int main(void) int main(void) int main(void)


{ { { {
int i; int i; int i; int i;
int j; int j; int j; int j;
for(i=1; i<=6; i++) for(i=1; i<=6; i++) i=1; i=1;
{ { while(i<=6) do
for(j=1; j<=i; j++) j=1; { {
printf("*"); while(j<=i) j=1; j=1;
printf("\n"); { while(j<=i) do
} printf("*"); { {
j++; printf("*"); printf("*");
return(0); } j++; j++;
} printf("\n"); } } while(j<=i);
} printf("\n"); printf("\n");
i++; i++;
return(0); } } while(i<=6);
}
return(0); return(0);
} }

Output:
Chapter 5 – Appendix 5/18/2008 Page 6 of 6
6) Write nests of loops that cause the following output to be displayed:

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

Solution:
#include<stdio.h>
int main(void)
{
int outer,inner,k,m=0;

for(outer=0;outer<=8;outer++)
{
if(outer<=4)
k=outer;
else
{
m+=2;
k=outer-m;
}

for(inner=0;inner<=k;inner++)
printf("%2d",inner);
printf("\n");
}
return(0);
}

Output:

Вам также может понравиться