C Programming

C programming

 

 

Check some challenge problems in C here. 

  

Example programs.

This program prints odds numbers from 1 to N:  

#include <stdio.h>

int main (void) {

int j;

int n;

int result = 1;

printf ("Enter an odd number: ");

scanf ("%d", &n);

while (n & 2 == 0) {

printf ("Value must be odd number. Enter a number again.\n");

scanf ("%d", &n);

}

for (j = 1; j <= n; j = j + 2) {

printf ("%d ", j);

}

return 0;

}

 *********************************************************************************************

Read multiple user inputs.

Write two scanf statements to get input values into birthMonth and birthYear. Then write a statement to output the month, a slash, and the year. End with newline.

#include <stdio.h> 

int main (void) {

int birthDay;

int birthMonth;

int birthYear;

printf("Enter bithday Day: ");

scanf ("%d", &birthDay);

printf("Enter bithday Month: ");

scanf ("%d", &birthMonth);

printf("Enter bithday Year: ");

scanf ("%d", &birthYear);

printf ("%d/%d/%d\n", birthMonth, birthDay, birthYear);

return 0;

} 

 

*********************************************************************************************

Output simple text with newlines.

Write code that prints the following. End each output line with a newline. 

A1

B2

Note: Whitespace (blank spaces / blank lines) matters; make sure your whitespace exactly matches the expected output.

 

 

 

#include <stdio.h> 

int main(void) { 

printf("A1\n"); 

printf("B2\n"); 

return 0; 

}

 

 ********************************************************************************************* 

Output text and variable.

Write a statement that outputs variable numCars as follows. End with a newline.
There are 99 cars.

#include <stdio.h> 

int main(void) { 

int numCars = 99; 
printf("There are %d cars.\n", numCars); 

return 0; 

}

 

*********************************************************************************************

Read user input and print to output.

Write a statement that reads an integer into userNum, and a second statement that prints userNum followed by a newline.

 

#include <stdio.h>

int main(void) {
 int userNum = 0;

scanf("%d", &userNum);
 printf("%d\n", userNum );

return 0;
}

*********************************************************************************************
Read multiple user inputs.
Write two statements to get input values into birthMonth and birthYear. Then write a statement to output the month, a slash, and the year. End with newline.

 

#include <stdio.h>

int main(void) {
 int birthMonth;
 int birthYear;

scanf("%d", &birthMonth);
 scanf("%d", &birthYear);
 printf("%d/%d\n", birthMonth, birthYear);

return 0;
}

*********************************************************************************************
Basic syntax errors.

 Type the statements. Then, correct the one syntax error in each statement. Hints: Statements end in semicolons, and string literals use double quotes.

printf("Predictions are hard.\n";
printf("Especially ');
printf("about the future.\n").
print("Num is: %d\n", userNum);

Corrected:

 

#include <stdio.h>

int main(void) {
 int userNum;

userNum = 5;

printf("Predictions are hard.\n");
printf("Especially ");
printf("about the future.\n");
printf("Num is: %d\n", userNum);

return 0;
}

  *********************************************************************************************

 

More syntax errors.

Retype the statements, correcting the syntax errors.

   printf("Num: %d\n", songnum);
   printf("%d\n", int songNum); 
   printf("%d songs\n" songNum);
 
#include <stdio.h>

int main(void) {
 int songNum;

songNum = 5;

printf("Num: %d\n", songNum);
printf("%d\n", songNum); 
printf("%d songs\n", songNum);


 return 0;
}

 ********************************************************************************************* 

 

Array calculator in C.

Multiplying arrays in C

Example program using switch statements

 

 

/* Copyright ©TheBookdale

This program is the start of project 2 - A Matrix Calculator*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void){

int i, j, k; //used in for loops

double matrixA[10][10]; // initialized at 10 just to have it initialized
 double matrixB[10][10];
 int rowA, colA;
 int rowB, colB;

double sumM[10][10]; //sum operation matrix
 double subM[10][10]; //substration operation matrix
 double scaM[10][10]; //scalar operation matrix
 double mulM[10][10]; //multiplication operation matrix

char operation = ' ';//used in swtich statements
 char again = 'Y';
 double scalar = 0.0;

while (again == 'Y'){


 //this is the operation menu just type A, B, C or D to calculate
 printf("\nOperation Menu\n");
 printf("\t A to Add\n");
 printf("\t B to Subtract\n");
 printf("\t C to Scalar Multiply\n");
 printf("\t D to Multiply two matrices\n");
 printf("Enter yout choice: ");
 scanf(" %c", &operation);
 operation = toupper(operation);//this will convert the letter input into a capital letter, this way the switch statement will work well

switch (operation){

case 'A':
 printf("\nEnter the #rows and #cols for matrix A: ");
 scanf("%d%d", &rowA, &colA);

printf("Enter the #rows and #cols for matrix B: ");
 scanf("%d%d", &rowB, &colB);

while ((rowA != rowB) && (colA != colB)){
 printf("\nMatrices must be the same size\n");
 printf("\nEnter the #rows and #cols for matrix A: ");
 scanf("%d%d", &rowA, &colA);

printf("Enter the #rows and #cols for matrix B: ");
 scanf("%d%d", &rowB, &colB);

}


 printf("\n\tEnter elements of Matrix A a %d x %d matrix.\n", rowA, colA); // with the %d we remember the user the dimentions of the array


 for (i = 0; i < rowA; i++){
 for (j = 0; j < colA; j++){
 scanf("%lf", &matrixA[i][j]); // we tell the program where to store the values
 }
 }


 printf("\n\tEnter elements of Matrix B a %d x %d matrix.\n", rowB, colB); // with the %d we remember the user the dimentions of the array

for (i = 0; i < rowB; i++){
 for (j = 0; j < colB; j++){
 scanf("%lf", &matrixB[i][j]); // we tell the program where to store the values
 }
 }

printf("\nThe Sum of matrixA + matrixB is : \n");
 for (i = 0; i < rowA; i++){
 for (j = 0; j < colA; j++){
 sumM[i][j] = matrixA[i][j] + matrixB[i][j];
 printf("%.1lf\t", sumM[i][j]);
 }
 printf("\n");
 }
 break;

case 'B':

printf("\nEnter the #rows and #cols for matrix A: ");
 scanf("%d%d", &rowA, &colA);

printf("Enter the #rows and #cols for matrix B: ");
 scanf("%d%d", &rowB, &colB);

while ((rowA != rowB) && (colA != colB)){
 printf("\nMatrices must be the same size\n");
 printf("\nEnter the #rows and #cols for matrix A: ");
 scanf("%d%d", &rowA, &colA);

printf("Enter the #rows and #cols for matrix B: ");
 scanf("%d%d", &rowB, &colB);
 }

printf("\n\tEnter elements of Matrix A a %d x %d matrix.", rowA, colA); // with the %d we remember the user the dimentions of the array


 for (i = 0; i < rowA; i++){
 for (j = 0; j < colA; j++){
 scanf("%lf", &matrixA[i][j]); // we tell the program where to store the values
 }
 }

printf("\n\tEnter elements of Matrix B a %d x %d matrix.", rowB, colB); // with the %d we remember the user the dimentions of the array

for (i = 0; i < rowB; i++){
 for (j = 0; j < colB; j++){
 scanf("%lf", &matrixB[i][j]); // we tell the program where to store the values
 }
 }

printf("\nThe difference between matrixA - matrixB is : \n");
 for (i = 0; i < rowA; i++){
 for (j = 0; j < colA; j++){
 subM[i][j] = matrixA[i][j] - matrixB[i][j];
 printf("%.1lf\t", subM[i][j]);
 }
 printf("\n");
 }
 break;

case 'C':

printf("Enter the scalar: ");
 scanf("%lf", &scalar);
 printf("\nThe scalar is: %.1lf ", scalar);


 printf("\nEnter the #rows and #cols for matrix A: ");
 scanf("%d%d", &rowA, &colA);

printf("\n\tEnter elements of Matrix A a %d x %d matrix.", rowA, colA); // with the %d we remember the user the dimentions of the array


 for (i = 0; i < rowA; i++){
 for (j = 0; j < colA; j++){
 scanf("%lf", &matrixA[i][j]); // we tell the program where to store the values
 }
 }

printf("\nThe scalar multiplication between matrixA * %.1lf is: \n", scalar);
 for (i = 0; i < rowA; i++){
 for (j = 0; j < colA; j++){
 scaM[i][j] = scalar * matrixA[i][j];
 printf("%.1lf\t", scaM[i][j]);
 }
 printf("\n");
 }

break;

case 'D':
 //when mulotiplying arrays matrixA colum # has to equal matrixB row #
 printf("\nEnter the #rows and #cols for matrix A: ");
 scanf("%d%d", &rowA, &colA);

printf("Enter the #rows and #cols for matrix B: ");
 scanf("%d%d", &rowB, &colB);

// Column of first matrix should be equal to column of second matrix and
 while (colA != rowB)
 {
 printf("\n\nError! column of first matrix not equal to row of second.\n\n");
 printf("\nEnter the #rows and #cols for matrix A: ");
 scanf("%d%d", &rowA, &colA);

printf("Enter the #rows and #cols for matrix B: ");
 scanf("%d%d", &rowB, &colB);
 }

// Storing elements of first matrix.
 printf("\n\tEnter elements of Matrix A a %d x %d matrix.", rowA, colA); // with the %d we remember the user the dimentions of the array


 for (i = 0; i < rowA; i++){
 for (j = 0; j < colA; j++){
 scanf("%lf", &matrixA[i][j]); // we tell the program where to store the values
 }
 }

// Storing elements of second matrix.
 printf("\n\tEnter elements of Matrix B a %d x %d matrix.", rowB, colB); // with the %d we remember the user the dimentions of the array

for (i = 0; i < rowB; i++){
 for (j = 0; j < colB; j++){
 scanf("%lf", &matrixB[i][j]); // we tell the program where to store the values
 }
 }

// Initializing all elements of result matrix to 0
 for (i = 0; i<rowA; ++i)
 for (j = 0; j<colB; ++j)
 {
 mulM[i][j] = 0;
 }

// Multiplying matrices a and b and
 // storing result in result matrix
 for (i = 0; i<rowA; ++i)
 for (j = 0; j<colB; ++j)
 for (k = 0; k<colA; ++k)
 {
 mulM[i][j] += matrixA[i][k] * matrixB[k][j];
 }

// Displaying the result
 printf("\nOutput Matrix:\n");
 for (i = 0; i<rowA; ++i)
 for (j = 0; j<colB; ++j)
 {
 printf("%.1lf ", mulM[i][j]);
 if (j == colB - 1)
 printf("\n\n");


 }


 break;
default:
 printf("\nIncorrect option! Please choose option A-D.");
 break;
}

printf("\n\nDo you want to calculate again? Y/N\n");
 scanf(" %c", &again);
 again = toupper(again);
 }
 printf("\n\nGoodbye!\n\n");

return 0;
}

 

 ********************************************************************************************* 

Calculating area a geometrical figures with user defined functions

/*This program calculates area of various geometrical shapes*/

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
#define M_PI 3.14159265358979323846


//User Defined Function Declaration
double triangleArea(int b, int h);
double circleArea(int r);
double rectangleArea(int b, int h);

int main(void){
 int x, y, radius;
 char operation = ' ';
 char loop = 'Y';

while (loop == 'Y'){


 printf("\tGeometrical Shapes Calculator\n\n");
 printf("What would you like to calculate the are of?\n\n");
 printf("\t1.\tTriangle\n");
 printf("\t2.\tCircle\n");
 printf("\t3.\tRectangle\n");
 printf("\t4.\tSquare\n\n");
 printf("Enter an option(1,2,3,4): ");
 scanf(" %c", &operation);
 operation = toupper(operation);
 


switch (operation){
 case 'A':
 printf("Enter base: ");
 scanf("%d", &x);
 printf("\nEnter height: ");
 scanf("%d", &y);
 printf("\nThe Triangle area is: %.2lf.\n", triangleArea(x, y));
 break;

case 'B':
 printf("Radius: ");
 scanf("%d", &radius);
 printf("\nThe circle area is: %.2lf\n", circleArea(radius));
 break;
 
 case 'C':
 printf("Enter base: ");
 scanf("%d", &x);
 printf("\nEnter width: ");
 scanf("%d", &y);
 printf("\nThe rectangle area is: %.2lf\n", rectangleArea(x,y));
 break;
 
 case 'D':
 printf("Enter square side lenght: ");
 scanf("%d", &x);
 printf("\nThe square area is: %.2lf\n", rectangleArea(x,x));
 break;
 
default:
 printf("\nIncorrect option! Please choose a number 1-4.");
 break;


 }

printf("\nDo you want to calculate again? Y/N\n");
 scanf(" %c", &loop);
 loop = toupper(loop);


 }

printf("\n\nGoodbye!");

return 0;
}

//User Defined Function Definition
double triangleArea(int b, int h){
 double area;
 area = (b*h) / 2;

return area;


}

double circleArea(int r){
 double area;
 area = M_PI*(r*r);
 return area;
 
}

double rectangleArea(int b, int h){
 double area;
 area = b * h;
 
 return area;
}

 

 ********************************************************************************************* 

matrix calculator with user defined functions

 

/*
Copyright ©TheBookdale. All rights reserved.
This program calculates the  addition, subtraction and multiplication of matrices
as well as a scalar multiplication
(PROJECT 3)
*/

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>


//User Defined Function Declaration
void readMatrix(int array[10][10], int rows, int colums);
void printMatrix(int array[10][10], int rows, int colums);
void matrixAddSub(int arrayone[10][10], int arraytwo[10][10], int rows, int colums, int mul);
void matrixScalarMultiply(int array[10][10], int scalar, int rows, int colums);
void matrixMultiply(int arrayone[10][10], int arraytwo[10][10], int rowsA, int columsA, int columsB);

int main(void){

 int i, j, k; //used in for loops

 int matrixA[10][10]; // initialized at 10 just to have it initialized
 int matrixB[10][10];
 int rowA, colA;
 int rowB, colB;
 int operation;//used in swtich statements
 char again = 'Y';
 int scalar = 0;
 int add = 1;
 int sub = -1;

 while (again == 'Y'){


  //this is the operation menu just type A, B, C or D to calculate
  printf("\nOperation Menu\n");
  printf("\t1. to Add\n");
  printf("\t2. to Subtract\n");
  printf("\t3. to Scalar Multiply\n");
  printf("\t4. to Multiply two matrices\n");
  printf("Enter yout choice: ");
  scanf(" %d", &operation);
  

  switch (operation){

  case 1:
   printf("\nEnter the #rows and #cols for matrix A: ");
   scanf("%d%d", &rowA, &colA);

   printf("Enter the #rows and #cols for matrix B: ");
   scanf("%d%d", &rowB, &colB);

   while ((rowA != rowB) && (colA != colB)){
    printf("\nMatrices must be the same size\n");
    printf("\nEnter the #rows and #cols for matrix A: ");
    scanf("%d%d", &rowA, &colA);

    printf("Enter the #rows and #cols for matrix B: ");
    scanf("%d%d", &rowB, &colB);

   }


   printf("\n\tEnter elements of Matrix A a %d x %d matrix.\n", rowA, colA); // with the %d we remember the user the dimentions of the array
   readMatrix(matrixA, rowA, colA);
   printf("\n\t\tMatrix A\n\n");
   printMatrix(matrixA, rowA, colA);


   printf("\n\tEnter elements of Matrix B a %d x %d matrix.\n", rowB, colB); // with the %d we remember the user the dimentions of the array
   readMatrix(matrixB, rowB, colB);
   printf("\n\t\tMatrix B\n\n");
   printMatrix(matrixB, rowB, colB);

   
   printf("\nThe Sum of matrixA + matrixB is : \n");
   matrixAddSub(matrixA, matrixB, rowA, colA, add);

   break;

  case 2:

   printf("\nEnter the #rows and #cols for matrix A: ");
   scanf("%d%d", &rowA, &colA);

   printf("Enter the #rows and #cols for matrix B: ");
   scanf("%d%d", &rowB, &colB);

   while ((rowA != rowB) && (colA != colB)){
    printf("\nMatrices must be the same size\n");
    printf("\nEnter the #rows and #cols for matrix A: ");
    scanf("%d%d", &rowA, &colA);

    printf("Enter the #rows and #cols for matrix B: ");
    scanf("%d%d", &rowB, &colB);
   }

   printf("\n\tEnter elements of Matrix A a %d x %d matrix.\n", rowA, colA); // with the %d we remember the user the dimentions of the array
   readMatrix(matrixA, rowA, colA);
   printf("\n\t\tMatrix A\n\n");
   printMatrix(matrixA, rowA, colA);

   printf("\n\tEnter elements of Matrix B a %d x %d matrix.\n", rowB, colB); // with the %d we remember the user the dimentions of the array
   readMatrix(matrixB, rowB, colB);
   printf("\n\t\tMatrix B\n\n");
   printMatrix(matrixB, rowB, colB);

   printf("\nThe difference between matrixA - matrixB is : \n");
   matrixAddSub(matrixA, matrixB, rowA, colA, sub);
   break;

  case 3:

   printf("\nEnter the scalar: ");
   scanf("%d", &scalar);
   printf("\nThe scalar is: %d ", scalar);


   printf("\nEnter the #rows and #cols for matrix A: ");
   scanf("%d%d", &rowA, &colA);

   printf("\n\tEnter elements of Matrix A a %d x %d matrix.\n", rowA, colA); // with the %d we remember the user the dimentions of the array
   readMatrix(matrixA, rowA, colA);
   printf("\n\t\tMatrix A\n\n");
   printMatrix(matrixA, rowA, colA);

   printf("\nThe scalar multiplication between matrixA * %d is: \n", scalar);
   matrixScalarMultiply(matrixA, scalar, rowA, colA);

   break;

  case 4:
   //when mulotiplying arrays matrixA colum # has to equal matrixB row #
   printf("\nEnter the #rows and #cols for matrix A: ");
   scanf("%d%d", &rowA, &colA);

   printf("Enter the #rows and #cols for matrix B: ");
   scanf("%d%d", &rowB, &colB);

   // Column of first matrix should be equal to column of second matrix and
   while (colA != rowB)
   {
    printf("\n\nError! column of first matrix not equal to row of second.\n\n");
    printf("\nEnter the #rows and #cols for matrix A: ");
    scanf("%d%d", &rowA, &colA);

    printf("Enter the #rows and #cols for matrix B: ");
    scanf("%d%d", &rowB, &colB);
   }

   // Storing elements of first matrix.
   printf("\n\tEnter elements of Matrix A a %d x %d matrix.\n", rowA, colA); // with the %d we remember the user the dimentions of the array
   readMatrix(matrixA, rowA, colA);
   printf("\n\t\tMatrix A\n\n");
   printMatrix(matrixA, rowA, colA);

   // Storing elements of second matrix.
   printf("\n\tEnter elements of Matrix B a %d x %d matrix.\n", rowB, colB); // with the %d we remember the user the dimentions of the array
   readMatrix(matrixB, rowB, colB);
   printf("\n\t\tMatrix A\n\n");
   printMatrix(matrixB, rowB, colB);

   //multiplyng arrays
   matrixMultiply(matrixA, matrixB, rowA, colA, colB);
   
   break;

  default:
   printf("\nIncorrect option! Please choose a number 1-4.");
   break;
  }

  printf("\n\nDo you want to calculate again? Y/N\n");
  scanf(" %c", &again);
  again = toupper(again);
 }
 printf("\n\nGoodbye!\n\n");

 return 0;
}


//User Defined Function Definition
void readMatrix(int array[10][10], int rows, int colums){
 int i, j;
 for (i = 0; i < rows; i++){
  printf("\t%d entries for row %d: ", colums, i + 1);
  for (j = 0; j < colums; j++){
   scanf("%d", &array[i][j]);
  }
 }
 
 return;
}

void printMatrix(int array[10][10], int rows, int colums){
 int i, j;

 for (i = 0; i < rows; i++) {
  for (j = 0; j < colums; j++){
   printf("\t%d", array[i][j]);
  }
  printf("\n");
 }
}


void matrixAddSub(int arrayone[10][10], int arraytwo[10][10], int rows, int colums, int mul){
 int i, j;
 int sumM[10][10];
 int scaM[10][10];
 for (i = 0; i < rows; i++){
  for (j = 0; j < colums; j++){
   scaM[i][j] = mul * arraytwo[i][j];
   }
  }


 for (i = 0; i < rows; i++){
  for (j = 0; j < colums; j++){
   sumM[i][j] = arrayone[i][j] + scaM[i][j];
   printf("\t%d", sumM[i][j]);
  }
  printf("\n");
 }
}


void matrixScalarMultiply(int array[10][10], int scalar, int rows, int colums){
 int i, j;
 int scaM[10][10];
 for (i = 0; i < rows; i++){
  for (j = 0; j < colums; j++){
   scaM[i][j] = scalar * array[i][j];
   printf("%d\t", scaM[i][j]);
  }
  printf("\n");
 }

}

void matrixMultiply(int arrayone[10][10], int arraytwo[10][10], int rowsA, int columsA,int columsB){
 int i, j, k;
 int mulM[10][10];

 // Initializing all elements of result matrix to 0
 for (i = 0; i<rowsA; ++i)
  for (j = 0; j<columsB; ++j)
  {
   mulM[i][j] = 0;
  }

 // Multiplying matrices a and b and
 // storing result in result matrix
 for (i = 0; i<rowsA; ++i)
  for (j = 0; j<columsB; ++j)
   for (k = 0; k<columsA; ++k)
   {
    mulM[i][j] += arrayone[i][k] * arraytwo[k][j];
   }
 printf("\nOutput Matrix:\n");
 for (i = 0; i<rowsA; ++i)
  for (j = 0; j<columsB; ++j)
  {
   printf("\t%d ", mulM[i][j]);
   if (j == columsB - 1)
    printf("\n\n");
  }
}


*********************************************************************************************

 Pythagorean Triple function in C

/*
Copyright ©TheBookdale. All rights reserved.
This program checks to see if user inputs represent a Pythagorean Triple
*/


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>

// User Defined Function Declaration
int pythagoreanTriple(int x, int y, int z);

int main(void){
 int a, b, c;
 char loop = 'Y';

while (loop == 'Y' || loop == 'y'){

printf("This program verifies if inputs represent a Pythagorean Triple\n\n");
 printf("Enter values for a, b, and c: ");
 scanf("%d%d%d", &a, &b, &c);

pythagoreanTriple(a, b, c);

printf("\n\nDo you want to calculate again? Y/N\n");
 scanf(" %c", &loop);
 }

printf("\nGoodbye!\n\n");


 return 0;
}

int pythagoreanTriple(int x, int y, int z){
 int triple;

if (pow(x, 2) + pow(y, 2) == pow(z, 2)){

triple = 1;
 printf("triple = %d\n", triple);
 printf("%d, %d and %d are Pythagorean Triple triple\n", x, y, z);
 }

else{

triple = 0;
 printf("triple = %d\n", triple);
 printf("%d, %d and %d are not Pythagorean Triple triple\n", x, y, z);
 }

return triple;
}

 *********************************************************************************************

 

Complex numbers basic calculator

 

#include <stdio.h>

int main (void){

double a, b, c, d;
double realsum, imagsum, realsub, imagsub, realmul, imagmul, realdivt, imagdivt, realdivb, realdiv, imagdiv;

printf("\nEnter the first set of numbers: ");
scanf("%lf%lf", &a, &b);
printf("\nEnter the second set of numbers: ");
scanf("%lf%lf", &c, &d);


realmul = (a*c) + (b*d)*(-1);
imagmul = (a*d) + (b*c);

realsum = a+c;
imagsum = b+d;

realsub = a-c;
imagsub = b-d;

realdivt = a*c +b*d;
imagdivt = a*d*(-1) + b*c;
realdivb = c*c + d*d;
realdiv = realdivt/realdivb;
imagdiv = imagdivt/realdivb;


printf("The sum of (%.2lf %.2lfi) + (%.2lf %.2lfi) = %.2lf %.2lfi\n\n", a, b, c, d, realsum, imagsum);

printf("The substraction (%.2lf %.2lfi)-(%.2lf %.2lfi) = %.2lf %.2lfi\n\n", a, b, c, d, realsub, imagsub);


printf("The multiplication (%.2lf %.2lfi)*(%.2lf %.2lfi) = %.2lf %.2lfi\n\n", a, b, c, d, realmul, imagmul);

printf("The divition (%.2lf %.2lfi) / (%.2lf %.2lfi) = %.2lf %.2lfi\n\n", a, b, c, d, realdiv, imagdiv);


return 0;
}

 *********************************************************************************************

Calculating change.

/*This program calculates change in different coins. This program combines user defined functions and pointers.
Enjoy the code
*/
#include <stdio.h>

void ComputeChange(int totCents, int* quartersChange, int* dimesChange, int* nickelsChange, int* penniesChange) { // FIXME add four pass by pointer parameters
 int cents = 0;
 
 *quartersChange = totCents / 25;
 cents = totCents - (*quartersChange * 25);
 
 *dimesChange = cents / 10;
 cents = cents - (*dimesChange * 10);
 
 *nickelsChange = cents / 5;
 cents = cents - (*nickelsChange * 5);
 
 *penniesChange = cents;
 
 return;
}

int main(void) {

int totalCents = 67; // Total amount of change needed
 int quartersChange = 0; // Number of quarters used for change
 int dimesChange = 0; // Number of dimes used for change
 int nickelsChange = 0; // Number of nickels used for change
 int penniesChange = 0; // Number of pennies used for change

ComputeChange(totalCents, &quartersChange, &dimesChange, &nickelsChange, &penniesChange); // FIXME Add four pointer arguments

printf("Change for %d cents is:\n", totalCents);
 printf("\t%d quarters\n", quartersChange);
 printf("\t%d dimes\n", dimesChange);
 printf("\t%d nickels\n", nickelsChange);
 printf("\t%d pennies\n", penniesChange);

return 0;
}
  *********************************************************************************************

Working with strings

Write a C program  that finds the length of  a string, and finds the total number of letter 't' in that string.

#include <stdio.h>
#include <string.h>
int
main (void)
{
char str1[70] =
"Why do mathematicians like parks? Because of all the natural logs.";


char str2[70] =
"Why did the programmer quit his job? Because he didn't get arrays.";


char str3[70] =
"What did the force day to the distance? We're having a moment.";


int len1 = 0;
int len2 = 0;
int len3 = 0;
int t1 = 0;
int t2 = 0;
int t3 = 0;
int i = 0;

 

len1 = strlen (str1);
len2 = strlen (str2);
len3 = strlen (str3);


printf ("String lenght 1 : %d \n", len1);
printf ("String lenght 2 : %d\n", len2);
printf ("String lenght 3 : %d\n", len3);


for (i = 0; i < len1; i++){
if (str1[i] == 't'){
t1 = t1 + 1;
}
}


for (i = 0; i < len2; i++){
if (str2[i] == 't'){
t2 = t2 + 1;
}
}


for (i = 0; i < len3; i++){
if (str3[i] == 't'){
t3 = t3 + 1;
}
}


printf ("String 1 has %d t's\n", t1);
printf ("String 2 has %d t's\n", t2);
printf ("String 3 has %d t's\n", t3);


return 0;
}