Skip to content

Commit 2c6a75f

Browse files
committed
Add Day 7: Jump statements
1 parent f69cf87 commit 2c6a75f

File tree

7 files changed

+236
-5
lines changed

7 files changed

+236
-5
lines changed

Day 6/nestedLoops.c

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Program to print multiplication table
3-
* of numbers from 1 to 10
3+
* of numbers from 1 to 5 (generalized to n)
44
* using nested loops.
55
*/
66

@@ -9,10 +9,12 @@
99
int main () {
1010

1111
/*
12-
1 2 3 4 5 6 7 8 9 10
13-
2 4 6 8 10 12 14 16 18 20
14-
3 6 9 12 15 18 21 24 27 30
15-
4 ...
12+
1 2 3 4 5
13+
2 4 6 8 10
14+
3 6 9 12 15
15+
4 8 12 16 20
16+
..
17+
10 20 30 40 50
1618
*/
1719

1820
// what are we looping on?
@@ -24,5 +26,35 @@ int main () {
2426
// outer loop: we loop on the i-th multiple (i=1 to 10)
2527
// inner loop: all numbers
2628

29+
/*
30+
pseudo code:
31+
32+
// outer loop
33+
for (multiple = 1 to multiple = 10) {
34+
// inner loop
35+
for (number = 1 to number = 5) {
36+
print(number*multiple)
37+
}
38+
}
39+
40+
*/
41+
42+
int n;
43+
scanf("%d", &n);
44+
45+
// outer loop takes care of all the multiples from 1 to 10
46+
for (int multiple = 1; multiple <= 10; multiple ++) {
47+
48+
// inner loop takes care of all numbers from 1 to n
49+
// %4d in the printf means that for the integer which
50+
// is to be replaced by %d, the amount of space
51+
// is fixed, i.e. 4 characters
52+
for (int number = 1; number <= n; number ++) {
53+
printf("%4d", number * multiple);
54+
}
55+
56+
printf("\n");
57+
}
58+
2759
return 0;
2860
}

Day 7/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## Break, continue and goto
2+
1. [Example program to understand break](break.c)
3+
2. [Example program to understand continue](continue.c)
4+
3. [Example program to understand goto](goto.c)
5+
6+
## Usage of break
7+
1. [Check whether given number is prime](prime.c)
8+
9+
## Creating your own functions
10+
1. [Sum of first n prime numbers](sumOfNPrimes.c)

Day 7/break.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Program to understand usage of break statement
3+
* inside loops.
4+
*/
5+
6+
#include <stdio.h>
7+
8+
int main () {
9+
10+
// break during second iteration of the loop
11+
/*
12+
for (int i = 0; i < 10; i ++) {
13+
if (i == 1) {
14+
break;
15+
}
16+
printf("%d ", i);
17+
}
18+
*/
19+
20+
for (int multiple = 1; multiple <= 10; multiple ++) {
21+
for (int number = 12; number <= 16; number ++) {
22+
if (number == 15) {
23+
break;
24+
}
25+
printf("%4d", number * multiple);
26+
}
27+
28+
printf("\n");
29+
}
30+
31+
// break just inside if is useless
32+
/*
33+
int n = 5;
34+
if (n == 6) {
35+
break;
36+
}
37+
*/
38+
39+
return 0;
40+
}

Day 7/continue.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Program to understand usage of continue statement
3+
* inside loops.
4+
*/
5+
6+
#include <stdio.h>
7+
8+
int main () {
9+
10+
// sum of even numbers from 1 to n
11+
12+
int n;
13+
scanf("%d", &n);
14+
15+
int sum = 0;
16+
for (int number = 1; number <= n; number ++) {
17+
// number is odd
18+
if (number % 2 == 1) {
19+
continue;
20+
// if the above line is executed,
21+
// rest of the loop is skipped
22+
// and we move to next iteration
23+
}
24+
sum += number;
25+
}
26+
27+
printf("%d\n", sum);
28+
return 0;
29+
}

Day 7/goto.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Program to understand usage of goto
3+
*/
4+
5+
#include <stdio.h>
6+
7+
int main () {
8+
9+
for (int i = 0; i < 10; i ++) {
10+
for (int j = 0; j < 10; j ++) {
11+
for (int k = 0; k < 10; k ++) {
12+
for (int l = 0; l < 10; l ++) {
13+
14+
// requirement:
15+
// you want to terminate all the loops
16+
// when i = 5, j = 5, k = 5, l = 5
17+
18+
if (i == 5 && j == 5 && k == 5 && l == 5) {
19+
// break; this will just terminate the inner loop
20+
goto end;
21+
}
22+
23+
}
24+
}
25+
}
26+
}
27+
28+
end:
29+
return 0;
30+
}

Day 7/prime.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Program to check whether number input from the user
3+
* is a prime number or not!
4+
*/
5+
6+
#include <stdio.h>
7+
8+
int main () {
9+
10+
int number;
11+
scanf("%d", &number);
12+
13+
// we need to check whether the number is a prime or not
14+
// prime no. is the one which has only 1 and the number
15+
// itself as its divisors
16+
17+
// ex: n = 2, 3, 5, 7, 11, 13, 17, ...
18+
19+
// there are infinitely many primes!!!
20+
21+
// Shubham's idea:
22+
// we can start a loop of 'divisor' from 2 to n/2,
23+
// if the number is divisbile by divisor => no. is not prime
24+
// why do we check only till n/2, because
25+
// a number n cannot be divided by number > n/2 except itself
26+
27+
int count = 0;
28+
for (int divisor = 2; divisor <= number/2; divisor ++) {
29+
if (number % divisor == 0) {
30+
count ++;
31+
printf("Number is not prime\n");
32+
break;
33+
}
34+
}
35+
36+
if (count == 0) {
37+
printf("Number is prime.\n");
38+
}
39+
40+
return 0;
41+
}

Day 7/sumOfNPrimes.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Program to find sum of first n primes
3+
*/
4+
5+
#include <stdio.h>
6+
7+
// write our own functions
8+
// our first very own function to check whether a number is prime
9+
// syntax:
10+
/* functionReturnType functionName(paramterType paramterName, ...) {
11+
function body;
12+
}
13+
*/
14+
15+
int isPrime(int number) {
16+
for (int divisor = 2; divisor <= number/2; divisor ++) {
17+
if (number % divisor == 0) {
18+
return 0;
19+
}
20+
}
21+
return 1;
22+
}
23+
24+
int main () {
25+
26+
int n;
27+
scanf("%d", &n);
28+
29+
int result = 0; // to store the sum of the primes
30+
int currentNumber = 2; // current number being checked for prime
31+
int countedPrimes = 0; // no of primes already counted
32+
while (countedPrimes < n) {
33+
// if current number is prime
34+
// add the current no. to result if it's a prime
35+
// increase the current no.
36+
37+
// how do we call a function?
38+
// functionName(function Parameters)
39+
if (isPrime(currentNumber) == 1) {
40+
result += currentNumber;
41+
countedPrimes ++;
42+
}
43+
44+
currentNumber ++;
45+
}
46+
47+
printf("%d\n", result);
48+
return 0;
49+
}

0 commit comments

Comments
 (0)