Put everything in folders

This commit is contained in:
2015-10-14 02:01:48 +02:00
parent 66424386af
commit 5f40a4bfe1
8 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
#include <stdio.h>
int main()
{
float nr1, nr2;
printf("First number: ");
scanf("%f", &nr1);
printf("Second number: ");
scanf("%f", &nr2);
printf("Total: %.2f\n", nr1+nr2);
printf("Average: %.2f\n", (nr1+nr2)/2);
return 0;
}

View File

@@ -0,0 +1,31 @@
#include <stdio.h>
/* A program to calculate the average of a set of numbers and the biggest
* of those numbers. From the book "Vägen till C". Translated it to english.
*/
int main()
{
float nr, sum, biggest;
int ant;
sum = 0;
biggest = 0;
ant = 0;
printf("Write the numbers.\n");
printf("Finish with EOF.\n"); // EOF is CTRL-D in Unix/Linux
while (scanf("%f", &nr) == 1) // As long as we get characters to scanf, it's
{ // true, ie '1'.
ant++;
sum += nr; // Same as sum = sum + nr.
if (nr > biggest) // If 'nr' is bigger than 'biggest', make 'nr'
{ // the new 'biggest' value.
biggest = nr;
}
}
printf ("Average: %.3f\n", sum / ant);
printf ("Biggest: %.3f\n", biggest);
return 0;
}

View File

@@ -0,0 +1,40 @@
#include <stdio.h>
/* A first example from the book "Vägen till C" where we get to use functions.
* Here is a function to get the biggest number from a set of numbers.
*/
float max(float x, float y) // It's return value is a float.
{
if (x > y)
{
return x;
}
else
{
return y;
}
}
int main() // main's return value is an int (return 0 at the bottom).
{
float number, sum, biggest;
int ant;
sum = 0;
biggest = 0;
ant = 0;
printf("Write the numbers\nFinish with EOF\n");
while (scanf("%f", &number) == 1)
{
ant++;
sum += number;
biggest = max(biggest, number); // biggest goes into x, number into y.
}
printf("Average: %.2f\n", sum / ant);
printf("Biggest: %.2f\n", biggest);
return 0;
}

View File

@@ -0,0 +1,20 @@
#include <stdio.h>
#define YEARS 15
/* Simple example of a program to calculate interest on interest */
int main()
{
float cap, interest;
int year;
printf("Invested capial: "); scanf("%f", &cap);
printf("Interest? "); scanf("%f", &interest);
printf("\n Year Holding\n ==== =======\n");
for (year = 1; year <= YEARS; year++)
{
cap = cap * (1 + interest / 100);
printf("%5d%13.2f\n", year, cap);
}
return 0;
}

View File

@@ -0,0 +1,19 @@
#include <stdio.h>
int main()
{
float cap, interest;
int year;
int nrOfYears;
printf("Invested capial: "); scanf("%f", &cap);
printf("Interest? "); scanf("%f", &interest);
printf("How many years? "); scanf("%d", &nrOfYears);
printf("\n Year Holding\n ==== =======\n");
for (year = 1; year <= nrOfYears; year++)
{
cap = cap * (1 + interest / 100);
printf("%5d%13.2f\n", year, cap);
}
return 0;
}

View File

@@ -0,0 +1,30 @@
#include <stdio.h>
/* A simple program from the book "Vägen till C" to calculate for how long your
* money will have to sit in the bank at a given interest rate to reach the
* amount of money you'll want
*/
int main()
{
float cap, want, interest;
int year;
printf("Invested capital: "); scanf("%f", &cap);
printf("Want sum: "); scanf("%f", &want);
printf("Interest? "); scanf("%f", &interest);
year = 0;
/* As long as your capital at the bank is less than you want, keep rerunning
* the loop until we reach the amout we want. Each loop is one year.
*/
while (cap < want)
{
year = year + 1;
cap = cap * (1 + interest / 100);
}
printf("The capital will have to be at the bank for %d years\n", year);
return 0;
}