0% found this document useful (0 votes)
80 views12 pages

Lab Manual for Programming Exercises

The document is a lab manual for students at the City College of Management and Science, outlining mandatory items to bring to the lab, attendance requirements, and a dress code. It includes a series of programming exercises in C, covering various topics such as arithmetic operations, string manipulation, and array handling. Each exercise is accompanied by example code to guide students in completing their lab assignments.

Uploaded by

ccmasacademic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views12 pages

Lab Manual for Programming Exercises

The document is a lab manual for students at the City College of Management and Science, outlining mandatory items to bring to the lab, attendance requirements, and a dress code. It includes a series of programming exercises in C, covering various topics such as arithmetic operations, string manipulation, and array handling. Each exercise is accompanied by example code to guide students in completing their lab assignments.

Uploaded by

ccmasacademic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

CITY COLLEGE OF MANAGEMENT AND SCIENCE

SAI PRIYA NAGAR, 3RD LANE, RAYAGADA-765001

LAB MANUAL

LAB NAME: ____________________________________

LABCODE_______________________________________________
YEAR__________________ SEMESTER_______________

Name of Faculty __________________________________


INSTRUCTIONS TO STUDENTS:
Before entering the lab the student should carry the following things (MANDATORY)
1. Identity card issued by the college.
2. Class notes
3. Lab observation book
4. Lab Manual
5. Lab Record
 Student must sign in and sign out in the register provided when attending the lab session
without fail.
 Come to the laboratory in time. Students, who are late more than 15 min., will not be
allowed to attend the lab.
 Students need to maintain 100% attendance in lab if not a strict action will be taken.
 All students must follow a Dress Code while in the laboratory
 All bags must be left at the indicated place.
 Refer to the lab staff if you need any help in using the lab.
 Respect the laboratory and its other users.
 Workspace must be kept clean and tidy after experiment is completed.
 Read the Manual carefully before coming to the laboratory and be sure about what you are
supposed to do. Do the experiments as per the instructions given in the manual.
 Copy all the programs to observation which are taught in class before attending the lab
session.
 Students are not supposed to use floppy disks, pen drives without permission of lab- in
charge.
 Lab records need to be submitted on or before the date of submission.
CONTENTS:
1. Write a Program to find greatest among three numbers.
2. Write a Program to all arithmetic operation using switch case.
3. Write a Program to print the sum and product of digits of an integer.
4. Write a Program to reverse a number.
5. Write a Program to compute the sum of the first n terms of the following series S = 1+1/2+1/3+1/4+……
6. Write a Program to compute the sum of the first n terms of the following series S =1-2+3-4+5…………….
7. Write a function that checks whether a given string is Palindrome or not. Use this function to find whether the
string entered by user is Palindrome or not.
8. Write a function to find whether a given no. is prime or not. Use the same to generate the prime numbers less
than 100.
9. Write a Program to compute the factors of a given number.
10. Write a program to swap two numbers using macro.
11. Write a Program to print a triangle of stars as follows (take number of lines from user):
*
***
*****
*******
12. Write a Program to perform following actions on an array entered by the user:
13. Print the even-valued elements.
14. Print the odd-valued elements.
15. Calculate and print the sum and average of the elements of array.
16. Print the maximum and minimum element of array.
17. Remove the duplicates from the array.
18. Print the array in reverse order.
19. The program should present a menu to the user and ask for one of the options. The menu should also include
options to re-enter array and to quit the program.
20. Write a Program that prints a table indicating the number of occurrences of each alphabet in the text entered as
command line arguments.
21. Write a program that swaps two numbers using pointers.
22. Write a program in which a function is passed address of two variables and then alter its contents.
23. Write a program which takes the radius of a circle as input from the user, passes it to another function that
computes the area and the circumference of the circle and displays the value of area and circumference from the
main( ) function.
24. Write a program to find sum and average of n elements entered by the user. To write this program, allocate
memory dynamically using malloc ( ) / calloc( ) functions.
25. Write a menu driven program to perform following operations on strings:
26. Show address of each character in string.
27. Concatenate two strings without using strcat function.
28. Concatenate two strings using strcat function.
29. Compare two strings.
30. Calculate length of the string (use pointers).
31. Convert all lowercase characters to uppercase.
32. Convert all uppercase characters to lowercase.
33. Calculate number of vowels.
34. Reverse the string.
35. Given two ordered arrays of integers, write a program to merge the two-arrays to get an ordered array.
36. Write a program to copy the content of one file to other.
[Link] a c program to find greatest number among three numbers
#include <stdio.h>

int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

if (a >= b && a >= c)


printf("%d is the greatest number.\n", a);
else if (b >= a && b >= c)
printf("%d is the greatest number.\n", b);
else
printf("%d is the greatest number.\n", c);

return 0;
}

[Link] to Perform All Arithmetic Operations Using Switch Case


#include <stdio.h>

int main() {
int a, b, choice;
float result;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

printf("\nSelect operation:\n");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch(choice) {
case 1:
result = a + b;
printf("Result = %.2f\n", result);
break;
case 2:
result = a - b;
printf("Result = %.2f\n", result);
break;
case 3:
result = a * b;
printf("Result = %.2f\n", result);
break;
case 4:
if (b != 0)
result = (float)a / b;
else {
printf("Division by zero is not allowed.\n");
return 0;
}
printf("Result = %.2f\n", result);
break;
default:
printf("Invalid choice!\n");
}

return 0;
}
3. Program to Print Sum and Product of Digits of an Integer
#include <stdio.h>

int main() {
int num, digit;
int sum = 0, product = 1;

printf("Enter an integer: ");


scanf("%d", &num);

while (num != 0) {
digit = num % 10;
sum += digit;
product *= digit;
num /= 10;
}

printf("Sum of digits = %d\n", sum);


printf("Product of digits = %d\n", product);

return 0;
}
4 .Program to Reverse a Number
#include <stdio.h>

int main() {
int num, reversed = 0, remainder;

printf("Enter a number: ");


scanf("%d", &num);

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

printf("Reversed number = %d\n", reversed);

return 0;
}

5. Program to compute the sum of the first n terms of the series S = 1 + 1/2 + 1/3 + 1/4 + …
#include <stdio.h>

int main() {
int n, i;
float sum = 0.0;

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


scanf("%d", &n);

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


sum += 1.0 / i;
}

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


return 0;
}

6. Program to compute the sum of the series S = 1 - 2 + 3 - 4 + 5 - 6 + …


#include <stdio.h>

int main() {
int n, i, sum = 0;

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


scanf("%d", &n);

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


if(i % 2 == 0)
sum -= i;
else
sum += i;
}

printf("Sum of the series = %d\n", sum);


return 0;
}
7. Program to check whether a given string is Palindrome or not (using function)
#include <stdio.h>
#include <string.h>

int isPalindrome(char str[]) {


int i, len = strlen(str);
for(i = 0; i < len / 2; i++) {
if(str[i] != str[len - i - 1])
return 0; // Not palindrome
}
return 1; // Palindrome
}

int main() {
char str[100];

printf("Enter a string: ");


scanf("%s", str);

if(isPalindrome(str))
printf("%s is a Palindrome.\n", str);
else
printf("%s is not a Palindrome.\n", str);

return 0;
}

8. Program to check whether a number is prime and generate prime numbers less than 100
#include <stdio.h>

int isPrime(int n) {
if(n <= 1)
return 0;
for(int i = 2; i * i <= n; i++) {
if(n % i == 0)
return 0;
}
return 1;
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if(isPrime(num))
printf("%d is a Prime number.\n", num);
else
printf("%d is not a Prime number.\n", num);

printf("\nPrime numbers less than 100 are:\n");


for(int i = 2; i < 100; i++) {
if(isPrime(i))
printf("%d ", i);
}
printf("\n");
return 0;
}
9. Program to compute the factors of a given number
#include <stdio.h>

int main() {
int n, i;
printf("Enter a number: ");
scanf("%d", &n);

printf("Factors of %d are: ", n);


for(i = 1; i <= n; i++) {
if(n % i == 0)
printf("%d ", i);
}
printf("\n");
return 0;
}

10. Program to swap two numbers using a macro


#include <stdio.h>

#define SWAP(a, b) { int temp = a; a = b; b = temp; }

int main() {
int x, y;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);

printf("Before swapping: x = %d, y = %d\n", x, y);


SWAP(x, y);
printf("After swapping: x = %d, y = %d\n", x, y);

return 0;
}

11. Program to print a triangle of stars


Pattern:
*
***
*****
*******

#include <stdio.h>

int main() {
int n, i, j, k;

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


scanf("%d", &n);

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


for(j = 1; j <= n - i; j++)
printf(" "); // For spacing (optional)
for(k = 1; k <= 2 * i - 1; k++)
printf("*");
printf("\n");
}

return 0;
}
12. Program to perform multiple operations on an array (menu-driven)
#include <stdio.h>

#define MAX 100

void printEven(int arr[], int n) {


printf("Even elements: ");
for(int i = 0; i < n; i++)
if(arr[i] % 2 == 0)
printf("%d ", arr[i]);
printf("\n");
}

void printOdd(int arr[], int n) {


printf("Odd elements: ");
for(int i = 0; i < n; i++)
if(arr[i] % 2 != 0)
printf("%d ", arr[i]);
printf("\n");
}

void sumAvg(int arr[], int n) {


int sum = 0;
for(int i = 0; i < n; i++)
sum += arr[i];
printf("Sum = %d, Average = %.2f\n", sum, (float)sum / n);
}

void maxMin(int arr[], int n) {


int max = arr[0], min = arr[0];
for(int i = 1; i < n; i++) {
if(arr[i] > max) max = arr[i];
if(arr[i] < min) min = arr[i];
}
printf("Maximum = %d, Minimum = %d\n", max, min);
}

int removeDuplicates(int arr[], int n) {


int i, j, k;
for(i = 0; i < n; i++) {
for(j = i + 1; j < n; j++) {
if(arr[i] == arr[j]) {
for(k = j; k < n - 1; k++)
arr[k] = arr[k + 1];
n--;
j--;
}
}
}
return n;
}

void printReverse(int arr[], int n) {


printf("Array in reverse: ");
for(int i = n - 1; i >= 0; i--)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[MAX], n, choice;

printf("Enter number of elements: ");


scanf("%d", &n);
printf("Enter %d elements: ", n);
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);

while(1) {
printf("\n------ MENU ------\n");
printf("1. Print even elements\n");
printf("2. Print odd elements\n");
printf("3. Sum and average\n");
printf("4. Maximum and minimum\n");
printf("5. Remove duplicates\n");
printf("6. Print reverse\n");
printf("7. Re-enter array\n");
printf("8. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch(choice) {
case 1: printEven(arr, n); break;
case 2: printOdd(arr, n); break;
case 3: sumAvg(arr, n); break;
case 4: maxMin(arr, n); break;
case 5: n = removeDuplicates(arr, n);
printf("Duplicates removed. Updated array: ");
for(int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
break;
case 6: printReverse(arr, n); break;
case 7:
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements: ", n);
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);
break;
case 8: printf("Exiting program.\n"); return 0;
default: printf("Invalid choice!\n");
}
}

return 0;
}

13. Program to count occurrences of each alphabet in command line arguments


#include <stdio.h>
#include <ctype.h> // for tolower()

int main(int argc, char *argv[]) {


int count[26] = {0}; // To store frequency of each alphabet
char ch;

// Loop through each command line argument


for (int i = 1; i < argc; i++) {
for (int j = 0; argv[i][j] != '\0'; j++) {
ch = tolower(argv[i][j]); // Convert to lowercase
if (ch >= 'a' && ch <= 'z')
count[ch - 'a']++; // Increase count for the letter
}
}

// Display the frequency table


printf("Alphabet frequency count:\n");
for (int i = 0; i < 26; i++) {
printf("%c : %d\n", i + 'a', count[i]);
}

return 0;
}
14. Program to swap two numbers using pointers

#include <stdio.h>

int main() {
int a, b, *p1, *p2, temp;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

p1 = &a;
p2 = &b;

temp = *p1;
*p1 = *p2;
*p2 = temp;

printf("After swapping: a = %d, b = %d\n", a, b);

return 0;
}

15. Program in which a function is passed the address of two variables and alters their contents

#include <stdio.h>

void alter(int *x, int *y) {


*x = *x + 10;
*y = *y + 20;
}

int main() {
int a, b;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

printf("Before altering: a = %d, b = %d\n", a, b);

alter(&a, &b);

printf("After altering: a = %d, b = %d\n", a, b);

return 0;
}

16. Program to compute area and circumference of a circle using function

#include <stdio.h>

#define PI 3.1416

void compute(float r, float *area, float *circumference) {


*area = PI * r * r;
*circumference = 2 * PI * r;
}

int main() {
float radius, area, circumference;

printf("Enter the radius of the circle: ");


scanf("%f", &radius);

compute(radius, &area, &circumference);


printf("Area = %.2f\n", area);
printf("Circumference = %.2f\n", circumference);

return 0;
}

17. Program to find sum and average of n elements using dynamic memory allocation (malloc/calloc)

#include <stdio.h>
#include <stdlib.h>

int main() {
int n, i;
float *arr, sum = 0.0, avg;

printf("Enter number of elements: ");


scanf("%d", &n);

// Allocate memory dynamically using malloc


arr = (float *)malloc(n * sizeof(float));

if(arr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}

printf("Enter %d elements: ", n);


for(i = 0; i < n; i++) {
scanf("%f", &arr[i]);
sum += arr[i];
}

avg = sum / n;

printf("Sum = %.2f\n", sum);


printf("Average = %.2f\n", avg);

free(arr); // Free allocated memory

return 0;
}

You might also like