diff options
Diffstat (limited to 'programs')
-rw-r--r-- | programs/add-2-nums-fns.c | 39 | ||||
-rw-r--r-- | programs/avg-fn.c | 39 | ||||
-rw-r--r-- | programs/leap-year-fn.c | 35 | ||||
-rw-r--r-- | programs/pre-post.c | 7 |
4 files changed, 120 insertions, 0 deletions
diff --git a/programs/add-2-nums-fns.c b/programs/add-2-nums-fns.c new file mode 100644 index 0000000..82a5bd9 --- /dev/null +++ b/programs/add-2-nums-fns.c @@ -0,0 +1,39 @@ +#include <stdio.h> + +int add(int a, int b) +{ + return a + b; +} + +int main(void) +{ + int a, b, sum; + printf("Enter first number: "); + scanf("%d", &a); + printf("Enter second number: "); + scanf("%d", &b); + sum = add(a, b); + printf("The sum of %d and %d is %d.\n", a, b, sum); + return 0; +} + +/* +Output: + +Set 1: +Enter first number: 10 +Enter second number: 20 +The sum of 10 and 20 is 30. + +Set 2: +Enter first number: 1 +Enter second number: 2 +The sum of 1 and 2 is 3. + +Set 3: +Enter first number: 33 +Enter second number: 44 +The sum of 33 and 44 is 77. + +*/ + diff --git a/programs/avg-fn.c b/programs/avg-fn.c new file mode 100644 index 0000000..da8ec19 --- /dev/null +++ b/programs/avg-fn.c @@ -0,0 +1,39 @@ +#include <stdio.h> + +float average(float a, float b, float c) +{ + return (a + b + c) / 3; +} + +int main(void) +{ + float a, b, c; + printf("Average of three numbers\n\n"); + printf("Enter three numbers: "); + scanf("%f%f%f", &a, &b, &c); + printf("The average is %f\n", average(a, b, c)); + return 0; +} + +/* +Output: +Set 1: +Average of three numbers + +Enter three numbers: 1 2 3 +The average is 2.000000 + +Set 2: +Average of three numbers + +Enter three numbers: 1.5 2.3 7.9 +The average is 3.900000 + +Set 3: +Average of three numbers + +Enter three numbers: 123 45.6 7.89 +The average is 58.829999 + +*/ + diff --git a/programs/leap-year-fn.c b/programs/leap-year-fn.c new file mode 100644 index 0000000..43842a9 --- /dev/null +++ b/programs/leap-year-fn.c @@ -0,0 +1,35 @@ +#include <stdio.h> +#include <stdbool.h> + +bool is_leap_year(int y) +{ + return y % 400 == 0 || (y % 100 != 0 && y % 4 == 0); +} + +int main(void) +{ + int year; + printf("To check whether a given year is leap year or not\n\n"); + printf("Enter a year: "); + scanf("%d", &year); + if (year <= 1200 || year >= 9999) { + printf("Invalid year.\n"); + return 0; + } + printf("%d is%s a leap year.\n", year, is_leap_year(year) ? "" : " not"); + return 0; +} + +/* +Output: +Set 1: + + +Set 2: + +Set 3: + +Set 4: + +*/ + diff --git a/programs/pre-post.c b/programs/pre-post.c new file mode 100644 index 0000000..7eab3a2 --- /dev/null +++ b/programs/pre-post.c @@ -0,0 +1,7 @@ +#include <stdio.h> + +int main(int argc, char **argv) +{ + printf("%d%d%d", argc++, argc, ++argc); +} + |