[go: up one dir, main page]

0% found this document useful (0 votes)
6 views52 pages

POP Lab Programs

The document contains multiple C programming exercises, including a simple calculator, programs for basic arithmetic operations, quadratic equations, area and circumference calculations, and various control structures. It also includes examples of user input handling, conditional statements, loops, and functions, along with expected outputs for each program. The document serves as a comprehensive guide for beginners to practice and understand fundamental programming concepts in C.

Uploaded by

wachheomkar85
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)
6 views52 pages

POP Lab Programs

The document contains multiple C programming exercises, including a simple calculator, programs for basic arithmetic operations, quadratic equations, area and circumference calculations, and various control structures. It also includes examples of user input handling, conditional statements, loops, and functions, along with expected outputs for each program. The document serves as a comprehensive guide for beginners to practice and understand fundamental programming concepts in C.

Uploaded by

wachheomkar85
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
You are on page 1/ 52

1.

Write a C program for simulation of simple Calculator

#include <stdio.h>

int main() {

char operator;

double num1, num2, result;

// Input operator from user

printf("Enter operator (+, -, *, /): ");

scanf("%c", &operator);

// Input two numbers from user

printf("Enter two numbers: ");

scanf("%lf %lf", &num1, &num2);

// Perform calculation based on the operator

switch(operator) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

// Check if the second number is not zero


if (num2 != 0)

result = num1 / num2;

else {

printf("Error! Division by zero.\n");

return 1; // Exit program with error status

break;

default:

printf("Error! Invalid operator.\n");

return 1; // Exit program with error status

// Output the result

printf("Result: %.2lf\n", result);

return 0;

Expected Output:

Output 1:

Enter operator (+, -, *, /): +

Enter two numbers: 10

Result: 15.00

Output 2:

Enter operator (+, -, *, /): -

Enter two numbers: 10

5
Result: 5.00
Output 3:

Enter operator (+, -, *, /): *

Enter two numbers: 10

Result: 50.00

Output 4:

Enter operator (+, -, *, /): /

Enter two numbers: 10

Result: 2.00

1a.Write a C program to print the welcome to the world of C

#include <stdio.h>

int main()

printf(“\nWelcome to the world of C”);

return 0;

Expected output:

Welcome to the world of C

1b.Write a C program to find addition of 2 integers

#include <stdio.h>

int main()

{
int number1, number2, sum;

printf("\nEnter the First Number: ");

scanf("%d",&number1);

printf("\nEnter the Second Number: ");

scanf("%d",&number2);

sum = number1+number2;

printf(" %d + %d = %d", number1,number2, sum);

return 0;

Expected output:

Enter the First Number: 64

Enter the Second Number: 36

64 + 36 = 100

2.Write a C Program to Compute the roots of a quadratic equation by accepting the


coefficients. Print appropriate messages

#include <stdio.h>

#include <math.h>

int main()

double a, b, c;

double discriminant, root1, root2;

// Input the coefficients

printf("Enter the coefficients of the quadratic equation (a, b, c):\n ");


scanf("%lf %lf %lf", &a, &b, &c);

// Calculate the discriminant

discriminant = b * b - 4 * a * c;

// Check the discriminant for the nature of roots

if (discriminant > 0)

// Two real and distinct roots

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("The Roots are real\n");

printf("Root 1 = %lf\n", root1);

printf("Root 2 = %lf\n", root2);

else if (discriminant == 0)

// One real root (repeated)

root1 = -b / (2 * a);

printf("The Roots are equal\n");

printf("Root 1 = Root 2 = %lf\n", root1);

else

// Complex roots

double realPart = -b / (2 * a);


double imaginaryPart = sqrt(-discriminant) / (2 * a);

printf("The roots are imaginary\n");

printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart);

printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart);

return 0;

Expected Output:

Output 1:

Enter the coefficients of the quadratic equation (a, b, c):

The Roots are real

Root 1 = -1.000000

Root 2 = -3.000000

Output 2:

Enter the coefficients of the quadratic equation (a, b, c):

The Roots are equal

Root 1 = Root 2 = -1.000000

Output 3:

Enter the coefficients of the quadratic equation (a, b, c):

1
1

The roots are imaginary

Root 1 = -0.50 + 1.94i

Root 2 = -0.50 - 1.94i

2a.Write a C program to find area and circumference of circle

#include<stdio.h>

int main()

int rad;

float PI = 3.14, area, ci;

printf("\nEnter radius of circle: ");

scanf("%d", &rad);

area = PI * rad * rad;

printf("\nArea of circle : %f ", area);

ci = 2 * PI * rad;

printf("\nCircumference : %f ", ci);

return 0;

Expected Output:

Enter radius of circle: 5

Area of circle : 78.500000

Circumference : 31.400002

2b.Write a C program to calculate simple interest

#include <stdio.h>

int main()
{

float principal, rate, time, simple_interest;

// Input

printf("Enter principal amount: ");

scanf("%f", &principal);

printf("Enter rate of interest (in percentage): ");

scanf("%f", &rate);

printf("Enter time (in years): ");

scanf("%f", &time);

// Calculate Simple Interest

simple_interest = (principal * rate * time) / 100.0;

// Display Result

printf("Simple Interest: %.2f\n", simple_interest);

return 0;

Expected Output:

Enter principal amount: 1000

Enter rate of interest (in percentage): 5

Enter time (in years): 6

Simple Interest: 300.00

3. 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. 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. Write a
program to read the name of the user, number of units consumed and print out the charges

#include <stdio.h>

int main() {
// Variables

char name[50];

int units;

double totalAmount, surcharge = 0;

// Input

printf("Enter the name of the user: ");

scanf("%s", name);

printf("Enter the number of units consumed: ");

scanf("%d", &units);

// Calculate charges

if (units <= 200) {

totalAmount = 100 + 0.8 * units;

} else if (units <= 300) {

totalAmount = 100 + (0.8 * 200) + (0.9 * (units - 200));

} else {

totalAmount = 100 + (0.8 * 200) + (0.9 * 100) + (1.0 * (units - 300));

// Check for surcharge

if (totalAmount > 400) {

surcharge = 0.15 * totalAmount;

totalAmount += surcharge;

// Display charges

printf("\nElectricity Charges for %s:\n", name);


printf("Units Consumed: %d\n", units);

printf("Meter Charge: Rs. 100.00\n");

printf("Total Charges: Rs. %.2f\n", totalAmount);

printf("Surcharge: Rs. %.2f\n", surcharge);

return 0;

Expected Output:
Output 1:

Enter the name of the user: Anitha

Enter the number of units consumed: 0

Electricity Charges for Anitha:

Units Consumed: 0

Meter Charge: Rs. 100.00

Total Charges: Rs. 100.00

Surcharge: Rs. 0.00

Output 2:
Enter the name of the user: Anuradha
Enter the number of units consumed: 167
Electricity Charges for Anuradha:
Units Consumed: 167
Meter Charge: Rs. 100.00
Total Charges: Rs. 233.60
Surcharge: Rs. 0.00
Output 3:
Enter the name of the user: Rajeev
Enter the number of units consumed: 598
Electricity Charges for Rajeev:
Units Consumed: 598
Meter Charge: Rs. 100.00
Total Charges: Rs. 745.20
Surcharge: Rs. 97.20

3a. Write a C Program To Check the Given Character is Lowercase or Uppercase or Special
Character.

#include <stdio.h>

int main()

char ch;

printf("Enter a character: ");

scanf("%c", &ch);

// Check if the character is a lowercase letter

if (ch >= 'a' && ch <= 'z')

printf("%c is a lowercase letter.\n", ch);

// Check if the character is an uppercase letter

else if (ch >= 'A' && ch <= 'Z')

printf("%c is an uppercase letter.\n", ch);


}

// Check if the character is a special character

else

printf("%c is a special character.\n", ch);

return 0;

Expected Output:

Output 1:

Enter a character: a

a is a lowercase letter.

Output 2:

Enter a character: A

A is an uppercase letter.

Output 3:

Enter a character: &

& is a special Character.

3b. Write a C program to Check whether an integer is odd or even

#include <stdio.h>

int main()

{
int number;

printf("Enter an integer: ");

scanf("%d", &number);

// True if the remainder is 0

if (number%2 == 0)

printf("%d is an even integer.",number);

else

printf("%d is an odd integer.",number);

return 0;

Expected Output:

Output 1:

Enter an integer: 2

2 is an even integer.

Output 2:

Enter an integer: 7

7 is an odd integer.
4. Write a C Program to display the following by reading the number of rows as input,
1
121
12321
1234321
---------------------------
nth row
#include <stdio.h>

void main()
{
int i,j,n;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=0; i<=n; i++)
{
for(j=1; j<=n-i; j++) //print blank spaces
printf(" ");

for(j=1;j<=i; j++) //Display number in ascending order upto middle


printf("%d", j);

for(j=i-1;j>=1;j--) //Display number in reverse order after middle


printf("%d",j);

printf("\n");
}
}

Expected Output:
Input number of rows : 4

121

12321

1234321
4a. Write a C program to find largest of 3 numbers using ternary operator

#include <stdio.h>

int main()

int num1, num2, num3;

int largest;

printf("Enter three numbers: ");

scanf("%d %d %d", &num1, &num2, &num3);

largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) : ((num2 > num3) ? num2 : num3);

printf("The largest number is: %d\n", largest);

return 0;

Expected Output:

Enter three numbers:

The largest number is: 6


4b. Write a c program to determine whether an entered character is vowel or not

#include <stdio.h>

int main()

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

switch (ch)

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':

case 'A':

case 'E':

case 'I':

case 'O':

case 'U':

printf("%c is a vowel.\n", ch);

break;

default:

printf("%c is not a vowel.\n", ch);

break;
}

return 0;

Expected Output:

Output 1:

Enter a character: s

s is not a vowel.

Output 2:

Enter a character: e

e is a vowel.

5. Implement Binary Search on Integers.

#include <stdio.h>

int main()

int n, target;

printf("Enter the number of elements in the sorted array: ");

scanf("%d", &n);

int arr[n];

printf("Enter the sorted array elements:\n");

for (int i = 0; i < n; i++)

scanf("%d", &arr[i]);

printf("Enter the element to search: ");

scanf("%d", &target);
int left = 0;

int right = n - 1;

int index = -1; // Store the index of the element if found

while (left <= right)

int mid = left + (right - left) / 2;

if (arr[mid] == target)

index = mid;

break;

else if (arr[mid] < target)

left = mid + 1; // Search the right subarray

else

right = mid - 1; // Search the left subarray

if (index != -1)

printf("Element found at index %d\n", index);

else
{

printf("Element not found in the array.\n");

return 0;

Expected Output:

Output1:

Enter the number of elements in the sorted array: 3

Enter the sorted array elements:

Enter the element to search: 1

Element found at index 0

Output2:

Enter the number of elements in the sorted array: 3

Enter the sorted array elements:

Enter the element to search: 4

Element not found in the array.

5a. Write a C program to print Fibonacci series using recursion

#include <stdio.h>

// Function to calculate the nth Fibonacci number using recursion


int fibonacci(int n)

if (n <= 1)

return n;

else

return (fibonacci(n - 1) + fibonacci(n - 2));

// Function to display the Fibonacci series up to the nth term

void displayFibonacciSeries(int n)

for (int i = 0; i < n; i++)

printf("%d ", fibonacci(i));

printf("\n");

int main()

int n;

printf("Enter the number of terms in the Fibonacci series: ");

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

displayFibonacciSeries(n);

return 0;

Expected Output:

Enter the number of terms in the Fibonacci series: 10

Fibonacci Series: 0 1 1 2 3 5 8 13 21 34

6. Implement Matrix multiplication and validate the rules of multiplication.

#include<stdio.h>

#include<stdlib.h>

int main()

int m, n, p, q, i, j, k, a[10][10], b[10][10];

int c[10][10]={0};

printf("PROGRAMTOIMPLEMENTMATRIXMULTIPLICATION \n");

printf("\nEntertheOrderofMatrix1\n");

scanf("%d%d",&m, &n);

printf("\nEntertheOrderofMatrix2\n");

scanf("%d%d",&p,&q);

if(n!=p)

printf("Matrix Multiplication is not possible\n");exit(0);

printf("EntertheelementsofMatrix1\n");

for(i=0;i<m;i++)
for(j=0;j<n;j++)

scanf("%d",&a[i][j]);

printf("EntertheelementsofMatrix2\n");

for(i=0;i<p;i++)

for(j=0;j<q;j++)

scanf("%d", &b[i][j]);

for(i=0;i<m;i++)

for(j=0;j<q;j++)

for(k=0;k<n;k++)

c[i][j]+=a[i][k]*b[k][j];

printf("\nMatrix1\n");

for(i=0;i<m;i++)

for(j=0;j<n;j++)

printf("%d\t",a[i][j]);

printf("\n");

}
printf("\n");

printf("\nMatrix2\n");

for(i=0;i<p;i++)

for(j=0;j<q;j++)

printf("%d\t",b[i][j]);

printf("\n");

printf("\n");

printf("\nTheproductoftheMatrixis\n");

for(i=0;i<m;i++)

for(j=0;j<q;j++)

printf("%d\t",c[i][j]);

printf("\n");

printf("\n");

return 0;

Expected Output:

Output 1:
PROGRAMTOIMPLEMENTMATRIXMULTIPLICATION

EntertheOrderofMatrix1

EntertheOrderofMatrix2

EntertheelementsofMatrix1

EntertheelementsofMatrix2

10

11

12

Matrix1

1 2

3 4

5 6
Matrix2

7 8 9

10 11 12

The product of the Matrix is

27 30 33

61 68 75

95 106 117

Output 2:

PROGRAMTOIMPLEMENTMATRIXMULTIPLICATION

EntertheOrderofMatrix1

EntertheOrderofMatrix2

Matrix Multiplication is not possible

6a. Develop a C program to print the following pattern.

H
HE
HEL
HELL
HELLO
HELL
HEL
HE
H.

#include<stdio.h>

#include<stdlib.h>
int main()

// variables

char str[20];

int len, place;

// take input

printf("Enter a string: ");

scanf("%[^\n]", str);

// find length of string

for(len=0; str[len]!='\0'; len++);

// actual length is length - 1

len--;

// outer loop for row

for(int i=0; i<(2*len+1); i++)

// find the place

if(i<len) place = i;

else place = abs(2*len - i);

// inner loop for column

for(int j=0; j<=place; j++)

printf("%c",str[j]); // print

printf("\n"); // next line

return 0;
}

Expected Output:

Enter a string: hello

he

hel

hell

hello

hell

hel

he

6b.Write a C program to generate Pascal’s triangle.

#include <stdio.h>

// Function to calculate factorial of a number

int factorial(int n) {

if (n == 0 || n == 1) {

return 1;

} else {

return n * factorial(n - 1);

// Function to calculate binomial coefficient C(n, r)


int binomialCoefficient(int n, int r) {

return factorial(n) / (factorial(r) * factorial(n - r));

// Function to generate Pascal's Triangle

void generatePascalsTriangle(int numRows) {

for (int i = 0; i < numRows; i++) {

for (int j = 0; j <= i; j++) {

printf("%d ", binomialCoefficient(i, j));

printf("\n");

int main() {

int numRows;

// Input the number of rows for Pascal's Triangle

printf("Enter the number of rows for Pascal's Triangle: ");

scanf("%d", &numRows);

// Generate and print Pascal's Triangle

generatePascalsTriangle(numRows);
return 0;

Expected Output:

Enter the number of rows for Pascal's Triangle: 05

11

121

1331

14641

7 Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the
built-in library function. Print both the results with appropriate inferences.

#include<stdio.h>

#include<math.h>

#define PI 3.142857

int main()

float x,degree,nume,deno,sum,term;

int i;

printf("Enterdegree:");

scanf("%f",&degree);

x=degree*(PI/180.0);

sum=0;

nume=x;

deno=1.0;i=1;

do
{

term=nume/deno;

sum=sum+term;

i=i+2;

nume=-nume*x*x;

deno=deno*i*(i-1);

}while(fabs(term)>=0.00001);

printf("ComputedvalueofSin(%f)=%f\n",degree,sum);

printf ("Value from library function is sin(%f) = %f\n",degree,sin(x));

return 0;

Expected Output:

Enterdegree:60

ComputedvalueofSin(60.000000)=0.866236

Value from library function is sin(60.000000) = 0.866236

7a.Write a program to transpose the elements of a 3 x 3 matrix

#include<stdio.h>

int main()

int mat[3][3], i, j, matTrans[3][3];

printf("Enter 3*3 Matrix Elements: ");

for(i=0; i<3; i++)

for(j=0; j<3; j++)


scanf("%d", &mat[i][j]);

// Transposing the Matrix...

for(i=0; i<3; i++)

for(j=0; j<3; j++)

matTrans[j][i] = mat[i][j];

printf("\nTranspose of given Matrix is:\n");

for(i=0; i<3; i++)

for(j=0; j<3; j++)

printf("%d ", matTrans[i][j]);

printf("\n");

return 0;

Expected Output:

Enter 3*3 Matrix Elements: 1

7
8

Transpose of given Matrix is:

1 4 7

2 5 8

3 6 9

7b. Write a program to multiply two numbers using functions.

#include <stdio.h>

// Function prototype for multiplication

int multiply(int num1, int num2);

int main() {

int num1, num2, result;

// Input the two numbers

printf("Enter the first number: ");

scanf("%d", &num1);

printf("Enter the second number: ");

scanf("%d", &num2);

// Call the multiply function and store the result

result = multiply(num1, num2);

// Display the result

printf("The product of %d and %d is: %d\n", num1, num2, result);

return 0;

}
// Function to multiply two numbers

int multiply(int num1, int num2) {

return num1 * num2;

Expected Output:

Enter the first number: 6

Enter the second number: 5

The product of 6 and 5 is: 30

8. Sort the given set of N numbers using Bubble sort.

#include<stdio.h>

#include<stdlib.h>

int main()

int num,i,j,arr[10],temp;

printf("\n Program to implement Bubble Sort\n");

printf("\n Enter number of elements\n");scanf("%d",&num);

printf("\n Enter the elements\n");

for(i=0;i<num;i++)

scanf("%d", &arr[i]);

for(i=0;i<num;i++)

for(j=0;j<num-i-1;j++)

{
if(arr[j]>arr[j+1])

temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;

printf("\n The sorted array is\n");for(i=0;i<num;i++)

printf("%d \t", arr[i]);printf("\n");

return 0;

Expected Output:

Enter the number of elements: 5

Enter the elements:

56

14

03

89

55

Sorted array: 3 14 55 56 89

8a.Develop a C program that reads N integer numbers and arrange them in ascending order
using selection Sort.
#include <stdio.h>

int main() {

int n;

// Input the size of the array

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

scanf("%d", &n);

int arr[n];

// Input the elements of the array

printf("Enter the elements:\n");

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

scanf("%d", &arr[i]);

// Selection Sort

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

// Find the minimum element in the unsorted part of the array

int minIndex = i;

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

if (arr[j] < arr[minIndex]) {

minIndex = j;

// Swap the found minimum element with the first element

int temp = arr[i];


arr[i] = arr[minIndex];

arr[minIndex] = temp;

// Display the sorted array

printf("Sorted array: ");

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

printf("%d ", arr[i]);

printf("\n");

return 0;

Expected Output:

Enter the number of elements: 05

Enter the elements:

12

02

23

45

78

Sorted array: 2 12 23 45 78

8b.Develop a C program that reads N integer numbers and arrange them in ascending
order using Insertion Sort.

#include <stdio.h>
void insertionSort(int arr[], int n) {

int i, key, j;

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

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

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

int i;

for (i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[100], n, i;

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

scanf("%d", &n);

printf("Enter the elements:\n");

for (i = 0; i < n; i++)


scanf("%d", &arr[i]);

printf("Original array: \n");

printArray(arr, n);

insertionSort(arr, n);

printf("\nSorted array: \n");

printArray(arr, n);

return 0;

Expected Output:

Enter the number of elements: 5

Enter the elements:

12

65

48

23

Original array:

12 5 65 48 23

Sorted array:

5 12 23 48 65

9 Write functions to implement string operations such as compare, concatenate, and find
string length. Use the parameter passing techniques.

#include <stdio.h>

#include <string.h>
// Function prototypes

int compareStrings(char str1[], char str2[]);

void concatenateStrings(char result[], char str1[], char str2[]);

int stringLength(char str[]);

int main() {

char string1[100], string2[100], result[200];

// Input strings

printf("Enter the first string: ");

gets(string1);

printf("Enter the second string: ");

gets(string2);

// Compare strings

int resultCompare = compareStrings(string1, string2);

if (resultCompare == 0) {

printf("Strings are equal.\n");

} else {

printf("Strings are not equal.\n");

// Concatenate strings

concatenateStrings(result, string1, string2);

printf("Concatenated string: %s\n", result);

// String length

int length1 = stringLength(string1);

int length2 = stringLength(string2);


printf("Length of string 1: %d\n", length1);

printf("Length of string 2: %d\n", length2);

return 0;

// Function to compare two strings

int compareStrings(char str1[], char str2[]) {

return strcmp(str1, str2);

// Function to concatenate two strings

void concatenateStrings(char result[], char str1[], char str2[]) {

strcpy(result, str1);

strcat(result, str2);

// Function to find the length of a string

int stringLength(char str[]) {

return strlen(str);

Expected Output:

Output 1:

Enter the first string: rain

Enter the second string: bow

Strings are not equal.

Concatenated string: rainbow

Length of string 1: 4
Length of string 2: 3

Output 2:

Enter the first string: rain

Enter the second string: rain

Strings are equal.

Concatenated string: rainrain

Length of string 1: 4

Length of string 2: 4

9a.Develop a C program to concatenate two strings without using built-in function

#include <stdio.h>

// Function prototype for string concatenation

void concatenateStrings(char result[], const char str1[], const char str2[]);

int main() {

char string1[100], string2[100], result[200];

// Input strings

printf("Enter the first string: ");

gets(string1);

printf("Enter the second string: ");

gets(string2);

// Concatenate strings

concatenateStrings(result, string1, string2);

// Display concatenated string

printf("Concatenated string: %s\n", result);

return 0;
}

// Function to concatenate two strings

void concatenateStrings(char result[], const char str1[], const char str2[]) {

int i = 0, j = 0;

// Copy characters from the first string to the result

while (str1[i] != '\0') {

result[i] = str1[i];

i++;

// Copy characters from the second string to the result

while (str2[j] != '\0') {

result[i + j] = str2[j];

j++;

// Null-terminate the concatenated string

result[i + j] = '\0';

Expected Output:

Enter the first string: rain

Enter the second string: bow

Concatenated string: rainbow

10 Implement structures to read, write and compute average- marks of the students, list the
students scoring above and below the average marks for a class of N students.

#include <stdio.h>

#define MAX_STUDENTS 50
struct Student {

char name[50];

int marks;

};

int main() {

struct Student students[MAX_STUDENTS];

int n;

// Read the number of students

printf("Enter the number of students (up to %d): ", MAX_STUDENTS);

scanf("%d", &n);

if (n <= 0 || n > MAX_STUDENTS) {

printf("Invalid number of students. Exiting the program.\n");

return 1;

// Read student data

printf("\nEnter student data:\n");

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

printf("Enter the name of student %d: ", i + 1);

scanf("%s", students[i].name);

printf("Enter the marks of student %d: ", i + 1);

scanf("%d", &students[i].marks);

// Display student data

printf("\nStudent Data:\n");
printf("%-20s %-10s\n", "Name", "Marks");

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

printf("%-20s %-10d\n", students[i].name, students[i].marks);

// Compute average marks

int totalMarks = 0;

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

totalMarks += students[i].marks;

float average = (float)totalMarks / n;

printf("\nAverage marks for the class: %.2f\n", average);

// Display students above and below average marks

printf("\nStudents scoring above average:\n");

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

if (students[i].marks > average) {

printf("%s\n", students[i].name);

printf("\nStudents scoring below or equal to average:\n");

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

if (students[i].marks <= average) {

printf("%s\n", students[i].name);

}
return 0;

Expected Output:

Enter the number of students (up to 50): 05

Enter student data:

Enter the name of student 1: sejal

Enter the marks of student 1: 60

Enter the name of student 2: ankita

Enter the marks of student 2: 70

Enter the name of student 3: komal

Enter the marks of student 3: 55

Enter the name of student 4: harsha

Enter the marks of student 4: 80

Enter the name of student 5: punam

Enter the marks of student 5: 95

Student Data:

Name Marks

sejal 60

ankita 70

komal 55

harsha 80

punam 95

Average marks for the class: 72.00

Students scoring above average:


harsha

punam

Students scoring below or equal to average:

sejal

ankita

komal

10a. Write a C program to read a sentence and count the number of words in the sentence.

#include <stdio.h>

#include <string.h>

void main()

char str[200];

int y = 0, x;

printf("Enter The String To Find The No. Of Words: \n");

scanf("%[^\n]str", str);

for (x = 0;str[x] != '\0';x++)

if (str[x] == ' ' && str[x+1] != ' ')

y++;

printf("The No. Of Words In The Given String Is: %d\n", y + 1);

Expected Output:

Enter The String To Find The No. Of Words:


Welcome to C programming class

The No. Of Words In The Given String Is: 5

11 Develop a program using pointers to compute the sum, mean and standard deviation of
all elements stored in an array of N real numbers.

#include<stdio.h>

#include<math.h>

void main ()

float a[20], sum1 = 0, sum2 = 0, mean, var, dev;

int i, n;

printf ("Enter no of elements:");

scanf ("%d", &n);

printf ("Enter array elements:");

for (i = 0; i < n; i++)

scanf ("%f", a + i);

sum1 = sum1 + * (a + i);

mean = sum1 / n;

for (i = 0; i < n; i++)

sum2 = sum2 + pow ((*(a + i) - mean), 2);

var = sum2 / n;
dev = sqrt (var);

printf ("Sum :%f\n", sum1);

printf ("Mean :%f\n", mean);

printf ("Variance :%f\n", var);

printf ("Deviation :%f\n", dev);

Expected Output:

Enter no of elements:05

Enter array elements:23

11

45

89

56

Sum :224.000000

Mean :44.799999

Variance :739.359985

Deviation :27.191175

11a. Write a C program to swap two integer values using pointers.

#include <stdio.h>

int main() {

int a, b, temp;

int *ptr1, *ptr2;

printf("Enter the value of a and b: ");

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


printf("\nBefore swapping a = %d and b = %d", a, b);

// Assign the memory address of a and b to *ptr1 and *ptr2

ptr1 = &a;

ptr2 = &b;

// Swap the values a and b

temp = *ptr1;

*ptr1 = *ptr2;

*ptr2 = temp;

printf("\nAfter swapping a = %d and b = %d", a, b);

return 0;

Expected Output:

Enter the value of a and b:

24

12

Before swapping a = 24 and b = 12

After swapping a = 12 and b = 24

11b. Write a program in C using functions to swap two numbers using global variables
concept and call by reference concept.

#include <stdio.h>

swap (int *, int *);

int main()

int a, b;

printf("\nEnter value of a & b: ");


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

printf("\nBefore Swapping:\n");

printf("\na = %d\n\nb = %d\n", a, b);

swap(&a, &b);

printf("\nAfter Swapping:\n");

printf("\na = %d\n\nb = %d", a, b);

return 0;

swap (int *x, int *y)

int temp;

temp = *x;

*x = *y;

*y = temp;

Expected Output:

Enter value of a & b: 24

12

Before Swapping:

a = 24

b = 12

After Swapping:

a = 12

b = 24
12. Write a C program to copy a text file to another, read both the input file name and
target file name.

#include <stdio.h>

#include <stdlib.h>

#define BUFFER_SIZE 1024

int main() {

FILE *sourceFile, *targetFile;

char sourceFileName[100], targetFileName[100];

char buffer[BUFFER_SIZE];

size_t bytesRead;

// Input source file name

printf("Enter the source file name: ");

scanf("%s", sourceFileName);

// Open source file in read mode

sourceFile = fopen(sourceFileName, "r");

if (sourceFile == NULL) {

printf("Error opening source file.\n");

exit(EXIT_FAILURE);

// Input target file name

printf("Enter the target file name: ");

scanf("%s", targetFileName);

// Open target file in write mode

targetFile = fopen(targetFileName, "w");

if (targetFile == NULL) {
printf("Error opening target file.\n");

fclose(sourceFile);

exit(EXIT_FAILURE);

// Copying contents from source to target file

while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, sourceFile)) > 0) {

fwrite(buffer, 1, bytesRead, targetFile);

printf("File copied successfully.\n");

// Close file pointers

fclose(sourceFile);

fclose(targetFile);

return 0;

Expected Output:

Enter the source file name: source.txt

Enter the target file name: target.txt

File copied successfully.

You might also like