PPC18 Lab Manual
PPC18 Lab Manual
Laboratory Schedule:
All users are charged a minimum of Rs. 100 as meter charge. If the total amount
is more than Rs 400, then an additional surcharge of 15% of total amount is
charged
4. Creating and Running C Programs on Repetition or Loops: 2hr
• C-Program to print a number series from 1 to a user-specified limit in the form
of a angle
• C-Program to print the number and sum of digits in an integer.
• C-Program to calculate the factorial of a number using for loop/ Recursion
• C-Program to calculate nth Fibonacci number.
• C-Program to convert binary to a decimal number
5. Creating and Running C Programs on One Dimensional Arrays: 2hr
• C-Program to print square of index and print it.
• C-Program to calculate average of the number in an array.
• C-Program to sort the list using bubble sort.
• C-Program to search an ordered list using binary search.
6. Creating and Running C Programs on Two Dimensional Arrays: 2hr
• C-Program to perform addition of two matrices.
• C-Program to perform multiplication of two matrices.
• C-Program to find transpose of the given matrices.
• C-Program to find row sum and column sum and sum of all elements in a
matrix.
• C-Program initialize/fill all the diagonal elements of a matrix with zero and
print
7. Creating and Running C Programs on User Defined Functions: 2hr
• C-program to read a number, Find its factorial using function with argument
and with return type.
• C-Program to read two number, Find its GCD and LCM using function with
arguments and without return type.
• C-Program to read a number, Find whether it is a palindrome or not using
function without argument and with return type.
• C-Program to read a number, Find whether it is prime number or not using
function without arguments and without return type.
8. Creating and Running C Programs on Strings: 2hr
• C-program read two strings, Combine them without using string built-in
functions.
• C-program read two strings, Compare them without using string built-in
functions.
• C-program read two strings, concatenate them without using string built-in
functions.
• C Program to Check if the Substring is Present in the Given String.
• C-program to demonstrate built-in sting functions like strlen(), strcpy(),
strcmp(), strcat()
Revision as per format MSRIT. F702 Rev. No. 2
Programs
1.Creating and Running Simple C Programs:
• C-Program to calculate the sum of three numbers / C-Program to demonstrate a Simple
Calculator.
• C-Program to calculate the area and circumference of a circle using PI as a defined constant.
• C-Program to convert temperature given in Celsius to Fahrenheit and Fahrenheit to Celsius
• C-Program to compute the roots of a quadratic equation by accepting the coefficients.
// Calculating sum
sum = a + b + c;
// Displaying output
printf("Sum = %d \n", sum);
return 0;
}
Output:
Enter 3 numbers:
567
Sum = 18
Average = 6.00
Revision as per format MSRIT. F702 Rev. No. 2
int main() {
char operator;
double num1, num2, result;
switch (operator) {
case '+':
result = num1 + num2;
printf("Result: %.2lf\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2lf\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2lf\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2lf\n", result);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Error! Invalid operator.\n");
}
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Enter an operator (+, -, *, /): +
Enter two operands: 5 6
Result: 11.00
#include <stdio.h>
#define PI 3.14159
int main() {
double radius, area, circumference;
return 0;
}
Output:
Enter the radius of the circle: 6
Area of the circle: 113.10
Circumference of the circle: 37.70
Revision as per format MSRIT. F702 Rev. No. 2
#include <stdio.h>
int main() {
char choice;
double temperature;
printf("Choose conversion:\n");
printf("a. Celsius to Fahrenheit\n");
printf("b. Fahrenheit to Celsius\n");
printf("Enter your choice (a/b): ");
scanf(" %c", &choice);
switch (choice) {
case 'a':
case 'A':
printf("%.2lf Celsius is equal to %.2lf Fahrenheit.\n", temperature,
celsiusToFahrenheit(temperature));
break;
case 'b':
case 'B':
printf("%.2lf Fahrenheit is equal to %.2lf Celsius.\n", temperature,
fahrenheitToCelsius(temperature));
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Enter temperature: 34
Choose conversion:
a. Celsius to Fahrenheit
b. Fahrenheit to Celsius
Enter your choice (a/b): a
34.00 Celsius is equal to 93.20 Fahrenheit.
#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
// Calculate quotient
quotient = dividend / divisor;
// Calculate remainder
remainder = dividend % divisor;
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Enter dividend: 7
Enter divisor: 3
Quotient: 2
Remainder: 1
typedef struct {
double real;
double imag;
} Complex;
int main() {
Complex num1, num2, sum, product;
return 0;
}
Output:
Enter real and imaginary parts of first complex number: 2
+ 3j
Enter real and imaginary parts of second complex number: Sum: 5.00 + 0.00 i
Product: 6.00 + 0.00 i
int main() {
// Automatic type conversion (promotion)
int a = 10;
double b = 3.5;
printf("\nType Casting:\n");
printf("x (double) = %.2lf\n", x);
printf("y (int) = %d\n", y);
return 0;
}
Output:
Automatic Type Conversion:
a + b = 13.50
Type Casting:
x (double) = 20.70
y (int) = 20
Revision as per format MSRIT. F702 Rev. No. 2
C-Program to calculate the total sales given the unit price, quantity, discount and tax rate
#include <stdio.h>
int main() {
double unitPrice, quantity, discount, taxRate;
double totalSales;
return 0;
}
Output:
Enter unit price: 25
Enter quantity: 2
Enter discount (as a percentage): 10
Enter tax rate (as a percentage): 2
Total Sales: $45.90
Revision as per format MSRIT. F702 Rev. No. 2
C-Program to calculate a student’s average score for a course with 4 quizzes, 2 midterms
and a final. The quizzes are weighted 30%, the midterms 40% and the final 30%.
#include <stdio.h>
int main() {
double quiz1, quiz2, quiz3, quiz4, midterm1, midterm2, finalExam;
double quizWeight = 0.3, midtermWeight = 0.4, finalWeight = 0.3;
double totalScore;
printf("Quiz 2: ");
scanf("%lf", &quiz2);
printf("Quiz 3: ");
scanf("%lf", &quiz3);
printf("Quiz 4: ");
scanf("%lf", &quiz4);
printf("Midterm 2: ");
scanf("%lf", &midterm2);
return 0;
Revision as per format MSRIT. F702 Rev. No. 2
Output:
All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more than Rs
400, then an additional surcharge of 15% of total amount is charged
C-Program to determine the use of the character classification functions found in c-type
library.
#include <stdio.h>
#include <ctype.h>
int main() {
char ch;
else
printf("%c is not a digit.\n", ch);
return 0;
}
Output:
Enter a character: t
t is not a digit.
t is an alphabetic character.
t is not a whitespace character.
t is not an uppercase letter.
t is a lowercase letter.
Revision as per format MSRIT. F702 Rev. No. 2
C-Program to read a test score, calculate the grade for the score and print the grade.
#include <stdio.h>
int main() {
int score;
return 0;
}
Output:
Enter the test score (out of 100): 98
Grade for the test score 98 is: A
Revision as per format MSRIT. F702 Rev. No. 2
C-Program to uses a menu to allow the user to add, multiply, subtracts and divides two
numbers using switch case
#include <stdio.h>
int main() {
int choice;
double num1, num2, result;
printf("Menu:\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
result = num1 + num2;
printf("Result of addition: %.2lf\n", result);
break;
case 2:
result = num1 - num2;
printf("Result of subtraction: %.2lf\n", result);
break;
case 3:
result = num1 * num2;
printf("Result of multiplication: %.2lf\n", result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
printf("Result of division: %.2lf\n", result);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Invalid choice!\n");
}
Revision as per format MSRIT. F702 Rev. No. 2
return 0;
}
Output:
Enter first number: 4
Enter second number: 6
Menu:
1. Add
2. Subtract
3. Multiply
4. Divide
Enter your choice: 1
Result of addition: 10.00
C-Program to read the name of the user, number of units consumed and print out the
charges. An electricity board charges the following rates for the use of electricity:
For the first 200 units 80 paise per unit
For the next 100 units 90 paise per unit
Beyond 300 units Rs 1 per unit.
#include <stdio.h>
int main() {
char name[100];
int units;
double totalCharge;
return 0;
}
Output:
Enter your name: nithin
Enter the number of units consumed: 45
Hello, nithin!
Total charges for 45 units: Rs 36.00
C-Program to print a number series from 1 to a user-specified limit in the form of a angle
#include <stdio.h>
int main() {
int limit;
return 0;
}
Output:
Enter the limit: 6
1 degrees
2 degrees
3 degrees
4 degrees
5 degrees
6 degrees
Revision as per format MSRIT. F702 Rev. No. 2
#include <stdio.h>
int main() {
int num, originalNum, sum = 0, remainder;
originalNum = num;
while (num != 0) {
remainder = num % 10;
sum += remainder;
num /= 10;
}
return 0;
}
Output:
Enter an integer: 345
Sum of digits of 345 = 12
#include <stdio.h>
int main() {
int num;
unsigned long long factorial = 1;
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Enter a non-negative integer: 5
Factorial of 5 = 120
int main() {
int n, i, a = 0, b = 1, next;
return 0;
}
Output:
Enter the number of terms: 4
Fibonacci Series: 0, 1, 1, 2,
Revision as per format MSRIT. F702 Rev. No. 2
return decimal;
}
int main() {
int binary;
return 0;
}
Output:
Enter a binary number: 1001
Decimal equivalent: 9
Revision as per format MSRIT. F702 Rev. No. 2
int main() {
int n;
return 0;
}
Output:
Enter the number of elements: 4
Square of index 0: 0
Square of index 1: 1
Square of index 2: 4
Square of index 3: 9
#include <stdio.h>
int main() {
int n;
double sum = 0.0;
int arr[n];
return 0;
}
Output:
Enter the number of elements: 5
Enter the elements:
34567
Average: 5.00
int arr[n];
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output:
Enter the number of elements: 5
Enter the elements:
31256
Sorted array:
12356
if (arr[mid] == key)
return mid;
return -1;
}
int main() {
int n, key;
int arr[n];
if (result == -1)
printf("Element not found.\n");
else
printf("Element found at index %d.\n", result);
return 0;
}
Output:
void addMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows, int cols)
{
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
int main() {
int mat1[10][10], mat2[10][10], result[10][10];
int rows, cols;
return 0;
}
Output:
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the first matrix:
2222
Enter elements of the second matrix:
2222
Sum of the matrices:
44
44
Revision as per format MSRIT. F702 Rev. No. 2
void multiplyMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows1, int cols1,
int rows2, int cols2) {
for (int i = 0; i < rows1; i++) {
for (int j = 0; j < cols2; j++) {
result[i][j] = 0;
for (int k = 0; k < cols1; k++) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}
int main() {
int mat1[10][10], mat2[10][10], result[10][10];
int rows1, cols1, rows2, cols2;
return 0;
}
Output:
Enter the number of rows for matrix 1: 2
Enter the number of columns for matrix 1: 2
Enter elements of matrix 1:
2222
Enter the number of rows for matrix 2: 2
Enter the number of columns for matrix 2: 2
Enter elements of matrix 2:
2222
Result of matrix multiplication:
88
88
Revision as per format MSRIT. F702 Rev. No. 2
#include <stdio.h>
int main() {
int mat[10][10], transpose[10][10];
int rows, cols;
return 0;
}
Output:
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the matrix:
2345
Original Matrix:
23
45
C-Program to find row sum and column sum and sum of all elements in a matrix
#include <stdio.h>
#include <stdio.h>
int main() {
int mat[10][10];
int rows, cols;
// Calculate and display row sum, column sum, and total sum
printf("\n\nCalculating Sums...\n");
calculateSums(mat, rows, cols);
return 0;
}
Output:
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the matrix:
2 2 22
Original Matrix:
22
22
Calculating Sums...
Row sums:
Row 1: 4
Row 2: 4
Column sums:
Column 1: 4
Column 2: 4
C-Program initialize/fill all the diagonal elements of a matrix with zero and print
#include <stdio.h>
void fillDiagonalWithZero(int mat[10][10], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i == j) {
mat[i][j] = 0;
}
}
}
}
int main() {
int mat[10][10];
int rows, cols;
return 0;
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Enter the number of rows: 2
Enter the number of columns: 2
Enter elements of the matrix:
2222
Matrix with diagonal elements as zero:
02
20
C-program to read a number, Find its factorial using function with argument and with
return type.
#include <stdio.h>
unsigned long long factorial(int num) {
if (num == 0 || num == 1)
return 1;
else
return num * factorial(num - 1);
}
int main() {
int num;
if (num < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
unsigned long long result = factorial(num);
printf("Factorial of %d = %llu\n", num, result);
}
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Enter a non-negative integer: 6
Factorial of 6 = 720
C-Program to read two number, Find its GCD and LCM using function with
arguments and without return type.
#include <stdio.h>
// Find GCD
for (i = 1; i <= max; i++) {
if (num1 % i == 0 && num2 % i == 0)
*gcd = i;
}
// Find LCM
*lcm = (num1 * num2) / *gcd;
}
int main() {
int num1, num2, gcd, lcm;
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Enter first number: 4
Enter second number: 6
GCD of 4 and 6 is: 2
LCM of 4 and 6 is: 12
#include <stdio.h>
int isPalindrome() {
int num, reversedNum = 0, originalNum;
originalNum = num;
// Check if the original number and the reversed number are the same
if (originalNum == reversedNum)
return 1; // The number is a palindrome
else
return 0; // The number is not a palindrome
}
int main() {
int result;
result = isPalindrome();
if (result)
printf("The number is a palindrome.\n");
else
printf("The number is not a palindrome.\n");
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Enter a number: 121
The number is a palindrome.
C-Program to read a number, Find whether it is prime number or not using function
without arguments and without return type.
#include <stdio.h>
void checkPrime() {
int num, i, flag = 0;
if (flag == 0)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
}
int main() {
checkPrime();
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Enter a number: 4
4 is not a prime number.
Enter a number: 5
5 is a prime number.
C-program read two strings, Combine them without using string built-in functions.
#include <stdio.h>
int main() {
char str1[100], str2[100], combined[200];
scanf("%s", str2);
return 0;
}
Output:
Enter the first string: hello
Enter the second string: good
Combined string: hellogood
C-program read two strings, Compare them without using string built-in functions
#include <stdio.h>
int main() {
char str1[100], str2[100];
if (result == 0) {
printf("Strings are equal.\n");
} else if (result < 0) {
Revision as per format MSRIT. F702 Rev. No. 2
return 0;
}
Output:
Enter the first string: hello
Enter the second string: good
First string is greater than the second string.
int main()
{
char str[80], search[10];
int count1 = 0, count2 = 0, i, j, flag;
printf("Enter a string:");
gets(str);
printf("Enter search substring:");
gets(search);
while (str[count1] != '\0')
count1++;
while (search[count2] != '\0')
count2++;
for (i = 0; i <= count1 - count2; i++)
{
for (j = i; j < i + count2; j++)
{
flag = 1;
if (str[j] != search[j - i])
{
flag = 0;
break;
}
}
if (flag == 1)
break;
}
if (flag == 1)
printf("SEARCH SUCCESSFUL!");
else
Revision as per format MSRIT. F702 Rev. No. 2
printf("SEARCH UNSUCCESSFUL!");
return 0;
}
Output
Enter a string:HELLO HI
Enter search substring:HI
SEARCH SUCCESSFUL!
C-program to demonstrate built-in sting functions like strlen(), strcpy(), strcmp(), strcat()
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
// Demonstrate strlen()
printf("Enter a string: ");
scanf("%s", str1);
printf("Length of the string: %lu\n", strlen(str1));
// Demonstrate strcpy()
strcpy(str2, str1);
printf("Copied string using strcpy(): %s\n", str2);
// Demonstrate strcmp()
printf("Enter another string: ");
scanf("%s", str2);
int cmpResult = strcmp(str1, str2);
if (cmpResult == 0)
printf("Strings are equal.\n");
else if (cmpResult < 0)
printf("First string is less than the second string.\n");
else
printf("First string is greater than the second string.\n");
// Demonstrate strcat()
strcat(str1, str2);
printf("Concatenated string using strcat(): %s\n", str1);
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
/tmp/iEaAHSxUVB.o
Enter a string: hello
Length of the string: 5
Copied string using strcpy(): hello
Enter another string: good
First string is greater than the second string.
Concatenated string using strcat(): hellogood
#include <stdio.h>
void autoExample() {
auto int x = 10; // auto variable
void staticExample() {
static int y = 20; // static variable
int main() {
printf("Inside main function:\n");
autoExample();
Revision as per format MSRIT. F702 Rev. No. 2
staticExample();
autoExample();
staticExample();
return 0;
}
Output:
Inside main function:
Auto variable: 10
Modified auto variable: 11
Static variable: 20
Modified static variable: 21
Again inside main function:
Auto variable: 10
Modified auto variable: 11
Static variable: 21
Modified static variable: 22
C-program to add two numbers using pointers. / C-program to swap two numbers using
pointers
#include <stdio.h>
int main() {
int num1, num2, sum;
return 0;
}
Output
Enter first number: 5
Enter second number: 6
Sum: 5 + 6 = 11
#include <stdio.h>
int main() {
int num1, num2;
return 0;
}
Output:
C-program to show how the same pointer can point to different data variable
#include <stdio.h>
int main() {
int a = 10, b = 20;
int *ptr;
return 0;
}
Output:
Value at ptr: 10
Value at ptr: 20
C-program to show the use of different pointers point to the same variable
#include <stdio.h>
int main() {
int x = 10;
int *ptr1, *ptr2;
ptr1 = &x; // ptr1 points to variable x
ptr2 = &x; // ptr2 also points to variable x
return 0;
}
Output:
Value at ptr1: 10
Value at ptr2: 10
Revision as per format MSRIT. F702 Rev. No. 2
#include <stdio.h>
int main() {
int arr[100], n, sum = 0;
return 0;
}
Output:
Enter the number of elements (max 100): 7
Enter elements of the array:
Enter element 1: 4
Enter element 2: 6
Enter element 3: 7
Enter element 4: 8
Enter element 5: 9
Enter element 6: 2
Enter element 7: 1
Sum of the array elements: 37
Revision as per format MSRIT. F702 Rev. No. 2
#include <stdio.h>
int main() {
char tvStations[10][30] = {
"CNN",
"BBC News",
"ESPN",
"Discovery Channel",
"National Geographic",
"Cartoon Network",
"HBO",
"FOX News",
"CNBC",
"MTV"
};
return 0;
}
Output:
TV Stations for Cable TV Systems:
1. CNN
2. BBC News
3. ESPN
4. Discovery Channel
5. National Geographic
6. Cartoon Network
7. HBO
8. FOX News
9. CNBC
10. MTV
Revision as per format MSRIT. F702 Rev. No. 2
union SampleUnion {
short int s;
char c[2];
};
int main() {
union SampleUnion u;
return 0;
}
Output:
Enter a short integer: 345
Short int: 345
Characters (as bytes):
First char: Y
Second char:
C Program to read employee details (name, salary, address) and print the same
using structure
#include <stdio.h>
};
int main() {
struct Employee emp;
return 0;
}
Output:
Enter employee details:
Enter name: nithin
Enter salary: 456789
Enter address: mathikere
Employee Details:
Name: nithin
Salary: 456789.00
Address: mathikere
Revision as per format MSRIT. F702 Rev. No. 2
C-Program to read marks of three students in 3 subjects. Calculate the total marks
scored, student wise and subject wise using structure.
#include <stdio.h>
#define NUM_STUDENTS 3
#define NUM_SUBJECTS 3
int main() {
struct Student students[NUM_STUDENTS] = {{0}, {0}, {0}};
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Enter marks for Student 1:
Enter marks for Subject 1: 56
Enter marks for Subject 2: 78
Enter marks for Subject 3: 89
Enter marks for Student 2:
Enter marks for Subject 1: 87
Enter marks for Subject 2: 98
Enter marks for Subject 3: 79
Enter marks for Student 3:
Enter marks for Subject 1: 89
Enter marks for Subject 2: 67
Enter marks for Subject 3: 87
Total marks scored by each student:
Student 1 Total Marks: 223
Student 2 Total Marks: 264
Student 3 Total Marks: 243
#include <stdio.h>
typedef struct {
char name[50];
int age;
} Person;
int main() {
FILE *file;
Person person;
if (file == NULL) {
Revision as per format MSRIT. F702 Rev. No. 2
if (file == NULL) {
printf("Could not open the file for reading.\n");
return 1;
}
if (file == NULL) {
printf("Could not open the file for reading.\n");
return 1;
}
return 0;
}
Revision as per format MSRIT. F702 Rev. No. 2
Output:
Reading data using fread():
Name: Alice, Age: 30
Could not open the file for reading.
#include <stdio.h>
typedef struct {
char name[50];
int age;
} Person;
int main() {
FILE *binaryFile, *textFile;
Person persons[] = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}};
if (binaryFile == NULL) {
printf("Could not open the binary file for writing.\n");
return 1;
}
if (textFile == NULL) {
printf("Could not open the text file for writing.\n");
return 1;
}
return 0;
}
Output:
Data written to files successfully.