C How To Program 14
C How To Program 14
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);
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("*");
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;
temp = number;
return 0;
}
Output
int main() {
// Define the size of the checkerboard
int rows = 8;
int cols = 8;
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
}
2
4
8
16
32
64
128
256
512
1024
...
The End