Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions add-complex-numbers2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//return struct from function

#include <stdio.h>

typedef struct Complex {
double real;
double imag;
} complex;

complex addNumbers(complex c1, complex c2) {
complex sum;
sum.real = c1.real + c2.real;
sum.imag = c1.imag + c2.imag;
return sum;
}

int main() {

complex c1 = {.real = 1.4, .imag = 4.5};
complex c2 = {.real = 5.4, .imag = -3.4};
complex result;

result = addNumbers(c1, c2);
printf("Sum: %.2lf + %.2lfi", result.real, result.imag);

return 0;
}
23 changes: 23 additions & 0 deletions add-complex-numbers3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// add complex numbers with input
#include <stdio.h>

typedef struct Complex {
double real;
double imag;
} complex;

int main() {

complex c1, c2, result;

scanf("%lf %lf ", &c1.real, &c1.imag);

scanf("%lf %lf ", &c2.real, &c2.imag);

result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;

printf("%.2lf + %.2lfi", result.real, result.imag);

return 0;
}
33 changes: 33 additions & 0 deletions addingMass-with-struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Add 2 masses with different units using struct

#include <stdio.h>

typedef struct Mass {
int stone;
int pounds;
} mass;

int main(){

mass m1;
mass m2;
mass result;

printf("Enter first distances\n");
printf("Enter stone and pounds: ");
scanf("%d %d", &m1.stone, &m1.pounds);

printf("Enter second distances\n");
printf("Enter stone and pounds: ");
scanf("%d %d", &m2.stone, &m2.pounds);

result.stone = m1.stone + m2.stone;
result.pounds = m1.pounds + m2.pounds;

result.stone = result.stone + (result.pounds / 14);
result.pounds = result.pounds % 14;

printf("Sum is %d stone and %d pounds", result.stone, result.pounds);

return 0;
}
22 changes: 22 additions & 0 deletions area-rectangle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// area of rectangle

#include <stdio.h>

struct rectangle{
int length;
int width;
};

void findArea(struct rectangle rec1) {
int area = rec1.length * rec1.width;
printf("Area: %d", area);
}

int main() {

struct rectangle rec = {.length = 4, .width = 2};

findArea(rec);

return 0;
}
22 changes: 22 additions & 0 deletions perimeter-rectangle.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// perimiter of rectangle

#include <stdio.h>

struct Rectangle {
int length;
int breadth;
};

void findPerimeter(struct Rectangle rec1){
int perimeter = 2*rec1.length + 2*rec1.breadth;
printf("%d", perimeter);
}

int main(){
struct Rectangle rec1;
scanf("%d %d ", &rec1.length, &rec1.breadth);

findPerimeter(rec1);

return 0;
}
23 changes: 23 additions & 0 deletions print-struct-values.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Print values of struct

#include <stdio.h>

struct Employee{
int age;
char name[50];
};


int main() {

struct Employee employee1;

scanf("%d ", &employee1.age);

fgets(employee1.name, sizeof(employee1.name), stdin);

printf("%s", employee1.name);
printf("%d", employee1.age);

return 0;
}
29 changes: 29 additions & 0 deletions store-player-info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Store player info

#include <stdio.h>

struct Player {
char name[50];
int strength;
double stamina;
} player1;

int main() {

printf("Enter name: ");
fgets(player1.name, sizeof(player1.name), stdin);

printf("Enter strength: ");
scanf(" %d", &player1.strength);

printf("Enter stamina: ");
scanf(" %lf", &player1.stamina);


// print student info
printf("Name: %s", player1.name);
printf("Strength: %d\n", player1.strength);
printf("Stamina: %.2lf", player1.stamina);

return 0;
}
24 changes: 24 additions & 0 deletions subtract-complex-numbers.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Subtract two complex numbers

#include <stdio.h>

typedef struct Complex {
double real;
double imag;
} complex;

int main() {

complex c1, c2, result;

scanf("%lf %lf ", &c1.real, &c1.imag);

scanf("%lf %lf ", &c2.real, &c2.imag);

result.real = c1.real - c2.real;
result.imag = c1.imag - c2.imag;

printf("%.2lf + %.2lfi", result.real, result.imag);

return 0;
}