A silly program which prints
first 1000 multiples of 13
Note the same 4 lines are repeated 13000 times.
This means these lines could be replaced by a while loop
which executes 13000 times.
#include <stdio.h>
int main(void) {
int i;
i = 1;
if (i % 13 == 0) {
printf("%d\n", i);
}
i = i + 1;
if (i % 13 == 0) {
printf("%d\n", i);
}
i = i + 1;
if (i % 13 == 0) {
printf("%d\n", i);
}
i = i + 1;
if (i % 13 == 0) {
printf("%d\n", i);
}
i = i + 1;
if (i % 13 == 0) {
printf("%d\n", i);
}
i = i + 1;
if (i % 13 == 0) {
printf("%d\n", i);
}
i = i + 1;
if (i % 13 == 0) {
printf("%d\n", i);
}
...
A simple program demonstrating the use of a while loop
#include <stdio.h>
int main(void) {
int n, upperBound, sum;
sum = 0;
upperBound = 10;
n = 1;
while (n <= upperBound) {
sum = sum + n;
n = n + 1;
}
printf("Sum of integers 1..%d is %d\n", upperBound, sum);
return 0;
}
Read 43 numbers and then print the sum of the numbers
A simple program demonstrating
while & scanf
Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
#define N_NUMBERS 42
#include <stdio.h>
int main(void) {
int x, sum, n;
sum = 0;
printf("Enter %d numbers:\n", N_NUMBERS);
n = 0;
while (n < N_NUMBERS) {
scanf("%d", &x);
sum = sum + x;
n = n + 1;
}
printf("Sum of the numbers is %d\n", sum);
return 0;
}
Read n numbers and then print the sum of the numbers
A simple program demonstrating
while & scanf
Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
#include <stdio.h>
int main(void) {
int x, sum, n, nNumbers;
sum = 0;
printf("How many numbers do you wish to sum: ");
scanf("%d", &nNumbers);
printf("Enter %d numbers:\n", nNumbers);
n = 0;
while (n < nNumbers) {
scanf("%d", &x);
sum = sum + x;
n = n + 1;
}
printf("Sum of the numbers is %d\n", sum);
return 0;
}
Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
#include <stdio.h>
#define N_NUMBERS 5
int main(void) {
int howManyNumbers, number, maximum;
scanf("%d", &maximum);
howManyNumbers = 1;
while (howManyNumbers < N_NUMBERS) {
scanf("%d", &number);
if (number > maximum) {
maximum = number;
}
howManyNumbers = howManyNumbers + 1;
}
printf("Largest is %d\n", maximum);
return 0;
}
Read numbers until a negative number is read
then print the sum of the numbers (not including the negative number)
Version 1
A simple program demonstrating
stopping a while loop
when a particular value is read by scanf
Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
#include <stdio.h>
int main(void) {
int x, sum;
sum = 0;
x = 0;
printf("Enter numbers, terminate with a negative number:\n");
while (x >= 0) {
scanf("%d", &x);
if (x > 0) {
sum = sum + x;
}
}
printf("Sum of the numbers is %d\n", sum);
return 0;
}
Read numbers until a negative number is read
then print the sum of the numbers (not including the negative number)
Version 2
A simple program demonstrating
stopping a while loop
when a particular value is read by scanf
Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
#include <stdio.h>
int main(void) {
int x, sum;
sum = 0;
printf("Enter numbers, terminate with a negative number:\n");
scanf("%d", &x);
while (x >= 0) {
sum = sum + x;
scanf("%d", &x);
}
printf("Sum of the numbers is %d\n", sum);
return 0;
}
Read numbers printing whether they are even or odd
illustrates use of a sentinel variable (stop_loop)
Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
#include <stdio.h>
int main(void) {
int stop_loop, number;
printf("Enter numbers, 0 to stop\n");
stop_loop = 0;
while (stop_loop != 1) {
scanf("%d", &number);
if (number == 0) {
stop_loop = 1;
} else if (number % 2 == 1) {
printf("%d is odd.\n", number);
} else {
printf("%d is even.\n", number);
}
}
return 0;
}
A simple program which reads integers and
and if a composite number is read exists
printing the factor
Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
#include <stdio.h>
int main(void) {
int n, possibleFactor, keepLooping;
keepLooping = 1;
while (keepLooping == 1) {
printf("Enter a number: ");
scanf("%d", &n);
possibleFactor = 2;
while (possibleFactor < n && keepLooping == 1) {
if (n % possibleFactor == 0) {
printf("%d is composite %d is a factor\n", n, possibleFactor);
keepLooping = 0;
}
possibleFactor = possibleFactor + 1;
}
}
return 0;
}
A simple program which reads integers and prints snap
and exits if the same number is read twice in a row
Note for simplicity we are assuming scanf succeeds in reading an integer.
A robust program would check that scanf returns 1 to indicate an integer read.
#include <stdio.h>
int main(void) {
int n, previousN;
printf("Enter a number: ");
scanf("%d", &previousN);
printf("Enter a number: ");
scanf("%d", &n);
while (n != previousN) {
previousN = n;
printf("Enter a number: ");
scanf("%d", &n);
}
printf("Snap!\n");
return 0;
}
Read numbers until end of input (or a non-number) is reached
then print the sum of the numbers
Version 1
A simple program demonstrating
stopping a while loop
when scanf fails to read a number e.g. because end-of-input is reached
#include <stdio.h>
int main(void) {
int sum, x, numbersRead;
sum = 0;
numbersRead = 1;
printf("Enter numbers, indicate end-of-input with control-D:\n");
while (numbersRead == 1) {
// if scanf can read an integer it will place it in x and it will return 1
// if scanf can't read an integer it will not change x and it will return 0 or -1
numbersRead = scanf("%d", &x);
if (numbersRead == 1) {
sum = sum + x;
}
}
printf("Sum of the numbers is %d\n", sum);
return 0;
}
Read numbers until end of input (or a non-number) is reached
then print the sum of the numbers
Version 1
A simple program demonstrating
stopping a while loop
when scanf fails to read a number e.g. because end-of-input is reached
#include <stdio.h>
int main(void) {
int sum, x, numbersRead;
sum = 0;
numbersRead = 1;
printf("Enter numbers, indicate end-of-input with control-D:\n");
// if scanf can read an integer it will place it in x and it will return 1
// if scanf can't read an integer it will not change x and it will return 0 or -1
numbersRead = scanf("%d", &x);
while (numbersRead == 1) {
sum = sum + x;
numbersRead = scanf("%d", &x);
}
printf("Sum of the numbers is %d\n", sum);
return 0;
}
Read numbers until end of input (or a non-number) is reached
then print the sum of the numbers
Version 2
A simple program demonstrating
stopping a while loop
when scanf fails to read a number e.g. because end-of-input is reached
#include <stdio.h>
int main(void) {
int sum, x;
sum = 0;
printf("Enter number: ");
while (scanf("%d", &x) == 1) {
sum = sum + x;
printf("Enter number: ");
}
printf("Sum of the numbers is %d\n", sum);
return 0;
}
#include <stdio.h>
#define N 100
int main(void) {
int x, y, z;
x = 1;
while (x < N) {
y = 1;
while (y <= N) {
z = 1;
while (z <= N) {
if (x * x + y * y == z * z) {
printf("%d^2 + %d^2 = %d^2\n", x, y, z);
}
z = z + 1;
}
y = y + 1;
}
x = x + 1;
}
return 0;
}
A silly program which prints first 1000 multiples of 13