Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views39 pages

PREM

Uploaded by

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

PREM

Uploaded by

premmutha31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

NAME : PREM MUTHA IAR:15961 B.

TECH(IT)

/*

1. Write a C program that performs as a calculator (addition, multiplication, division, subtraction)


for 2 numbers.

*/

#include <stdio.h>

int main() {

char operator;

float num1, num2, result;

// Input the operator and the two numbers

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

scanf(" %c", &operator); // Note the space before %c to handle newline character

printf("Enter two numbers: ");

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

// Perform the calculation based on the operator

switch (operator) {

case '+':

result = num1 + num2;

printf("%.2f + %.2f = %.2f\n", num1, num2, result);

break;

case '-':

result = num1 - num2;

printf("%.2f - %.2f = %.2f\n", num1, num2, result);

break;

case '*':

result = num1 * num2;

printf("%.2f * %.2f = %.2f\n", num1, num2, result);

1|Page
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

break;

case '/':

// Check for division by zero

if (num2 != 0) {

result = num1 / num2;

printf("%.2f / %.2f = %.2f\n", num1, num2, result);

} else {

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

break;

default:

printf("Error! Operator is not correct.\n");

break;

return 0;

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

Enter two numbers: 15 4

15.00 * 4.00 = 60.00

2|Page
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

// 2. Write a C program to find area of triangle (a=h*b*0.5)

#include <stdio.h>

int main() {

float base, height, area;

// Input the base and height of the triangle

printf("Enter the base of the triangle: ");

scanf("%f", &base);

printf("Enter the height of the triangle: ");

scanf("%f", &height);

// Calculate the area of the triangle

area = 0.5 * base * height;

// Display the result

printf("The area of the triangle is: %.2f\n", area);

return 0;

OUTPUT :-
Enter the base of the triangle: 3

Enter the height of the triangle: 4

The area of the triangle is: 6.00

3|Page
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

// 3. Write a C program to calculate simple interest (i = (p*r*n)/100)

#include <stdio.h>

int main() {

float principal, rate, years, interest;

// Input the principal amount, rate of interest, and number of years

printf("Enter the principal amount: ");

scanf("%f", &principal);

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

scanf("%f", &rate);

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

scanf("%f", &years);

// Calculate the simple interest

interest = (principal * rate * years) / 100;

// Display the result

printf("The simple interest is: %.2f\n", interest);

return 0;

OUTPUT :-
Enter the principal amount: 1000

Enter the rate of interest (in percentage): 5

Enter the number of years: 3

The simple interest is: 150.00

4|Page
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

// 4. Write a C program to compute Fahrenheit from centigrade (f=1.8*c +32)

#include <stdio.h>

int main() {

float centigrade, fahrenheit;

// Input the temperature in Centigrade

printf("Enter temperature in Centigrade: ");

scanf("%f", &centigrade);

// Convert Centigrade to Fahrenheit

fahrenheit = 1.8 * centigrade + 32;

// Display the result

printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);

return 0;

OUTPUT :-
Enter temperature in Celsius: 25

Temperature in Fahrenheit: 77.00

5|Page
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

// 5. Write a C program to interchange two numbers.


#include <stdio.h>

int main() {

int num1, num2, temp;

// Input the two numbers from the user

printf("Enter the first number: ");

scanf("%d", &num1);

printf("Enter the second number: ");

scanf("%d", &num2);

// Display numbers before swapping

printf("Before swapping:\n");

printf("First number = %d\n", num1);

printf("Second number = %d\n", num2);

// Swapping using a temporary variable

temp = num1;

num1 = num2;

num2 = temp;

// Display numbers after swapping

printf("After swapping:\n");

printf("First number = %d\n", num1);

printf("Second number = %d\n", num2);

return 0;

6|Page
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

OUTPUT :-
Enter the first number: 5

Enter the second number: 10

Before swapping:

First number = 5

Second number = 10

After swapping:

First number = 10

Second number = 5

7|Page
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

6. Write a C program to prepare pay slip using the following data.


DA = 10% of Basic, HRA = 7.50% of basic, MA = 300, PF = 12.50% of Basic, Gross = Basic +
DA + HRA + MA, Net = Gross – PF.
*/

#include <stdio.h>

int main() {

float basic, da, hra, ma, pf, gross, net;

// Input the basic salary

printf("Enter the basic salary: ");

scanf("%f", &basic);

// Calculate allowances and deductions

da = 0.10 * basic; // DA = 10% of basic

hra = 0.075 * basic; // HRA = 7.5% of basic

ma = 300.0; // MA is a fixed amount

pf = 0.125 * basic; // PF = 12.5% of basic

// Calculate Gross Salary and Net Salary

gross = basic + da + hra + ma; // Gross = Basic + DA + HRA + MA

net = gross - pf; // Net = Gross - PF

// Display the pay slip

printf("\n---------- PAY SLIP ----------\n");

printf("Basic Salary : %.2f\n", basic);

printf("DA (10%%) : %.2f\n", da);

printf("HRA (7.5%%) : %.2f\n", hra);

printf("MA : %.2f\n", ma);

printf("PF (12.5%%) : %.2f\n", pf);

8|Page
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

printf("-------------------------------\n");

printf("Gross Salary : %.2f\n", gross);

printf("Net Salary : %.2f\n", net);

printf("-------------------------------\n");

return 0;

OUTPUT :-
Enter the basic salary: 5000

---------- PAY SLIP ----------

Basic Salary : 5000.00

DA (10%) : 500.00

HRA (7.5%) : 375.00

MA : 300.00

PF (12.5%) : 625.00

-------------------------------

Gross Salary : 6175.00

Net Salary : 5550.00

-------------------------------

9|Page
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

7. Write a program to find out the sum of the first and last digit of a given number.
*/

#include <stdio.h>

int main() {

int num, firstDigit, lastDigit, sum;

// Input the number from the user

printf("Enter a number: ");

scanf("%d", &num);

// Find the last digit

lastDigit = num % 10;

// Find the first digit

firstDigit = num;

while (firstDigit >= 10) {

firstDigit /= 10;

// Calculate the sum of the first and last digit

sum = firstDigit + lastDigit;

// Output the result

printf("The sum of the first and last digit is: %d\n", sum);

return 0;

}OUTPUT :-

Enter a number: 487

The sum of the first and last digit is: 11

10 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

8. Write a program to read marks of a student from keyboard whether the student is pass
or fail (using if else)
*/

#include <stdio.h>

int main() {

int marks, passMarks;

// Define the pass mark threshold

passMarks = 40; // You can adjust this value as per your criteria

// Input the student's marks

printf("Enter the marks obtained by the student: ");

scanf("%d", &marks);

// Check if the student passed or failed

if (marks >= passMarks) {

printf("The student has passed.\n");

} else {

printf("The student has failed.\n");

return 0;

OUTPUT :-
Enter the marks obtained by the student: 75

The student has passed.

11 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

9. Write a C program to find that the accepted number is Negative, or Positive or Zero.
*/

#include <stdio.h>

int main() {

int num;

// Input the number from the user

printf("Enter a number: ");

scanf("%d", &num);

// Check if the number is positive, negative, or zero

if (num > 0) {

printf("The number is positive.\n");

} else if (num < 0) {

printf("The number is negative.\n");

} else {

printf("The number is zero.\n");

return 0;

OUTPUT :-
Enter a number: 15

The number is Positive.

Enter a number: -8

The number is Negative.

Enter a number: 0

The number is Zero.

12 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

10. Write a program to read marks from keyboard and your program should display
equivalent grade according to following table (if else ladder)
Marks Grade
100 - 80 Distinction
79 - 60 First Class
59 - 40 Second Class

< 40 Fail
*/

#include <stdio.h>

int main() {

int marks;

// Input the student's marks

printf("Enter the marks obtained by the student: ");

scanf("%d", &marks);

// Determine the grade using if-else if-else ladder

if (marks >= 80 && marks <= 100) {

printf("Grade: Distinction\n");

} else if (marks >= 60 && marks <= 79) {

printf("Grade: First Class\n");

} else if (marks >= 40 && marks <= 59) {

printf("Grade: Second Class\n");

} else if (marks >= 0 && marks < 40) {

printf("Grade: Fail\n");

} else {

printf("Invalid marks entered. Please enter marks between 0 and 100.\n");

return 0;

13 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

OUTPUT :-
Enter the marks obtained by the student: 88

Grade: Distinction

Enter the marks obtained by the student: 65

Grade: First Class

Enter the marks obtained by the student: 58

Grade: Second Class

Enter the marks obtained by the student: 33

Grade: Fail

Enter the marks obtained by the student: 115

Invalid marks entered. Please enter marks between 0 and 100.

14 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

11. Write a program to read three numbers from the keyboard and find the maximum out
of these three. (Nested if else)
*/

#include <stdio.h>

int main() {

int num1, num2, num3;

// Input three numbers from the user

printf("Enter the first number: ");

scanf("%d", &num1);

printf("Enter the second number: ");

scanf("%d", &num2);

printf("Enter the third number: ");

scanf("%d", &num3);

// Determine the maximum number using nested if-else

if (num1 >= num2) {

// num1 is greater than or equal to num2

if (num1 >= num3) {

// num1 is greater than or equal to num3

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

} else {

// num3 is greater than num1

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

} else {

// num2 is greater than num1

if (num2 >= num3) {

// num2 is greater than or equal to num3

15 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

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

} else {

// num3 is greater than num2

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

return 0;

OUTPUT :-
Enter the first number: 15

Enter the second number: 64

Enter the third number: 58

The maximum number is: 64

16 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

12. Write a C program to check whether the entered character is capital, small letter, digit
or any special character.
*/

#include <stdio.h>

#include <ctype.h> // For isdigit(), isalpha(), isupper(), and islower() functions

int main() {

char ch;

// Input the character from the user

printf("Enter a character: ");

scanf("%c", &ch);

// Check the type of the character

if (isupper(ch)) {

printf("The character is an uppercase letter.\n");

} else if (islower(ch)) {

printf("The character is a lowercase letter.\n");

} else if (isdigit(ch)) {

printf("The character is a digit.\n");

} else {

printf("The character is a special character.\n");

return 0;

OUTPUT :-
Enter a character: 99

The character is a digit.

Enter a character: %

The character is a special character.

17 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

13. Write a C program to read no 1 to 7 and print relatively day Sunday to Saturday.
*/

#include <stdio.h>

int main() {

int dayNumber;

// Input the number from the user

printf("Enter a number between 1 and 7: ");

scanf("%d", &dayNumber);

// Determine and print the day of the week using a switch statement

switch(dayNumber) {

case 1:

printf("The day is Sunday.\n");

break;

case 2:

printf("The day is Monday.\n");

break;

case 3:

printf("The day is Tuesday.\n");

break;

case 4:

printf("The day is Wednesday.\n");

break;

case 5:

printf("The day is Thursday.\n");

break;

case 6:

printf("The day is Friday.\n");

18 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

break;

case 7:

printf("The day is Saturday.\n");

break;

default:

printf("Invalid input! Please enter a number between 1 and 7.\n");

break;

return 0;

OUTPUT :-
Enter a number between 1 and 7: 3

The day is Tuesday.

Enter a number between 1 and 7: 7

The day is Saturday.

Enter a number between 1 and 7: 1

The day is Monday.

19 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

14. Write a C program to find the factorial of a given number.


*/

#include <stdio.h>

void main() {

int n, i;

double factorial = 1;

printf("Enter a positive integer: ");

scanf("%d", &n);

// Check if the number is negative

if (n < 0) {

printf("Factorial of a negative number doesn't exist.\n");

else {

// Calculate factorial

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

factorial *= i; // Multiply factorial by i

printf("Factorial of %d = %.2lf\n", n, factorial);

OUTPUT :-
Enter a positive integer: 5

Factorial of 5 = 120.00

Enter a positive integer: 5

Factorial of 5 = 120.00

20 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

15. Write a program to reverse a number.


*/

#include <stdio.h>

void main() {

int num, reversed = 0, remainder;

// Ask for user input

printf("Enter an integer: ");

scanf("%d", &num);

// Loop to reverse the number

while (num != 0) {

remainder = num % 10; // Get the last digit

reversed = reversed * 10 + remainder; // Shift reversed number left by 1 digit and add remainder

num = num/10; // Remove the last digit from num

// Output the reversed number

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

OUTPUT :-
Enter an integer: 5643

Reversed number = 3465

Enter an integer: 861527

Reversed number = 725168

21 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

16. Write a program to generate first n number of Fibonacci series.


*/

#include <stdio.h>

void main() {

int n, first = 0, second = 1, next;

// Ask for user input

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

scanf("%d", &n);

printf("Fibonacci Series: ");

// Loop to print the Fibonacci series

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

if (i <= 1) {

next = i; // For the first two terms (0 and 1)

} else {

next = first + second; // Sum of previous two terms

first = second; // Update first

second = next; // Update second

printf("%d ", next); // Print the next term in the series

OUTPUT :-
Enter the number of terms: 5

Fibonacci Series: 0 1 1 2 3

Enter the number of terms: 9

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

22 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

17. Write a program to check whether the given number is prime or not.
*/

#include <stdio.h>

void main() {

int num, isPrime = 1;

// Ask for user input

printf("Enter an integer: ");

scanf("%d", &num);

// Check if the number is less than 2

if (num < 2) {

isPrime = 0; // Numbers less than 2 are not prime

} else {

// Loop to check if the number has any divisors other than 1 and itself

for (int i = 2; i <= num / 2; i++) {

if (num % i == 0) { // If num is divisible by i, it's not prime

isPrime = 0;

break; // Exit the loop early

// Output whether the number is prime

if (isPrime == 1) {

printf("%d is a prime number.\n", num);

} else {

printf("%d is not a prime number.\n", num);

OUTPUT :- Enter an integer: 5


5 is a prime number.

23 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/* 18. Write a C program to find the sum and average of different numbers which are
accepted by user as many as user wants
*/

#include <stdio.h>

void main() {

int n, num, sum = 0;

float average;

// Ask the user for the number of inputs

printf("How many numbers do you want to enter? ");

scanf("%d", &n);

// Loop to get each number from the user and calculate the sum

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

printf("Enter number %d: ", i);

scanf("%d", &num);

sum += num; // Add each number to sum

// Calculate the average

average = (float)sum / n;

// Output the sum and average

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

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

OUTPUT :-
How many numbers do you want to enter? 5

Enter number 1: 8

Enter number 2: 11

Enter number 3: 6

Enter number 4: 15

Enter number 5: 21

Sum = 61

Average = 12.20

24 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/* 19. Write a program to print following patterns:

a) *

**

***

****

*****

*/

#include <stdio.h>

void main() {

int n;

// Ask the user for the number of rows

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

scanf("%d", &n);

// Loop to print each row

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

// Inner loop to print '*' in each row

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

printf("*");

printf("\n"); // Move to the next line after each row

OUTPUT :-
Enter the number of rows: 5

**

***

****

*****

25 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

b) * * * * *
****
***
**
*

#include <stdio.h>

int main()

int rows = 5;

// first loop for printing all rows

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

// first inner loop for printing white spaces

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

printf(" ");

// second inner loop for printing star *

for (int k = 0; k < rows - i; k++) {

printf("* ");

printf("\n");

return 0;

OUTPUT :-
*****
****
***
**
*

26 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

c) *
**
***
****
*****

int main()

int rows = 5;

// first loop is for printing the rows

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

// loop for printing leading whitespaces

for (int j = 0; j < 2 * (rows - i) - 1; j++) {

printf(" ");

// loop for printing * character

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

printf("* ");

printf("\n");

return 0;

OUTPUT :-
*
**
***
****
*****

27 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*

20. Write a C program to calculate the average of n elements in an array.


*/

#include <stdio.h>

int main() {

int n, i;

float sum = 0.0, average;

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

scanf("%d", &n);

float arr[n];

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

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

printf("Element %d: ", i + 1);

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

sum += arr[i];

average = sum / n;

printf("The average is: %.2f\n", average);

return 0;

OUTPUT :-
Enter the number of elements: 5

Enter the elements:

Element 1: 1

Element 2: 2

Element 3: 3

Element 4: 4

Element 5: 5

The average is: 3.00

28 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/* 21. Write a program to add 2 matrices*/

#include <stdio.h>

int main() {

int rows, cols;

int i, j;

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

scanf("%d", &rows);

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

scanf("%d", &cols);

int matrix1[rows][cols], matrix2[rows][cols], sum[rows][cols];

// Input for the first matrix

printf("Enter elements of the first matrix:\n");

for (i = 0; i < rows; i++) {

for (j = 0; j < cols; j++) {

printf("Element [%d][%d]: ", i + 1, j + 1);

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

// Input for the second matrix

printf("Enter elements of the second matrix:\n");

for (i = 0; i < rows; i++) {

for (j = 0; j < cols; j++) {

printf("Element [%d][%d]: ", i + 1, j + 1);

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

// Calculating the sum of matrices

for (i = 0; i < rows; i++) {

for (j = 0; j < cols; j++) {

sum[i][j] = matrix1[i][j] + matrix2[i][j];

29 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

printf("Sum of the matrices:\n");

for (i = 0; i < rows; i++) {

for (j = 0; j < cols; j++) {

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

printf("\n");

return 0;

OUTPUT :-
Enter the number of rows: 3

Enter the number of columns: 2

Enter elements of the first matrix:

Element [1][1]: 1

Element [1][2]: 6

Element [2][1]: 3

Element [2][2]: 4

Element [3][1]: 5

Element [3][2]: 8

Enter elements of the second matrix:

Element [1][1]: 8

Element [1][2]: 4

Element [2][1]: 7

Element [2][2]: 5

Element [3][1]: 6

Element [3][2]: 1

Sum of the matrices:

9 10

10 9

11 9

30 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/* 22. Write a program that defines a function to add 2 numbers .*/

#include <stdio.h>

// Function to add two numbers

int add(int a, int b) {

return a + b;

int main() {

int num1, num2, sum;

printf("Enter the first number: ");

scanf("%d", &num1);

printf("Enter the second number: ");

scanf("%d", &num2);

// Calling the add function

sum = add(num1, num2);

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

return 0;

OUTPUT :-
Enter the first number: 51

Enter the second number: 25

The sum of 51 and 25 is: 76

31 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/* 23. Write a program that defines a function to find the factorial of a number using
recursion.*/
#include <stdio.h>

// Recursive function to find factorial

int factorial(int n) {

if (n <= 1) // Base case: factorial of 0 or 1 is 1

return 1;

else

return n * factorial(n - 1); // Recursive case

int main() {

int num;

printf("Enter a positive integer: ");

scanf("%d", &num);

if (num < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else {

printf("The factorial of %d is: %d\n", num, factorial(num));

return 0;

OUTPUT :-
Enter a positive integer: 9

The factorial of 9 is: 362880

32 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/*24. Define a structured employee that would contain employee id, department id, and
salary. Using this structure read information of 5 employees and print the same on
screen.*/

#include <stdio.h>

// Define structure for Employee

struct Employee {

int emp_id;

int dept_id;

float salary;

};

int main() {

struct Employee employees[5];

// Reading information for 5 employees

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

printf("Enter details for Employee %d:\n", i + 1);

printf("Employee ID: ");

scanf("%d", &employees[i].emp_id);

printf("Department ID: ");

scanf("%d", &employees[i].dept_id);

printf("Salary: ");

scanf("%f", &employees[i].salary);

// Displaying information for 5 employees

printf("\nEmployee Details:\n");

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

printf("\nEmployee %d:\n", i + 1);

33 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

printf("Employee ID: %d\n", employees[i].emp_id);

printf("Department ID: %d\n", employees[i].dept_id);

printf("Salary: %.2f\n", employees[i].salary);

return 0;

OUTPUT :-
Enter details for Employee 1:

Employee ID: 1

Department ID: 12

Salary: 3000

Enter details for Employee 2:

Employee ID: 2

Department ID: 13

Salary: 5000

Enter details for Employee 3:

Employee ID: 3

Department ID: 14

Salary: 8000

Enter details for Employee 4:

Employee ID: 4

Department ID: 15

Salary: 9000

Enter details for Employee 5:

Employee ID: 5

Department ID: 16

Salary: 10000

Employee Details:

34 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

Employee 1:

Employee ID: 1

Department ID: 12

Salary: 3000.00

Employee 2:

Employee ID: 2

Department ID: 13

Salary: 5000.00

Employee 3:

Employee ID: 3

Department ID: 14

Salary: 8000.00

Employee 4:

Employee ID: 4

Department ID: 15

Salary: 9000.00

Employee 5:

Employee ID: 5

Department ID: 16

Salary: 10000.00

35 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

/* 25. Using cricket, declare an array player with 5 elements and write a C program to read
the information about all the 5 players and print a team wise list containing names of
players with their batting average.*/
#include <stdio.h>

#include <string.h>

// Define structure for Player

struct Player {

char name[50];

char team[50];

float batting_average;

};

int main() {

struct Player players[5];

char team_name[50];

// Reading information for 5 players

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

printf("Enter details for Player %d:\n", i + 1);

printf("Name: ");

scanf(" %[^\n]s", players[i].name);

printf("Team: ");

scanf(" %[^\n]s", players[i].team);

printf("Batting Average: ");

scanf("%f", &players[i].batting_average);

// Printing team-wise list of players

printf("\nTeam-wise list of players:\n");

// Iterate over each player’s team and print players belonging to that team

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

// Check if this team has already been printed

int already_printed = 0;

36 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

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

if (strcmp(players[i].team, players[j].team) == 0) {

already_printed = 1;

break;

// If the team has not been printed, print all players from that team

if (!already_printed) {

printf("\nTeam: %s\n", players[i].team);

printf("Player Name\tBatting Average\n");

printf("-----------------------------\n");

for (int k = 0; k < 5; k++) {

if (strcmp(players[i].team, players[k].team) == 0) {

printf("%-15s %.2f\n", players[k].name, players[k].batting_average);

return 0;

OUTPUT :-
Enter details for Player 1:

Name: PREM

Team: GUJARAT

Batting Average: 50

Enter details for Player 2:

Name: OM

Team: RAJASTHAN

Batting Average: 72

Enter details for Player 3:

Name: DHONI

37 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

Team: CHENNAI

Batting Average: 43

Enter details for Player 4:

Name: ROHIT

Team: MUMBAI

Batting Average: 38

Enter details for Player 5:

Name: VIRAT

Team: BANGLURU

Batting Average: 46

Team-wise list of players:

Team: GUJARAT

Player Name Batting Average

-----------------------------

PREM 50.00

Team: RAJASTHAN

Player Name Batting Average

-----------------------------

OM 72.00

Team: CHENNAI

Player Name Batting Average

-----------------------------

DHONI 43.00

Team: MUMBAI

Player Name Batting Average

-----------------------------

ROHIT 38.00

Team: BANGLURU

Player Name Batting Average

-----------------------------

VIRAT 46.00

38 | P a g e
NAME : PREM MUTHA IAR:15961 B.TECH(IT)

39 | P a g e

You might also like