SPL Lab Report3
SPL Lab Report3
Experiment Number : 01
Experiment Name : Write a c program to find average of the marks
Algorithm:
1. Start
2. Declare Marks `a`, `b`, `c`, and `avg`.
3. Read three numbers from the user.
4. Calculate the average: `avg = (a + b + c) / 3`.
5. Print the average.
6. Stop
Flowchart
2
Code
#include <stdio.h>
int main() {
// Declare marks
int a, b, c, avg;
return 0;
}
Output
Experiment Number : 02
Experiment Name : Write a c program to make a simple calculator
Algorithm:
1. Start
2. Declare variables
3. Read two numbers from the user
4. Compute the results
5. Print the results
6. End
Flowchart
4
Code
#include <stdio.h>
int main() {
// Declare variables
int a, b, w, x, y, z;
// Read two numbers from the user
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
// Compute the results
w = a + b;
x = a - b;
y = a * b;
z = a / b;
// Print the results
printf("The sum of %d and %d is %d\n", a, b, w);
printf("The difference of %d and %d is %d\n", a, b, x);
printf("The product of %d and %d is %d\n", a, b, y);
printf("The quotient of %d and %d is %d\n", a, b, z);
return 0;
}
Output
Enter two numbers: 10 5
The sum of 10 and 5 is 15
The difference of 10 and 5 is 5
The product of 10 and 5 is 50
5
Experiment Number : 03
Experiment Name : Write a c program to a simple calculator using
switch case
Algorithm:
1. Start
2. Read two numbers from the user.
3. Display the menu.
4. Get the user's choice.
5. Switch on the user's choice.
* Case '+': Add the two numbers and print the sum.
* Case '-': Subtract the two numbers and print the difference.
* Case '*': Multiply the two numbers and print the product.
* Case '/': Divide the two numbers and print the quotient.
* Case 'E': Exit the program.
6. Go to step 2 if the user wants to do another calculation.
7. Stop.
Flowchart
6
Code
#include <stdio.h>
int main() {
// Declare variables
int a, b;
char choice;
// Read two numbers from the user
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
// Display the menu
printf("Select operation:\n");
printf("+: Addition\n");
printf("-: Subtraction\n");
printf("*: Multiplication\n");
printf("/: Division\n");
printf("E: Exit\n");
// Get the user's choice
printf("Enter your choice: ");
scanf(" %c", &choice);
// Switch statement to perform the operation
switch (choice) {
case '+':
printf("Sum = %d\n", a + b);
break;
case '-':
printf("Difference = %d\n", a - b);
7
break;
case '*':
printf("Product = %d\n", a * b);
break;
case '/':
printf("Division = %d\n", a / b);
break;
case 'E':
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
Output
Enter two numbers: 10 20
Sum = 30
Enter two numbers: 10 5
Difference = 5
Enter two numbers: 10 0
Product = 0
8
Experiment Number : 04
Experiment Name : Write a c program to convert ASCII code to ASCII
charactor
Algorithm:
1. Start.
2. Prompt the user to enter an ASCII code.
3. Read the ASCII code from the user.
4. Convert the ASCII code to a character.
5. Check if the ASCII code is in the range 10 to 127.
If the ASCII code is in the range, print the character.
Otherwise, print a message saying that the ASCII code is not in the range.
6. End.
Flowchart
9
Code
#include <stdio.h>
int main() {
int ascii_code;
char character;
printf("Enter an ASCII code: ");
scanf("%d", &ascii_code);
// Convert ASCII code to character.
character = ascii_code;
// Check if the ASCII code is in the range 10 to 127.
if (ascii_code >= 10 && ascii_code <= 127) {
printf("The character is '%c'.\n", character);
} else {
printf("The ASCII code is not in the range 10 to 127.\n");
}
return 0;
}
Output
Enter an ASCII code: 65
The character is 'A'.
Enter an ASCII code: 128
The ASCII code is not in the range 10 to 127.
Enter an ASCII code: 0
10
Experiment Number : 05
Experiment Name : Write a c program to convert ASCII charactor to
ASCII code
Algorithm:
Start.
1.Input a character.
2.Convert the character to ASCII code.
3.Check if the character is valid.
4.If the character is a digit ('0' to '9'), print its ASCII code.
5.If the character is an uppercase letter ('A' to 'Z'), print its ASCII code.
6.If the character is a lowercase letter ('a' to 'z'), print its ASCII code.
7.Otherwise, print a message saying that the character is invalid.
8.End
Flowchart
11
Code
#include <stdio.h>
int main() {
char ch;
int ascii;
printf("Enter a character: ");
scanf("%c", &ch);
// Convert the character to ASCII code.
ascii = ch;
// Check if the character is valid.
if (ascii >= '0' && ascii <= '9') {
printf("The ASCII code of '%c' is %d.\n", ch, ascii);
} else if (ascii >= 'A' && ascii <= 'Z') {
printf("The ASCII code of '%c' is %d.\n", ch, ascii);
} else if (ascii >= 'a' && ascii <= 'z') {
printf("The ASCII code of '%c' is %d.\n", ch, ascii);
} else {
printf("Invalid character.\n");
}
return 0;
}
output
Enter a character: a
The ASCII code of 'a' is 97.
12
Experiment Number : 06
Experiment Name : Write a c program to check number is positive
nature and negative .
Algorithm:
1. Start.
2. Prompt the user to enter a number.
3. Read the number from the user.
4. Check if the number is positive.
If the number is positive, print "The number is positive."
Otherwise, print "The number is negative."
5. End
Flowchart
13
code
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is positive.
if (number >= 0) {
printf("The number is positive.\n");
} else {
// Check if the number is negative.
if (number < 0) {
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
}
return 0;
}
Output
Enter a number: 10
The number is positive.
Enter a number: -10
The number is negative. Enter a number: 0 The number is zero.
14
Experiment Number : 07
Experiment Name : Write a c program to check number is positive
nature and negative .
Algorithm:
1. Start
2. Display a menu of shapes:
markdown
Choose a shape to calculate its area: 1. Triangle 2. Circle 3. Square 4. Rectangle
3. Prompt the user to enter their choice of shape (1-4) and read the choice
into the variable choice.
4. Use a switch-case statement based on the value of choice:
• If choice is 1:
• Prompt the user to enter the base and height of the triangle.
• Read the values of base and height.
• Calculate the area of the triangle using the formula: area = 0.5
* base * height.
• If choice is 2:
• Prompt the user to enter the radius of the circle.
• Read the value of radius.
• Calculate the area of the circle using the formula: area =
3.14159 * radius * radius.
• If choice is 3:
• Prompt the user to enter the side length of the square.
• Read the value of side.
15
• Calculate the area of the square using the formula: area = side
* side.
• If choice is 4:
• Prompt the user to enter the length and width of the
rectangle.
• Read the values of length and width.
• Calculate the area of the rectangle using the formula: area =
length * width.
• If none of the above:
• Display "Invalid choice".
• Exit the program with an error code.
5. Display the calculated area using the format string "The area is: %.2f\n".
6. End
Flowchart
Code
#include <stdio.h>
int main() {
int choice;
float area;
scanf("%d", &choice);
switch (choice) {
case 1:
float base, height;
printf("Enter the base and height of the triangle: ");
scanf("%f %f", &base, &height);
Output
area = 0.5 * base * height;
break; Choose a shape to
case 2: calculate its area:
float radius;
1. Triangle
printf("Enter the radius of the circle: ");
scanf("%f", &radius); 2. Circle
area = 3.14159 * radius * radius; 3. Square
break;
case 3: 4. Rectangle
float side; Enter your choice (1-4): 2
printf("Enter the side length of the square: ");
Enter the radius of the
scanf("%f", &side);
circle: 5
area = side * side;
break; The area is: 78.54
case 4:
float length, width;
printf("Enter the length and width of the rectangle: ");
scanf("%f %f", &length, &width);
area = length * width;
break;
default:
printf("Invalid choice\n");
return 1; // Exit program with an error code
}
Experiment Number : 08
Experiment Name : Write a c program to check if a given number is
alphabet
Algorithm:
1. Start.
2. Declare a variable `sing_ch` of type `char`.
3. Prompt the user to enter a character and read it using the `scanf()` function.
4. Check if the character is an alphabet.
* If yes, print the message "This is an alphabet."
* If no, continue to the next step.
5. Check if the character is a digit.
* If yes, print the message "This is a digit."
* If no, print the message "This is a special character."
6. Stop.
Flowchart
18
Code
#include <stdio.h>
int main() {
char sing_ch;
printf("Input a character: ");
scanf("%c", &sing_ch);
// Check if the character is an alphabet.
if ((sing_ch >= 'a' && sing_ch <= 'z') ||
(sing_ch >= 'A' && sing_ch <= 'Z')) {
printf("This is an alphabet.\n");
} else {
// Check if the character is a digit.
if (sing_ch >= '0' && sing_ch <= '9') {
printf("This is a digit.\n");
} else {
// The character is a special character.
printf("This is a special character.\n");
}
}
return 0;
}
Output
Input: a
This is an alphabet.
Input: 0 This is a digit. Input: @ This is a special character.
19
Experiment Number : 09
Experiment Name : Write a c program to find the maximum of three
number.
Algorithm:
1. Start.
Code
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c) {
printf("The largest number is %d.\n", a);
} else {
printf("The largest number is %d.\n", c);
}
} else {
if (b > c) {
printf("The largest number is %d.\n", b);
} else {
printf("The largest number is %d.\n", c);
}
}
return 0;
}
Output
Enter two numbers: 1 5
The larger number is 5.
21
Experiment Number : 10
Experiment Name : Write a c program to find the maximum of four
number.
Algorithm:
1. Start
2. Declare four integer variables: num1, num2, num3, and num4.
3. Display a prompt asking the user to enter four numbers.
4. Read and store the values of num1, num2, num3, and num4.
5. Declare an integer variable max and initialize it with the value of num1.
6. Compare the value of num2 with the current value of max.
• If num2 is greater than max, update the value of max to num2.
7. Compare the value of num3 with the current value of max.
• If num3 is greater than max, update the value of max to num3.
8. Compare the value of num4 with the current value of max.
• If num4 is greater than max, update the value of max to num4.
9. Display the maximum value max.
10.End
22
Flowchart
23
Code
#include <stdio.h>
int main() {
int num1, num2, num3, num4;
printf("Enter four numbers: ");
scanf("%d %d %d %d", &num1, &num2, &num3, &num4);
int max = num1;
if (num2 > max) {
max = num2;
}
if (num3 > max) {
max = num3;
}
if (num4 > max) {
max = num4;
}
printf("The maximum number is: %d\n", max);
return 0;
}
Output
Enter four numbers: 15 27 9 34
The maximum number is: 34
24
Experiment Number : 11
Experiment Name : Write a c program to check if given number is even or
odd
Algorithm:
1. Start.
2. Declare a variable `number` of type `int`.
3. Prompt the user to enter a number and read it using the `scanf()` function.
4. Check if the number is even.
* If yes, print the message "Even Number".
* If no, print the message "Odd Number".
5. Stop.
Flowchart:
25
Code
#include <stdio.h>
int main() {
int number;
printf("Input a number: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("Even Number\n");
} else {
printf("Odd Number\n");
}
return 0;
}
Output
Input: 2
Even Number
Input: 11
Odd Number
Input: 0
Even Number
26
Experiment Number : 12
Experiment Name : Write a c program to find exponent
Algorithm:
1. Start.
2. Declare two variables, `base` and `power`, to store the base and power of the
exponentiation.
3. Prompt the user to enter the base and power and read them using the `scanf()` function.
4. Initialize a variable `result` to 1.
5. Use a for loop to iterate from 0 to `power`.
* In each iteration, multiply `result` by `base`.
6. Print the result.
7. Stop.
Flowchart:
27
Code
#include <stdio.h>
int main() {
int base, power;
printf("Enter base: ");
scanf("%d", &base);
printf("Enter power: ");
scanf("%d", &power);
// Calculate the exponentiation.
int result = 1;
for (int i = 0; i < power; i++) {
result *= base;
}
// Print the result.
printf("The exponentiation is: %d\n", result);
return 0;
}
Output
Input: base = 2, power = 3
The exponentiation is: 8
Input: base = 3, power = 4
The exponentiation is: 81
Input: base = 4, power = 0
The exponentiation is: 1
28
Experiment Number : 13
Experiment Name : Write a c program to find the factorial of a given
number
Algorithm
1. Start.
2. Declare three variables, `n`, `i`, and `fact`.
3. Prompt the user to enter the number and read it using the `scanf()` function.
4. Initialize `fact` to 1.
5. Use a for loop to iterate from 1 to `n`.
* In each iteration, multiply `fact` by `i`.
6. Print the factorial of `n`.
7. Stop.
flowchart
29
Code
#include <stdio.h>
int main() {
int n;
int i, fact = 1;
// Initialize variable n.
printf("Enter a number: ");
scanf("%d", &n);
// Calculate factorial of n.
for (i = 1; i <= n; i++) {
fact = fact * i;
}
// Print factorial of n.
printf("Factorial of %d is: %d\n", n, fact);
return 0;
}
Output
Input: n = 5
Factorial of 5 is: 120
Input: n = 3
Factorial of 3 is: 6
Input: n = 1
Factorial of 1 is: 1
30
Experiment Number : 14
Experiment Name : Write a c program to find the fdibonacci serise
Algorithm:
1.Start.
2. Declare three variables, `n`, `a`, and `b`.
3. Prompt the user to enter the number of terms and read it using the `scanf()`
function.
4. Initialize `a` to 0 and `b` to 1.
5. Print `a` and `b`.
6. Use a for loop to iterate from 2 to `n`.
* In each iteration, calculate the next term, `c`, as `a + b`.
* Print `c`.
* Update `a` and `b` to `b` and `c`, respectively.
7. Stop.
flowchart
31
Code
#include <stdio.h>
int main() {
int n;
int a = 0, b = 1, c;
printf("Enter the number of terms: ");
scanf("%d", &n);
// Print the first two terms.
printf("%d %d ", a, b);
// Calculate and print the remaining terms.
for (int i = 2; i <= n; i++) {
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
return 0;
}
Output
Input: n = 5
01123
Input: n = 10
0 1 1 2 3 5 8 13 21 34
Input: n = 1
0
32
Experiment Number : 15
Experiment Name : Write a c program to check the leap year
Algorithm
1. Start
2. Read the year from the user
3. Check if the year is divisible by 4
* If yes, go to step 4
* Otherwise, go to step 6
4. Check if the year is divisible by 100
* If yes, check if the year is divisible by 400
* If yes, the year is a leap year
* Otherwise, the year is not a leap year
* Otherwise, the year is a leap year
5. Print the result
6. Stop
Flowchart
33
Code
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// Check if the year is divisible by 4
if (year % 4 == 0) {
// Check if the year is divisible by 100 but not 400
if (year % 100 != 0 || year % 400 == 0) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
Output
Enter a year: 2023
2023 is not a leap year. Enter a year: 2024 , 2024 is a leap year.
34
Experiment Number : 16
Experiment Name : Write a c program to check if a number is prime
Algorithm
1.start
2. Declare variables `n`, `i`, and `check`.
3. Read the number from the user.
4. Initialize `check` to 1.
5. Loop from 1 to `n - 1`:
* Check if `n` is divisible by `i`.
* If it is, set `check` to 0 and break out of the loop.
6. Check if `check` is equal to 1.
* If it is, print a message stating that `n` is a prime number.
* Otherwise, print a message stating that `n` is not a prime number.
7.End
Flowchart
35
Code
#include <stdio.h>
int main() {
// Declare variables
int n, i, check;
// Read the number from the user
printf("Enter a number: ");
scanf("%d", &n);
// Initialize check to 1
check = 1;
// Loop from 1 to n-1
for (i = 2; i <= n - 1; i++) {
// If n is divisible by i, set check to 0
if (n % i == 0) {
check = 0;
break;
}
}
// Check if n is prime
Output
if (check == 1) {
Input: 2
printf("%d is a prime number.\n", n);
2 is a prime number.
} else {
Input: 3
printf("%d is not a prime number.\n", n);
3 is a prime number.
}
Input: 4
return 0;
4 is not a prime number
}
36
Experiment Number : 17
Experiment Name : Write a c program to size of different data type
Algorithm
1. Start
2. Declare variables
* int num;
* float value;
* char character;
3. Declare and initialize the size of each variable
* int int_size = sizeof(num);
* float float_size = sizeof(value);
* char char_size = sizeof(character);
4. Print the size of each variable
* printf("The size of int is %d bytes.\n", int_size);
* printf("The size of float is %d bytes.\n", float_size);
* printf("The size of char is %d bytes.\n", char_size);
5. Stop
Flowchart
37
Code
#include <stdio.h>
int main() {
// Declare variables
int num;
float value;
char character;
// Declare and initialize the size of each variable
int int_size = sizeof(num);
float float_size = sizeof(value);
char char_size = sizeof(character);
// Print the size of each variable
printf("The size of int is %d bytes.\n", int_size);
printf("The size of float is %d bytes.\n", float_size);
printf("The size of char is %d bytes.\n", char_size);
return 0;
}
Output
The size of int is 4 bytes.
The size of float is 4 bytes.
The size of char is 1 byte.
38
Experiment Number : 18
Experiment Name : Write a c program to check two string are equal
Algorithm
1. Start
2. Declare strings
* char s1[100];
* char s2[100];
3. Read strings
4. Compare strings
* If the result is negative, the first string is less than the second string.
* If the result is positive, the first string is greater than the second string.
6. Stop
Flowchart
39
Code
#include <stdio.h>
int main() {
// Declare strings
char s1[100], s2[100];
// Read strings
printf("Enter string 1: ");
scanf("%s", s1);
printf("Enter string 2: ");
scanf("%s", s2);
// Compare strings
int result = strcmp(s1, s2);
// Print the result
if (result == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
return 0;
}
Output
Enter string 1: Hello
Enter string 2: World
Strings are not equal.
40
Experiment Number : 19
Experiment Name : Write a c program to copy one string to other
Algorithm
1. The #include <stdio.h> statement tells the compiler to include the standard
input/output header file, which contains functions such as printf() and gets().
2. The int main() function is the starting point of the program.
3. The char str1[100] and char str2[100] statements declare two character arrays of size
100.
4. The printf("Enter a string: "); statement prompts the user to enter a string.
5. The gets(str1); statement reads the string entered by the user and stores it in str1.
6. The for (int i = 0; str1[i] != '\0'; i++) statement is a for loop that iterates until the end of
the string str1 is reached. The str1[i] != '\0' condition checks if the current character
in str1 is the null character (\0). If it is, the loop terminates. Otherwise, the current
character is copied to str2[i].
7. The printf("The copied string is: %s\n", str2); statement displays the copied string
in str2.
8. The return 0; statement tells the compiler that the program has terminated successfully.
Flowchart
41
Code
#include <stdio.h>
int main() {
char str1[100], str2[100];
Output
Enter a string: Hello world!
The copied string is: Hello world!
42
Experiment Number : 20
Experiment Name : Write a c program to create a structure and access
structure members
Algorithm
1. Start
2. Define a structure named 'Person':
• name: string
• age: integer
• height: float
3. Declare a structure variable 'person1' of type 'Person'
4. Set 'person1.name' to "John"
5. Set 'person1.age' to 25
6. Set 'person1.height' to 5.9
7. Call the function 'accessMembers' with 'person1' as an argument
8. Within the 'accessMembers' function:
• Print "Person's Name: person1.name"
• Print "Person's Age: person1.age"
• Print "Person's Height: person1.height"
9. End the 'accessMembers' function
10. Print "Person's Name: person1.name"
11. Print "Person's Age: person1.age"
12. Print "Person's Height: person1.height"
13. End
43
flowchart
44
Code
#include <stdio.h>
// Define a structure named 'Person'
struct Person {
char name[50];
int age;
float height;
};
// Function to access structure members
void accessMembers(struct Person p) {
printf("Person's Name: %s\n", p.name);
printf("Person's Age: %d\n", p.age);
printf("Person's Height: %.2f\n", p.height);
}
int main() {
// Declare a structure variable of type 'Person'
struct Person person1;
// Populate the structure members
strcpy(person1.name, "John");
person1.age = 25;
person1.height = 5.9;
Output
// Access structure members using the function
Person's Name: John
accessMembers(person1);
Person's Age: 25
return 0;
Person's Height: 5.90
}
Person's Name: John
Person's Age: 25
Person's Height: 5.90
45
Experiment Number : 21
Experiment Name : Write a c program to create a nested structure and
access structure members
Algorithm
1. Create a structure named Address with three text fields: street, city, and
state.
2. Create a structure named Person with a name (text), age (number), and
address (instance of Address).
3. Inside main: 4. Create a Person named person1.
5. Set person1's name to "John Doe".
6. Set person1's age to 25.
7. Set person1's address: 8. Street: "123 Main St"
9. City: "Exampleville"
10.State: "CA"
8. Print person1's name and age.
9. Print person1's address: street, city, and state.
Flowchart
Code
#include <stdio.h>
// Define the inner structure
struct Address {
char street[50];
char city[50];
char state[20];};
// Define the outer structure
46
struct Person {
char name[50];
int age;
struct Address address; // Nested structure
};
int main() {
// Declare and initialize a structure
struct Person person1 = {
"John Doe",
25,
{"123 Main St", "Exampleville", "CA"}
};
// Access and print the members of the nested structure
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Address:\n");
printf(" Street: %s\n", person1.address.street);
printf(" City: %s\n", person1.address.city);
printf(" State: %s\n", person1.address.state);
return 0;
}
Output
Name: John Doe
Age: 25
Address: Street: 123 Main St City: ExamplevilleState: CA
47
Experiment Number : 22
Experiment Name : Write a c program to create a union and access union
members
Algorithm
1. Start
2. Declare a union data variable u
3. Assign the value 10 to u.i
4. Print the value of u.i
5. Assign the value 2.5 to u.f
6. Print the value of u.f
7. Assign the string "Hello, world!" to u.str
8. Print the value of u.str
9. Return 0
10. End
Flowchart
48
Code
#include <stdio.h>
union data {
int i;
float f;
char str[20];
};
int main() {
union data u;
u.i = 10;
printf("The value of u.i is: %d\n", u.i);
u.f = 2.5;
printf("The value of u.f is: %f\n", u.f);
strcpy(u.str, "Hello, world!");
printf("The value of u.str is: %s\n", u.str);
return 0;
}
Output
The value of u.i is: 10
The value of u.f is: 2.500000
The value of u.str is: Hello, world!
49
Experiment Number : 23
Experiment Name : Write a c program to illustrate use of “typedef”
Algorithm
1. Start
2. Declare a typedef for int named myInteger
3. Declare an integer variable num
4. Assign the value 42 to num
5. Print "Value: " followed by the value of num
6. End
code
#include <stdio.h>
// Define a typedef for int
typedef int myInteger;
int main() {
myInteger num = 42;
printf("Value: %d\n", num);
return 0;
}
Output
Value: 42