0% found this document useful (0 votes)
2 views22 pages

C How To Program 14

Uploaded by

alina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views22 pages

C How To Program 14

Uploaded by

alina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

C how to program

MENTOR ALEENA RUBAB


Review exercise

Lets explore problems 36……..


3.36 (How Fast Is Your Computer?) How can you
determine how fast your own computer operates? Write
a program with a while loop that counts from 1 to
1,000,000,000, incrementing by 1 during each iteration
of the loop. Every time the count reaches a multiple of
100,000,000, print that number on the screen. Use your
watch to time how long each 100 million iterations of
the loop takes. [Hint: Use the remainder operator to
recognize each time the counter reaches a multiple of
100,000,000.
#include <stdio.h>
#include <time.h>

int main() {
// Start the clock
clock_t start_time, end_time;
double time_taken;

start_time = clock();
for (long long i = 1; i <= 1000000000; i++) {
// Check if i is a multiple of 100,000,000
if (i % 100000000 == 0) {
printf("%lld\n", i);

// Measure the time taken


end_time = clock();
time_taken = ((double) (end_time - start_time)) /
CLOCKS_PER_SEC;
printf("Time taken for 100,000,000 iterations: %f seconds\n",
time_taken);

// Reset the start time for the next 100,000,000 iterations


start_time = clock();
}
}

return 0;
}
Usage:
Run the program and use a watch or timer to
verify the time printed by the program for each
100 million iterations.
This will give you an idea of how fast your
computer can count to 1,000,000,000 in
segments of 100 million iterations.
3.37 (Detecting Multiples of 10) Write
a program that prints 100 asterisks,
one at a time. After every tenth
asterisk, print a newline character.
[Hint: Count from 1 to 100. Use the
% operator to recognize each time
the counter reaches a multiple of
10.]
#include <stdio.h>

int main() {
for (int i = 1; i <= 100; i++) {
// Print an asterisk
printf("*");

// Check if the counter is a multiple of 10


if (i % 10 == 0) {
// Print a newline character
printf("\n");
}
}

return 0;
}
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
3.38 (Counting 7s) Write a
program that reads an integer (5
digits or fewer) and determines
and prints how many digits in the
integer are 7s.
#include <stdio.h>

int main() {
int number, temp, digit;
int count = 0;

// Prompt the user to enter an integer (5 digits or fewer)


printf("Enter an integer (5 digits or fewer): ");
scanf("%d", &number);
// Check if the number is 5 digits or fewer
if (number > 99999 || number < -99999) {
printf("The number entered is more than 5 digits.\n");
return 1;
}

temp = number;

// Count the number of 7s in the integer


while (temp != 0) {
digit = temp % 10;
if (digit == 7 || digit == -7) {
count++;
}
temp /= 10;
}

// Print the result


printf("The number %d contains %d occurrence(s) of the digit 7.\n",
number, count);

return 0;
}
Output

The number 77237 contains 3 occurrence(s) of the digit 7.


Output

The number 12345 contains 0 occurrence(s) of the digit 7.


3.39 (Checkerboard Pattern of
Asterisks) Write a program
that displays the following
checkerboard pattern:
*********
*********
*********
*********
*********
*********
*********
#include <stdio.h>

int main() {
// Define the size of the checkerboard
int rows = 8;
int cols = 8;

// Loop through each row


for (int i = 0; i < rows; i++) {
// Loop through each column
for (int j = 0; j < cols; j++) {
// Print an asterisk or space depending on the position
if ((i + j) % 2 == 0) {
printf("*");
} else {
printf(" ");
}
}
// Print a newline character after each row
printf("\n");
}

return 0;
}
3.40 (Multiples of 2 with an Infinite Loop)
Write a program that keeps printing the
multiples of the integer 2, namely 2, 4, 8,
16, 32, 64, and so on. Your loop should
not terminate (i.e., you should create an
infinite loop). What happens when you
run this program?
#include <stdio.h>

int main() {
unsigned long long num = 2; // Start with the first multiple of 2

// Infinite loop
while (1) {
printf("%llu\n", num);
num *= 2; // Multiply by 2 to get the next multiple
}

return 0; // This line will never be reached


}
output

 2
 4
 8
 16
 32
 64
 128
 256
 512
 1024
 ...
The End

You might also like