Chapter 5 – Appendix
5/18/2008
Page 1 of 6
1) Write a program that produces this output: 0 1 2 3 4 5 6
1 2 4 8 16 32 64
Or double
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 i,product; i=0; product=1;
int main(void) { int i, product=1; for(i=0; i<=6; ++i) { printf("%d%3d\n",i,product); product*=2; }
while(i<=6) { printf("%d %3d\n",i,product); i=i+1; product *=2; } return (0); }
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 ev, sum=0, count=0;
int main(void) { int ev,sum,count; ev=2; sum=0; count=0;
int main(void) { int ev,sum,count; ev=2; sum=0; count=0;
for(ev=2; ev<=100; ev += 2) { sum += ev; count++; } printf("%d numbers were added.\n",count); printf("Sum of the even numbers is %d\n", sum);
do {
while(ev<=100) { sum = sum + ev; ++count; ev = ev+2; }
sum += ev; ++count; ev = ev+2; } while(ev<=100);
printf("%d numbers were added.\n",count);
printf("%d numbers were added.\n",count);
printf("Sum of the even numbers is %d\n", sum);
printf("Sum of the even numbers is %d\n", sum);
return(0); }
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 counter, total, grade; double average; total=0;
int main(void) { int counter, total, grade; double average; total=0; counter=1;
int main(void) { int counter, total, grade; double average; total=0; counter=1;
while(counter <= 10) { printf("Enter grade %2d: ", counter); scanf("%d",&grade); total += grade; counter++; }
do { printf("Enter grade %2d: ", counter); scanf("%d",&grade); total += grade; counter++; } while(counter <= 10);
average = (double) total/10; printf("Class average is %.2f\n", average);
average = (double) total/10; printf("Class average is %.2f\n", average);
for(counter=1; counter<=10; counter += 1) { printf("Enter grade %2d: ", counter); scanf("%d",&grade); total += grade; } average = (double) total/10; printf("Class average is %.2f\n", average); return(0); }
return(0); }
Output:
return(0); }
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 #include<stdio.h> int main(void) { int counter, total, grade; double average; total=0; counter=0;
Using do-while #include<stdio.h> int main(void) { int total; // sum of grades int counter; // number of grades entered int grade; // one grade double average;
// don't know number of grades printf("Enter grade, -1 to end: "); scanf("%d",&grade);
/* initialization */ total=0; counter=0;
//stop the loop if grade=-1 while(grade != -1) { total += grade; counter++; printf("Enter grade, -1 to end: "); scanf("%d",&grade); }
// don'n know number of grades do { printf("Enter grade, -1 to end: "); scanf("%d",&grade); if(grade == -1) break; total += grade; counter++; } while(grade != -1); //stop the loop if grade=-1
if(counter != 0 ) { average = (double) total/counter; printf("counter = %d\n",counter); printf("Class average is %.2f\n", average); } else printf("No grades were entered\n");
if(counter != 0) { average = (double) total/counter; printf("counter = %d\n",counter); printf("Class average is %.2f\n", average); } 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
Outer loop
1 2 3 4 5 6
* ** *** **** ***** ******
Solution: Using for (nested loop)
Using for and while (nested loop)
Using while (nested loop)
Using do-while (nested loop)
#include<stdio.h>
#include<stdio.h>
#include<stdio.h>
#include<stdio.h>
int main(void) { int i; int j;
int main(void) { int i; int j;
int main(void) { int i; int j;
int main(void) { int i; int j;
for(i=1; i<=6; i++) { for(j=1; j<=i; j++) printf("*"); printf("\n"); }
for(i=1; i<=6; i++) { j=1; while(j<=i) { printf("*"); j++; } printf("\n"); }
return(0); }
i=1; do { j=1; do { printf("*"); j++; } while(j<=i); printf("\n"); i++; } while(i<=6);
i=1; while(i<=6) { j=1; while(j<=i) { printf("*"); j++; } printf("\n"); i++; }
return(0); }
return(0);
return(0); }
Output:
}
Chapter 5 – Appendix
5/18/2008
6) Write nests of loops that cause the following output to be displayed: 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 3 2 3 4 2 3 2
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:
Page 6 of 6