From 395564a57ac45abcdc8f9d5315d2dd547bc4d3a6 Mon Sep 17 00:00:00 2001 From: "Shaquille K. Soekhlal" Date: Thu, 12 Sep 2019 08:55:54 +0200 Subject: [PATCH] Added previous assignments --- Assignment 1/main.c | 18 +++++++++++++++ Assignment 2/.vscode/tasks.json | 32 +++++++++++++++++++++++++++ Assignment 2/main.c | 21 ++++++++++++++++++ Assignment 3/.vscode/settings.json | 5 +++++ Assignment 3/main.c | 32 +++++++++++++++++++++++++++ Assignment 4/2.c | 35 ++++++++++++++++++++++++++++++ Assignment 4/main.c | 32 +++++++++++++++++++++++++++ 7 files changed, 175 insertions(+) create mode 100644 Assignment 1/main.c create mode 100644 Assignment 2/.vscode/tasks.json create mode 100644 Assignment 2/main.c create mode 100644 Assignment 3/.vscode/settings.json create mode 100644 Assignment 3/main.c create mode 100644 Assignment 4/2.c create mode 100644 Assignment 4/main.c diff --git a/Assignment 1/main.c b/Assignment 1/main.c new file mode 100644 index 0000000..50d1571 --- /dev/null +++ b/Assignment 1/main.c @@ -0,0 +1,18 @@ +/* + * Student: S.K Soekhlal + * Number: 4860632 + * Assignment: 1.3 + */ + +#include +#include + +int main(){ + double c,k=0,f=0; /*Declaring variables*/ + scanf("%lf",&c); /*Storing an inputted value in variable C(for celsius)*/ + k = c + 273.15; /*Calculating temperature in Kelvin*/ + f = (c*1.8)+32; /*Calculating temperature in Fahrenheit*/ + printf("C\tK\tF\n"); + printf("%.2f\t%.2f\t%.2f\n",c,k,f); /*printing values*/ + return 0; +} \ No newline at end of file diff --git a/Assignment 2/.vscode/tasks.json b/Assignment 2/.vscode/tasks.json new file mode 100644 index 0000000..168b333 --- /dev/null +++ b/Assignment 2/.vscode/tasks.json @@ -0,0 +1,32 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "type": "shell", + "label": "gcc.exe build active file", + "command": "C:\\MinGW\\bin\\gcc.exe", + "args": [ + "-g", + "-ansi", + "-pedantic", + "-Wall", + "-lm", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "C:\\MinGW\\bin" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + } + } + ] +} \ No newline at end of file diff --git a/Assignment 2/main.c b/Assignment 2/main.c new file mode 100644 index 0000000..9a18b5b --- /dev/null +++ b/Assignment 2/main.c @@ -0,0 +1,21 @@ +/* + *Student: S.K Soekhlal + * Number: 4860632 + * Assignment: 1.4 + */ + +#include +#include +#include + +int main(){ + double a,r,b=0,sum=0; /*Declaring variables*/ + int N; + scanf("%lf%d%lf",&a,&N,&r); /*Storing inputted values in variables*/ + for(;N>=0;--N){ /*Starting for loop to calculate sum*/ + b = a * (pow(r,N)); /*calculating sum*/ + sum += b; + } + printf("%.2f\n", sum); /*Printing sum on the screen*/ + return 0; +} \ No newline at end of file diff --git a/Assignment 3/.vscode/settings.json b/Assignment 3/.vscode/settings.json new file mode 100644 index 0000000..0f06797 --- /dev/null +++ b/Assignment 3/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "stdio.h": "c" + } +} \ No newline at end of file diff --git a/Assignment 3/main.c b/Assignment 3/main.c new file mode 100644 index 0000000..34fb07a --- /dev/null +++ b/Assignment 3/main.c @@ -0,0 +1,32 @@ +/* + *Student: S.K Soekhlal + * Number: 4860632 + * Assignment: 2.12 + */ +#include +#include +#include + +int main(){ + int a=0,i,temp=0; /*Declaring variables*/ + scanf("%d",&a); /*Storing user input in variabl a*/ + if(a < 2){ /*Testing if input is 0 or 1*/ + printf("%d is not a prime number\n",a); /*We don't see 1 as a prime number*/ + } + else + { + for(i=2; pow(i,2)<=a; i++){ /*using for loop to determine if input is not prime*/ + if (a%i == 0){ /*If remainder of a/i = 0 then the number is not prime*/ + temp = 1; + break; + } + } + if(temp == 1){ + printf("%d is not a prime number\n",a); + } + else{ + printf("%d is a prime number\n",a); + } + } + return 0; +} diff --git a/Assignment 4/2.c b/Assignment 4/2.c new file mode 100644 index 0000000..673f1a8 --- /dev/null +++ b/Assignment 4/2.c @@ -0,0 +1,35 @@ +#include +#include +#include + +int is_prime(int number) +{ /*Function decleration and definition */ + int x, temp = 0; + printf("%d is being checked\n",number); + for (x = 2; pow(x, 2) >= number; x++) { /*using for loop to determine if input is not prime*/ + printf("%d \t am in loop\n",number); + if (number < 2) { /*If number is 0 or 1, it is not prime*/ + temp = 1; + printf("%d is below 2, is not prime\n",number); + } + if (number % x == 0) { /*If remainder of input/x^2 = 0 then the number is not prime*/ + temp = 1; + printf("%d has another divisor, is not prime\n",number); + break; + } + } + if (temp == 0) { /*Print out number if the value of temp is not equal to 1 (so it outputs only prime number)*/ + printf("%d\n", number); + } + return number; +} + +int main() +{ + int lower_limit, upper_limit, i; /*Declaring variables*/ + scanf("%d%d", &lower_limit, &upper_limit); /*Storing user input in lower and upper limit variables*/ + for (i = lower_limit; i <= upper_limit; i++) { /*using for loop to call is_prime function to calculate if the number is prime*/ + is_prime(i); + } + return 0; +} \ No newline at end of file diff --git a/Assignment 4/main.c b/Assignment 4/main.c new file mode 100644 index 0000000..555fcfa --- /dev/null +++ b/Assignment 4/main.c @@ -0,0 +1,32 @@ +/* + *Student: S.K Soekhlal + * Number: 4860632 + * Assignment: 3.3 + */ +#include +#include +#include + +int is_prime(int number){ /*Function decleration and definition */ + int x; + if(number < 2){ /*If number is 0 or 1, it is not prime*/ + return 0; + } + for(x=2; pow(x,2)<=number; x++){ /*using for loop to determine if input is not prime*/ + if (number%x == 0){ /*If remainder of input/x^2 = 0 then the number is not prime*/ + return 0; + } + } + return 1; +} + +int main(){ + int lower_limit,upper_limit,i; /*Declaring variables*/ + scanf("%d%d",&lower_limit,&upper_limit); /*Storing user input in lower and upper limit variables*/ + for(i=lower_limit; i <= upper_limit; i++){ /*using for loop to call is_prime function to calculate if the number is prime*/ + if(is_prime(i) == 1){ + printf("%d\n",i); + } + } + return 0; +}