c8
This commit is contained in:
parent
cd1d50183b
commit
0fe9a5ef19
14
C8/ex2.c
Normal file
14
C8/ex2.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int input;
|
||||
|
||||
printf("Enter an integer: ");
|
||||
scanf("%d", &input);
|
||||
|
||||
if (input >= 0 && input <= 10) {
|
||||
printf("The number is in range\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
14
C8/ex3.c
Normal file
14
C8/ex3.c
Normal file
@ -0,0 +1,14 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int input;
|
||||
|
||||
printf("Enter an integer: ");
|
||||
scanf("%d", &input);
|
||||
|
||||
if (!(input >= 0 && input <= 10)) {
|
||||
printf("The number is not in range\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
21
C8/ex4.c
Normal file
21
C8/ex4.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int age;
|
||||
|
||||
printf("Enter your age: ");
|
||||
scanf("%d", &age);
|
||||
|
||||
// c only matches one branch so don't need to specify lower limits of each age range
|
||||
if ( age <= 0 ) {
|
||||
printf("Still a baby\n");
|
||||
} else if ( age <= 12 ) {
|
||||
printf("The junior years\n");
|
||||
} else if ( age < 20 ) {
|
||||
printf("Teenage years\n");
|
||||
} else {
|
||||
printf("Downhill all the way now!\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
25
C8/ex7.c
Normal file
25
C8/ex7.c
Normal file
@ -0,0 +1,25 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main() {
|
||||
int rc; // used to check return codes of scanf
|
||||
float radius, height;
|
||||
|
||||
printf("Enter radius: ");
|
||||
rc = scanf("%f", &radius);
|
||||
// scanf returns number of scanned items
|
||||
if (rc != 1) {
|
||||
printf("Please enter an positive real integer or decimal\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Enter height: ");
|
||||
rc = scanf("%f", &height);
|
||||
// scanf returns number of scanned items
|
||||
if (rc != 1) {
|
||||
printf("Please enter an positive real integer or decimal\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Surface area: %f\n", 2*M_PI*radius*radius + 2*M_PI*radius*height);
|
||||
}
|
@ -6,6 +6,11 @@ int main(void)
|
||||
// Declare some variables
|
||||
int a = 7, b=6;
|
||||
|
||||
printf("Enter a value for a: ");
|
||||
scanf("%d", &a);
|
||||
printf("Enter a value for b: ");
|
||||
scanf("%d", &b);
|
||||
|
||||
// A single line of code conditional on the value of a
|
||||
if ( a == 7 )
|
||||
printf ("The value of a is 7 - so I will do this\n");
|
||||
|
Reference in New Issue
Block a user