Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
104 views

C Programming Lab Report

The document contains 6 exercises for a C/C++ programming lab. The exercises cover topics like reading integers and classifying them as positive or negative, classifying student grades, calculating series sums, calculating factorials using iterations, converting binary to decimal, and implementing a guessing game to find a randomly generated number.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
104 views

C Programming Lab Report

The document contains 6 exercises for a C/C++ programming lab. The exercises cover topics like reading integers and classifying them as positive or negative, classifying student grades, calculating series sums, calculating factorials using iterations, converting binary to decimal, and implementing a guessing game to find a randomly generated number.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

International University

School of Computer Science and Engineering

C/C++ Programming
IT116IU

Lab Title

Submitted by
Đỗ Phú Thành – ITITWE22129
Nguyễn Trung Anh – ITITWE22122
Trần Võ Thế Vinh – ITITWE22124

Date Performed: 28/3/32024


Date Submitted: 28/3/32024
Lab Section: Lab 2
Course Instructor: M. Eng Nguyen Minh Thien

Exercise 1

Write a C program that reads 10 integers. The program should count the number of
positive and negative values among 10 inputs. Print them on the screen. (Use iteration
statements). Zero is neither a positive or a negative number.

Zero is neither a positive or a negative number.


Output:

Enter number 1: 16
Enter number 2: -7
Enter number 3: 23
Enter number 4: 49
Enter number 5: -21
Enter number 6: -15
Enter number 7: -33
Enter number 8: 54
Enter number 9: 42
Enter number 10: -37
Number of positive: 5
Number of negative: 5

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

int a, positive = 0, negative = 0;

for (int i = 1; i <= 10; i++) {


printf("Enter number %d: ", i);
scanf("%d", &a);

if (a > 0) { positive++;
}else if (a < 0) {negative++;
}
}

printf("Positive numbers count: %d\n", positive);


printf("Negative numbers count: %d\n", negative);

return 0;
}

Exercise 2
Write a C program that reads a student grade which is a character. The program should print the classification based
on the grade as:
Grade Classification
A Excellent
B Good
C Fair
D Average
F Weak
Other characters Invalid

After showing the Classification, the program should ask the user to continue to read another grade or not. by
choosing yes (‘y’) or no (‘n’). If yes, continues, otherwise, prints “Exit program...”.
Hints: Use switch Multiple selection
Output:

Enter a grade: B
Good!
Do you want to continue? y
Enter a grade: Y
Invalid. Enter a grade: D
Average!
Do you want to continue? n
Exit program...

#include <stdio.h>

int main() {
char grade, choice;

do {
printf("Enter student's grade: ");
scanf(" %c", &grade);

switch (grade) {
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Good\n");
break;
case 'C':
printf("Fair\n");
break;
case 'D':
printf("Average\n");
break;
case 'F':
printf("Weak\n");
break;

printf("Invalid grade\n");
}

printf("Do you want to continue (y/n)? ");


scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');

printf("Exit program...\n");

return 0;
}

Exercise 3
Write a C program to calculate the sum of series:
x-x^3+x^5-x^7…
The user should input the value for x and the number of terms
For Example
Output:
Input the value of x: 2
Input number of terms: 5
The sum of series: 410
#include <stdio.h>
#include <math.h>

int main() {
float x, sum = 0;
int n;

printf("x: ");
scanf("%f", &x);

printf("terms: ");
scanf("%d", &n);

for (int i = 1; i <= n; i++) {


int sign = (i % 2 == 1) ? 1 : -1;
float term = sign * pow(x, 2 * i - 1);
sum += term;
}

printf("Sum of the series: %.2f\n", sum);

return 0;
}

Exercise 4
Write a C program to calculate the factorial of a positive number.
The factorial can be described: n!=1×2×3…n
Note: Do not use recursion function. Use iterations instead,
Output:
Enter a positive integer: 4
Factorial of 4= 24

#include <stdio.h>

int main() {

int num, factorial = 1;

printf("Enter a positive integer: ");

scanf("%d", &num);

if (num < 0) {

printf("Error: Please enter a positive integer.\n");

return 1;

for (int i = 1; i <= num; ++i) {

factorial *= i;

printf("Factorial of %d = %d\n", num, factorial);

return 0;

}
Exercise 5
Write a C program to convert a Binary number to Decimal number using iterations. Program should allow
user to try again or to stop the program.
Output:
Enter binary value: 1010
Decimal value: 10

#include<stdio.h>
#include<math.h>
int main()
{
long number;
int dec = 0, rem = 0, place = 0;
printf("Enter the Binary Value\n");
scanf("%ld",&number );
printf("Decimal Equivalent of %ld is ",number);
while(number)
{
rem = number % 10;
dec = dec + rem * (pow( 2, place));
number= number / 10;
place++;
}
printf("%d\n",dec);
return 0;
}

Exercise 5
Write a C program to convert a Binary number to Decimal number using iterations. Program should allow
user to try again or to stop the program.
Output:
Enter binary value: 1010
Decimal value: 10
#include<stdio.h>
#include<math.h>
int main()
{
long number;
int dec = 0, rem = 0, place = 0;
printf("Enter the Binary Value\n");
scanf("%ld",&number );
printf("Decimal Equivalent of %ld is ",number);
while(number)
{
rem = number % 10;
dec = dec + rem * (pow( 2, place));
number= number / 10;
place++;
}
printf("%d\n",dec);
return 0;
}

Exercise 6
Write a C program that Program to generate a “mystery” number by the computer, that is a random
number from 1 to 100. Then, the user will guess this number by prompting a value. There are three
possible responses:
1. Excellent! You guessed the number!
2. Too low. Try again...
3. Too high. Try again...
The game ends when the user guesses the correct number. After that, let the user decide to replay or not
by choosing yes (‘y’) or no (‘n’)
Hint: Include stdlib.h and time.h. Use the following codes to generate a random number:
srand(time(0)); // random time moment
guess =rand()%100 +1 ; // generate a random number in the range [0-100]

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main() {
int guess, numberToGuess, rant = 0;
srand(time(0));
numberToGuess = rand() % 100 + 1;
while (guess != numberToGuess) {
printf("Guess from 1 to 100: ");
scanf("%d", &guess);
if (guess < numberToGuess) {
printf("Too low. Try again...\n");
} else if (guess > numberToGuess) {
printf("Too high. Try again...\n");
}
}
printf("Congratulations! You guessed the number!\n");
char c;
printf("Do you want to play again? (y/n): ");
scanf(" %c", &c);
if (c == 'y'|| c == 'Y') {
main();
} else {
printf("Game end here....\n");
}

return 0;
}
Bonus Exercise

Write a C program that Program to calculate the sin( x) which is described in series as belows:

3 5 7
( ) x x x
sin x =x− + − +… where x is angle∈radian
3! 5! 7!

The user should input the value for x and the number of terms

Tips: take a reference from Exercise 3 and 4

#include <stdio.h>

#include <math.h>

float sin_taylor(float x, int n) {

float sum = x, term = x;

int sign = 1;
for (int i = 3; i <= n; i += 2) {

term *= -x * x / ((i - 1) * i);

sum += sign * term;

sign *= -1;

return sum;

int main() {

float x, approx_sin;

int n;

printf("Enter the value for x: ");

scanf("%f", &x);

printf("Enter the number of terms: ");

scanf("%d", &n);

approx_sin = sin_taylor(x, n);

printf("The value of sin(%lf) = %f\n", x, approx_sin);

return 0;

You might also like