C Programming Lab Report
C Programming Lab Report
C/C++ Programming
IT116IU
Lab Title
Submitted by
Đỗ Phú Thành – ITITWE22129
Nguyễn Trung Anh – ITITWE22122
Trần Võ Thế Vinh – ITITWE22124
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.
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(){
if (a > 0) { positive++;
}else if (a < 0) {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("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);
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() {
scanf("%d", &num);
if (num < 0) {
return 1;
factorial *= i;
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
#include <stdio.h>
#include <math.h>
int sign = 1;
for (int i = 3; i <= n; i += 2) {
sign *= -1;
return sum;
int main() {
float x, approx_sin;
int n;
scanf("%f", &x);
scanf("%d", &n);
return 0;