Skip to content

Commit

Permalink
feat: projects
Browse files Browse the repository at this point in the history
  • Loading branch information
ADAMU MUHAMMAD MUHAMMAD committed Jan 18, 2024
1 parent ee704ec commit f3ba2c6
Show file tree
Hide file tree
Showing 14 changed files with 1,258 additions and 452 deletions.
320 changes: 32 additions & 288 deletions docs/C/ass.mdx
Original file line number Diff line number Diff line change
@@ -1,56 +1,41 @@
---
sidebar_position: 4
sidebar_position: 2
---

# Assignments

# project 1

## Algorithm:
- Get User Input:
Display a prompt asking the user to enter the first number.
Read the user input and store it in the variable num1.
Display a prompt asking the user to enter an operator (+, -, *, /).
Read the user input and store it in the variable operator with a space before %c to consume any whitespace characters.
Display a prompt asking the user to enter the second number.
Read the user input and store it in the variable num2.

- Perform Arithmetic Operations:
Use a switch statement to check the entered operator and perform the corresponding arithmetic operation.
For addition (+), add num1 and num2.
For subtraction (-), subtract num2 from num1.
For multiplication (*), multiply num1 and num2.
For division (/), check if num2 is not zero, then divide num1 by num2.
If num2 is zero, display an error message, and exit the program with an error code.
For an invalid operator, display an error message, and exit the program with an error code.
Store the result in the variable result.

- Display Result:
Display the result of the arithmetic operation with two decimal places.

- Exit Successfully:
Return 0 to indicate successful program execution.

## Program Flow
- User Interaction
User is prompted to enter the first number.
User enters the first number.
User is prompted to enter an operator (+, -, *, /).
User enters the operator.
User is prompted to enter the second number.
User enters the second number.

- Arithmetic Operations:
The program uses a switch statement to identify the operator and perform the corresponding arithmetic operation.
If the operator is invalid or division by zero is attempted, an error message is displayed, and the program exits with an error code.
Otherwise, the result of the arithmetic operation is calculated and stored in the variable result.

- Display Result:
The program displays the result of the arithmetic operation with two decimal places.
## Algorithm

- Exit Successfully:
The program exits with a return code of 0, indicating successful execution.
This program allows the user to perform basic arithmetic operations on two numbers based on user input. It includes error checking for invalid operators and division by zero.
1. Start
2. Input num1
3. Input operator
4. Input num2
5. Switch on operator
- Case '+': result = num1 + num2
- Case '-': result = num1 - num2
- Case '*': result = num1 * num2
- Case '/':
- If num2 is not equal to 0, result = num1 / num2
- Else, print an error message and return 1
- Default: Print an error message and return 1
6. Print the result: num1, operator, num2, result
7. End

## Flowchart

```mermaid
flowchart TD
A([Start]) --> B[/input num1 /]
B --> C[/ operations /]
C --> D[/ input num2 /]
D --> E{ +, -, *, / ?}
E --> G[ Sum ] --> J[/ Result /]
E --> F[ multiple ] --> J[/ Result /]
E --> H[ subtract ] --> J[/ Result /]
E --> I[ Divide ] --> J[/ Result /]
E -->|invalid| K([End])
J --> K
```

```c
#include <stdio.h>
Expand Down Expand Up @@ -98,247 +83,6 @@ int main() {

return 0; // Exit successfully
}

```

# project 2

## Algorithm

- step 1 Prompt User for Number of Courses:
Display a prompt asking the user to enter the number of courses.
Read the user input and store it in the variable numCourses.

- step 2 Validate Number of Courses:
Check if numCourses is less than or equal to 0.
If true, display an error message and exit the program with an error code.

- step 3 Initialize Variables:
Initialize totalCredits and totalGradePoints to 0.0.

- step 4 Loop for Each Course:
Use a for loop to iterate from 1 to numCourses.

- step 5 Inside the loop:
Display a prompt for course details (course number).
Read and store user input for course credits in the variable credits.
Read and store user input for course grade in the variable grade.

- step 6 Validate Grade:
Check if grade is outside the valid range of 0 to 4 (on a 4.0 scale).
If true, display an error message and exit the program with an error code.

- step 7 Update Total Credits and Grade Points:
Add credits to totalCredits.
Add the product of credits and grade to totalGradePoints.

- step 8 Calculate GPA:
Calculate the GPA by dividing totalGradePoints by totalCredits. Store the result in the variable gpa.

- step 9 Display GPA:
Display the calculated GPA with two decimal places.

- step 10 Exit Successfully:
Return 0 to indicate successful program execution.

## Program Flow:

- User Interaction:
User is prompted to enter the number of courses.
User enters the number of courses.

- Validation:
The program checks if the entered number of courses is valid (greater than 0).
If not, an error message is displayed, and the program exits with an error code.

- Initialization:
Variables totalCredits and totalGradePoints are initialized to 0.0.

- Loop for Each Course:
The program enters a loop to gather information for each course.

- User Interaction for Each Course:
User is prompted to enter credits for the current course.
User enters credits for the current course.
User is prompted to enter the grade for the current course.
User enters the grade for the current course.

- Grade Validation:
The program checks if the entered grade is within the valid range (0 to 4).
If not, an error message is displayed, and the program exits with an error code.

- Update Total Credits and Grade Points:
The program updates the cumulative total credits and grade points for all courses.

- Calculate GPA:
The program calculates the GPA based on the accumulated total credits and grade points.

- Display GPA:
The program displays the calculated GPA.

- Exit Successfully:
The program exits with a return code of 0, indicating successful execution.
This program allows a user to input information for multiple courses and calculates the GPA based on the entered data.

## Code
```c
#include <stdio.h>

double calculateGradePoints(double grade) // function protype
int main() {
int numCourses;
printf("Enter the number of courses: ");
scanf("%d", &numCourses);

// Ensure a valid number of courses is entered
if (numCourses <= 0) {
printf("Error: Please enter a valid number of courses.\n");
return 1; // Exit with an error code
}

double totalCredits = 0.0;
double totalGradePoints = 0.0;

for (int i = 1; i <= numCourses; ++i) {
double credits, grade;
printf("\nEnter details for Course %d:\n", i);

// Get user input for course credits
printf("Enter credits for Course %d: ", i);
scanf("%lf", &credits);

// Get user input for course grade
printf("Enter grade for Course %d: ", i);
scanf("%lf", &grade);

// Validate the grade entered
if (grade < 0 || grade > 100) {
printf("Error: Please enter a valid grade between 0 and 100.\n");
return 1; // Exit with an error code
}

// Calculate grade points based on the provided scale
double gradePoints = calculateGradePoints(grade);

// Update total credits and grade points
totalCredits += credits;
totalGradePoints += credits * gradePoints;
}

// Calculate GPA
double gpa = totalGradePoints / totalCredits;

// Display the GPA
printf("\nYour GPA is: %.2lf\n", gpa);

return 0; // Exit successfully
}
// function defination
double calculateGradePoints(double grade) {
if (grade >= 70) {
return 5.0;
} else if (grade >= 60) {
return 4.0;
} else if (grade >= 50) {
return 3.0;
} else if (grade >= 45) {
return 2.0;
} else if (grade >= 40) {
return 1.0;
} else {
return 0.0;
}
}
```
# project 3
## Algorithm
- Get User Input:
Display a menu for temperature conversion options.
Prompt the user to choose between Celsius to Fahrenheit (1) or Fahrenheit to Celsius (2).
Read the user's choice and store it in the variable choice.
Prompt the user to enter the temperature for conversion.
Read the user input for temperature and store it in the variable temperature.
- Perform Temperature Conversion:
Use a switch statement to check the user's choice and perform the corresponding temperature conversion.
If the choice is 1, call the celsiusToFahrenheit function and display the result.
If the choice is 2, call the fahrenheitToCelsius function and display the result.
If the choice is neither 1 nor 2, display an error message and exit the program with an error code.
- Display Result:
Display the result of the temperature conversion with two decimal places.
- Exit Successfully:
Return 0 to indicate successful program execution.
## Program Flow:
- User Interaction:
User is presented with a menu for temperature conversion options.
User is prompted to choose between Celsius to Fahrenheit (1) or Fahrenheit to Celsius (2).
User enters the choice.
User is prompted to enter the temperature for conversion.
User enters the temperature.
- Temperature Conversion:
The program uses a switch statement to identify the user's choice.
If the choice is 1, the program calls the celsiusToFahrenheit function, passing the entered temperature, and displays the result.
If the choice is 2, the program calls the fahrenheitToCelsius function, passing the entered temperature, and displays the result.
If the choice is neither 1 nor 2, an error message is displayed, and the program exits with an error code.
- Display Result:
The program displays the result of the temperature conversion with two decimal places.
- Exit Successfully:
The program exits with a return code of 0, indicating successful execution.
```c
#include <stdio.h>
// Function to convert Celsius to Fahrenheit
double celsiusToFahrenheit(double celsius) {
return (celsius * 9 / 5) + 32;
}
// Function to convert Fahrenheit to Celsius
double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
int main() {
int choice;
double temperature;
// Get user's choice
printf("Choose conversion:\n");
printf("1. Celsius to Fahrenheit\n");
printf("2. Fahrenheit to Celsius\n");
printf("Enter choice (1 or 2): ");
scanf("%d", &choice);
// Get temperature from user
printf("Enter temperature: ");
scanf("%lf", &temperature);
// Perform conversion based on user's choice
switch (choice) {
case 1:
printf("%.2lf Celsius is equal to %.2lf Fahrenheit\n", temperature, celsiusToFahrenheit(temperature));
break;
case 2:
printf("%.2lf Fahrenheit is equal to %.2lf Celsius\n", temperature, fahrenheitToCelsius(temperature));
break;
default:
printf("Error: Invalid choice. Please enter 1 or 2.\n");
return 1; // Exit with an error code
}
return 0; // Exit successfully
}
```


Loading

0 comments on commit f3ba2c6

Please sign in to comment.