Commit 92048566 authored by Alija Sabic's avatar Alija Sabic
Browse files

Add exercises of 3rd unit

parent 745e92c2
/*
============================================================================
Name : 03_area.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Calculate area of rectangle.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
// function prototype a.k.a. declaration
double calculateArea(double a, double b);
int main(void) {
double x = 10;
double y = 12.5;
// function call
double area = calculateArea(x, y);
printf("Die Fläche beträgt: %.2f m²\n", area);
return EXIT_SUCCESS;
}
// function implementation a.k.a. definition
double calculateArea(double a, double b) {
return a * b;
}
/*
============================================================================
Name : 03_even_numbers.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Print even number from MIN to MAX.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define MIN 0
#define MAX 30
int main(void) {
for (int i = MIN; i <= MAX/2; i++) printf("%d ",i*2);
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 03_gcd.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Calculate greatest common divisor.
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int a = 12;
int b = 44;
int result = 0;
int copyA = a;
int copyB = b;
if ( a == 0) {
result = b;
}
else {
while (b != 0) {
if ( a > b ) {
a = a - b;
}
else {
b = b - a;
}
}
result = a;
}
printf("ggT(%d,%d) = %d\n", copyA, copyB, result);
return EXIT_SUCCESS;
}
/*
============================================================================
Name : 03_gcd_function.c
Author : Sabic Alija <sabic@technikum-wien.at>
Version : 0.1
Copyright : Free for all.
Description : Calculate greatest common divisor (function).
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
int gcd(int a, int b);
int gcd2(int a, int b);
int main(void) {
int a = 12;
int b = 44;
printf("ggT(%d,%d) = %d\n", a, b, gcd2(a, b));
return EXIT_SUCCESS;
}
int gcd(int a, int b) {
int result = 0;
if ( a == 0) {
result = b;
}
else {
while (b != 0) {
if ( a > b ) {
a = a - b;
}
else {
b = b - a;
}
}
result = a;
}
return result;
}
int gcd2(int a, int b) {
int result = 0;
if ( a == 0 ) return abs(b);
if ( b == 0 ) return abs(a);
do
{
result = a % b;
a = b;
b = result;
}
while(b != 0);
return abs(a);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment