C Programming: Program for making change

/* Header Files */
#include <stdio.h>
#define QUARTER 25;
#define DIME 10;
#define NICKEL 5;

/* Main Function */
int main()
{
/* Variable Declaration */
int amount;
int myquarters;
int mydimes;
int mynickles;
int pennies;

/*Input*/
printf("Enter amount of change: ");
scanf("%d",&amount);

/*Quarters*/
myquarters = amount / QUARTER;
amount = amount % QUARTER;

/*Dimes*/
mydimes = amount / DIME;
amount = amount % DIME;

/*Nickles*/
mynickles = amount / NICKEL;

/*Pennies*/
pennies = amount % NICKEL;

/*Output*/
printf("%d quarters, %d dimes, %d nickels, %d pennies\n", myquarters, mydimes, mynickles, pennies);
printf("\n");

/* Main Function Return Statement */
return 0;
}

This example shows:

  • how to use the modulus operator

Goat Code CS50 Week 1: What Greedy.C should have taught me

I should have learned:

  1. The change-making problem
  2. What a greedy algorithm is
  3. Difference between float and double
  4. Variable conversion
  5. Typecasting
  6. The number entered by the user must first be turned into pennies to be able to work with the value as money because pennies are integers
  7. How the modulus operator works
  8. Modulo division
  9. Program for making change

C Programming: Program to add two numbers using command-line arguments

/*Source URL: http://www.c4learn.com/c-programs/c-program-to-add-two-numbers-using-command-line-arguments-parameters.html */

#include <stdlib.h>
#include<stdio.h>

int main(int argc, char * argv[]) {
int i, sum = 0;

if (argc != 3) {
printf("You have forgot to type numbers.");
exit(1);
}

printf("The sum is : ");

for (i = 1; i < argc; i++)
sum = sum + atoi(argv[i]);

printf("%d\n", sum);

return 0;

}

C Programming: Program for argc() and argv() functions

/* URL Source: http://fresh2refresh.com/c-programming/c-function/c-command-line-arguments/ */

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])   //  command line arguments
{
if(argc!=5)
{
printf("Arguments passed through command line " \
"not equal to 5");
return 1;
}

printf("\n Program name  : %s \n", argv[0]);
printf("1st arg  : %s \n", argv[1]);
printf("2nd arg  : %s \n", argv[2]);
printf("3rd arg  : %s \n", argv[3]);
printf("4th arg  : %s \n", argv[4]);
printf("5th arg  : %s \n", argv[5]);

return 0;
}

Notes:

  • You can use each argv element just like a string, or use argv as a two dimensional array.

Goat Code CS50 Week 2: What Caesar.c should have taught me

I should have learned:

  1. The Caesar Cipher and Modular Arithmetic
  2. How Unix and the C language were originally meant to work with each other
  3. The importance of the command-line in C programming
  4. Why windows doesn’t have a more well developed command line interface
  5. How the passing of command-line arguments work in C programming work
  6. String manipulation
  7. How functions work
  8. Converting a string to an integer with the stdlib.h library and the atoi() function
  9. Program to display lower-case alphabet using ASCII values [Code | Flowchart]
  10. Program to display upper-case alphabet using ASCII values [Code | Flowchart]
  11. Program to find ASCII value [Code | Flowchart]
  12. Program for argc() and argv() functions [Code]
  13. Program to add two numbers using command-line arguments [Code]
  14. Flowgorithm Caesar Cipher

C Programming: Print ASCII Value

/* Source URL: https://www.programiz.com/c-programming/examples/ASCII-value-character */
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");

// Reads character input from the user
scanf("%c", &c);

// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d\n", c, c);
return 0;
}

This example teaches:

  • ASCII values
  • Use of %c with printf()
  • Use of %d with printf()

C Programming: Program to display lower-case alphabet using ASCII values

/* URL SOURCE: http://www.codeforwin.in/2015/06/c-program-to-print-a-to-z.html  */

#include <stdio.h>

int main()
{
int i;

/* Prints all ASCII values from 97 to 122 (a to z) */
for(i=97; i<=122; i++)
{
printf("ASCII value of character %c = %d\n", i, i);
}

return 0;
}

The ASCII values for the alphabet in lowercase are: 97 to 122

C Programming: Program to display upper-case alphabet using ASCII values

/* URL SOURCE: http://www.codeforwin.in/2015/06/c-program-to-print-a-to-z.html  */

#include <stdio.h>

int main()
{
int i;

/* Prints all ASCII values from 65 to 90 (A to Z) */
for(i=65; i<=90; i++)
{
printf("ASCII value of character %c = %d\n", i, i);
}

return 0;
}

The ASCII values for the alphabet in uppercase are: 65 to 90