Beruflich Dokumente
Kultur Dokumente
(void)
x = 1;
y,v;
z = 1;
scanf(%d, &x);
scanf(%d, &y);
if (y == 0)
{ x = 1; }
v = x;
while (z < y)
{
x*=v;
z++;
}
printf(The mystery value is %d, x);
}
Answer: The code prints out xy, where x is the base and y is the exponent.
3. Consider this code fragment. How many times does the printf statement get
executed?
int count = 5;
while (count > 0 && count <= 5)
{
count--;
if (count / 2 > 1)
printf(Printing\n);
else
count--;
}
Answer: 1 time
else if (count == 9)
{
printf(Hello);
count++;
}
}
Answer: Using a while loop,
int main (void)
{
int count = 5;
while (count <= 8)
{
if (count == 8)
printf(Hello);
count++;
}
}
5. Create this image by using while loop and a do-while loop.
1
11
111
Answer:
Using a while loop
int main(void)
{
int i = 1, j = 0;
int main(void)
{
int i = 1, j = 0;
while (i<=3) {
j = 0;
while ( j<i)
{
printf("1");
j++;
}
printf("\n");
i++;
}
do {
j=0;
do {
printf("1");
j++;
} while ( j<i);
printf("\n");
i++;
} while (i <= 3);
6. Write a small routine that prints the sum of a list of integer values separated by a
space from a text file, data.txt. Also, include a flowchart for this routine.
Answer:
FILE * inFile;
inFile = fopen(data.txt, r);
int sum=0;
if (inFile != NULL)
{
while(fscanf(%d, &x)==1)
sum += x;
printf(Sum=%d \n, sum);
}
else
printf(File is not found!\n);
fclose(inFile);
system("PAUSE");
return 0;
}
Answer: 2.000