Programming in C 1BEIT105/205
1. Two friends namely Ram and Rahim are sportsmen and they wanted to know who is taller
among themselves. Solve this problem programmatically.
Program Logic:
1. Read the height of Ram
2. Read the height of Rahim
3. Compare both heights:
o If Ram’s height is greater → Ram is taller
o If Rahim’s height is greater → Rahim is taller
o Otherwise → Both are of equal height
Source Code:
#include <stdio.h>
int main()
{
float ramHeight, rahimHeight;
printf("Enter Ram's height (in cm): ");
scanf("%f", &ramHeight);
printf("Enter Rahim's height (in cm): ");
scanf("%f", &rahimHeight);
if (ramHeight > rahimHeight)
{
printf("Ram is taller than Rahim.\n");
}
else if (rahimHeight > ramHeight)
{
printf("Rahim is taller than Ram.\n");
}
else
{
printf("Both Ram and Rahim are of the same height.\n");
}
return 0;
}
Output:
Enter Ram's height (in cm): 162
Enter Rahim's height (in cm): 167
Rahim is taller than Ram.
Programming Assignment 1
Programming in C 1BEIT105/205
2. Develop a C Program to demonstrate the usage of electricity based on the units consumed
using following data.
If units consumed are:
• Less than 100 → Low usage
• 100 to 300 → Medium usage
• Units greater than 300 → High usage
Program Logic:
1. Read the number of units consumed
2. Check conditions:
o Units less than 100 → Low usage
o Units between 100 and 300 → Medium usage
o Units greater than 300 → High usage
Source Code:
#include <stdio.h>
int main()
{
int units;
printf("Enter the number of units consumed: ");
scanf("%d", &units);
if (units < 100)
{
printf("Electricity Usage: Low usage\n");
}
else if (units >= 100 && units <= 300)
{
printf("Electricity Usage: Medium usage\n");
}
else
{
printf("Electricity Usage: High usage\n");
}
return 0;
}
Output:
Enter the number of units consumed: 250
Electricity Usage: Medium usage
Programming Assignment 2
Programming in C 1BEIT105/205
3. Develop a C program to create a simple calculator using a switch statement. The program
should accept two numbers and an operator (+, -, *, /, %) and display the result.
Program Logic:
1. Read two numbers
2. Read an operator (+, -, *, /, %)
3. Use switch to perform the corresponding operation
4. Display the result
Source Code:
#include <stdio.h>
int main()
{
int a, b;
int ch;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Press 1 Addition: ");
printf("Press 2 Subtraction: ");
printf("Press 3 Multiplication: ");
printf("Press 4 Division: ");
printf("Press 5 Modulo Division: ");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Result = %d\n", a + b);
break;
case 2:
printf("Result = %d\n", a - b);
break;
case 3:
printf("Result = %d\n", a * b);
break;
Programming Assignment 3
Programming in C 1BEIT105/205
case 4:
if (b != 0)
printf("Result = %d\n", a / b);
else
printf("Error: Division by zero is not allowed\n");
break;
case 5:
if (b != 0)
printf("Result = %d\n", a % b);
else
printf("Error: Modulus by zero is not allowed\n");
break;
default:
printf("Invalid choice\n");
}
return 0;
}
Output:
Enter first number: 20
Enter second number: 5
Press 1 Addition:
Press 2 Subtraction:
Press 3 Multiplication:
Press 4 Division:
Press 5 Modulo Division:
Enter your choice 1
Result = 25
Programming Assignment 4
Programming in C 1BEIT105/205
4. Develop C program to find the Sum of Odd and Even Numbers of a Series.
Program Logic:
1. Read the number of elements n
2. Initialize evenSum = 0 and oddSum = 0
3. Read each number one by one
4. Check:
• If number is even → add to evenSum
• Else → add to oddSum
5. Display both sums
Source Code:
#include <stdio.h>
int main()
{
int n, i, num;
int evenSum = 0, oddSum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("Enter number %d: ", i);
scanf("%d", &num);
if (num % 2 == 0)
evenSum += num;
else
oddSum += num;
}
printf("\nSum of Even Numbers = %d", evenSum);
printf("\nSum of Odd Numbers = %d", oddSum);
return 0;
}
Output:
Enter number of elements: 5
Enter number 1: 10
Enter number 2: 15
Enter number 3: 20
Enter number 4: 25
Enter number 5: 30
Sum of Even Numbers = 60
Sum of Odd Numbers = 40
Programming Assignment 5
Programming in C 1BEIT105/205
5. A class has n students. Each student’s marks are stored in an array. Design a C Program
to
• Calculate total marks
• Find average marks
• Display highest and lowest marks
Program Logic:
1. Read number of students n
2. Read marks of n students into an array
3. Initialize:
total = 0
highest = marks[0]
lowest = marks[0]
4. Traverse the array:
Add each mark to total
Compare to find highest and lowest
5. Calculate average = total / n
6. Display results
Source Code:
#include <stdio.h>
int main()
{
int n, i;
int marks[50];
int total = 0;
int highest, lowest;
float average;
printf("Enter number of students: ");
scanf("%d", &n);
printf("Enter marks of %d students:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &marks[i]);
}
highest = marks[0];
lowest = marks[0];
for (i = 0; i < n; i++)
{
total += marks[i];
Programming Assignment 6
Programming in C 1BEIT105/205
if (marks[i] > highest)
highest = marks[i];
if (marks[i] < lowest)
lowest = marks[i];
}
average = total / n;
printf("\nTotal Marks = %d", total);
printf("\nAverage Marks = %.2f", average);
printf("\nHighest Marks = %d", highest);
printf("\nLowest Marks = %d", lowest);
return 0;
}
Output:
Enter number of students: 5
Enter marks of 5 students:70 85 90 60 75
Total Marks = 380
Average Marks = 76.00
Highest Marks = 90
Lowest Marks = 60
Programming Assignment 7
Programming in C 1BEIT105/205
6. Develop a C Program to find sum of even and odd numbers in an array of n elements.
Program Logic:
1. Read the value of n
2. Read n elements into an array
3. Initialize evenSum = 0 and oddSum = 0
4. Traverse the array:
• If element is even → add to evenSum
• Else → add to oddSum
5. Display both sums
Source Code:
#include <stdio.h>
int main()
{
int n, i;
int arr[50];
int evenSum = 0, oddSum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++)
{
if (arr[i] % 2 == 0)
evenSum += arr[i];
else
oddSum += arr[i];
}
printf("\nSum of Even Numbers = %d", evenSum);
printf("\nSum of Odd Numbers = %d", oddSum);
return 0;
}
Output:
Enter number of elements: 6
Enter 6 elements:10 15 20 25 30 35
Sum of Even Numbers = 60
Sum of Odd Numbers = 75
Programming Assignment 8
Programming in C 1BEIT105/205
7. An organization stores employee salaries in an array. Design a C Program to count
employees earning more than ₹50,000 salary.
Program Logic:
1. Read number of employees n
2. Read salaries into an array
3. Initialize count = 0
4. Traverse the array:
• If salary > 50,000 → increment count
5. Display the count
Source Code:
#include <stdio.h>
int main()
{
int n, i;
int salary[50];
int count = 0;
printf("Enter number of employees: ");
scanf("%d", &n);
printf("Enter salaries of %d employees:\n", n);
for (i = 0; i < n; i++)
{
scanf("%d", &salary[i]);
}
for (i = 0; i < n; i++)
{
if (salary[i] > 50000)
count++;
}
printf("\nNumber of employees earning more than ₹50,000 = %d", count);
return 0;
}
Output:
Enter number of employees: 6
Enter salaries of 6 employees:
30000 52000 48000 61000 75000 45000
Number of employees earning more than ₹50,000 = 3
Programming Assignment 9
Programming in C 1BEIT105/205
8. A website stores a user’s password as a string. Develop a C Program to check whether the
entered password matches the stored password.( Password Validation Program)
Program Logic:
1. Store the original password in a string
2. Read the entered password from the user
3. Compare both strings using strcmp()
4. If result is 0 → Password matched
5. Else → Invalid password
Source Code:
#include <stdio.h>
#include <string.h>
int main()
{
char storedPassword[20] = "AGMRCET";
char enteredPassword[20];
printf("Enter password: ");
scanf("%s", enteredPassword);
if (strcmp(storedPassword, enteredPassword) == 0)
{
printf("Password Matched. Access Granted.\n");
}
else
{
printf("Invalid Password. Access Denied.\n");
}
return 0;
}
Output:
Enter password: AGMRCET
Password Matched. Access Granted.
Enter password: AGM
Invalid Password. Access Denied.
Programming Assignment 10
Programming in C 1BEIT105/205
9. Develop a C Program to converts all lowercase characters in a string to uppercase
characters.
Program Logic:
1. Read the input string
2. Traverse each character of the string
3. If a character is lowercase ('a' to 'z'), convert it to uppercase
4. Display the converted string
Source Code:
#include <stdio.h>
int main()
{
char str[100];
int i;
printf("Enter the text: ");
gets(str);
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32; // ASCII difference between lowercase and uppercase letters is 32
}
}
printf("Text after conversion: %s", str);
return 0;
}
Output:
Enter the text: agmr cet
Text after conversion: AGMR CET
OR
Alternative C Program to convert lowercase to uppercase (Using Library Function)
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i;
printf("Enter the text: ");
gets(str);
for (i = 0; str[i] != '\0'; i++)
{
str[i] = toupper(str[i]);
}
printf("Text after conversion: %s", str);
return 0;
}
Programming Assignment 11
Programming in C 1BEIT105/205
10. A college wants to calculate the total and average marks of a students, Develop a C
Program using functions to calculate total and average marks.
Program Logic:
1. Read the marks of 3 subjects
2. Call the function named calculateTotal to calculate total marks of 3 subjects
3. Return total marks
4. Calculate average marks in main() program
5. Display the total and average marks
Source Code:
#include <stdio.h>
int calculateTotal(int m1, int m2, int m3)
{
return m1 + m2 + m3;
}
int main()
{
int m1, m2, m3, total;
float average;
printf("Enter 3 subject marks: ");
scanf("%d%d%d", &m1, &m2, &m3);
total = calculateTotal(m1, m2, m3);
average = total / 3.0;
printf("Total = %d\n", total);
printf("Average = %.2f", average);
return 0;
}
Output:
Enter 3 subject marks: 70 80 90
Total = 240
Average = 80.00
Programming Assignment 12
Programming in C 1BEIT105/205
11. Develop a C Program to check whether a number entered by the user is even or odd using
a function.
Program Logic:
1. Read a number from the user
2. Call a user-defined function
3. If number is divisible by 2 → Even
4. Otherwise → Odd
Source Code:
#include <stdio.h>
void checkEvenOdd(int num)
{
if (num % 2 == 0)
printf("The number is Even");
else
printf("The number is Odd");
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
checkEvenOdd(n);
return 0;
}
Output:
Enter a number: 5
The number is Odd
Enter a number: 8
The number is Even
Programming Assignment 13
Programming in C 1BEIT105/205
12. Develop a C Program to find the larger of two numbers entered by the user using a
function.
Program Logic:
1. Read two numbers from the user
2. Call a user-defined function with two arguments
3. Compare the numbers inside the function
4. Return the larger number
5. Display the result
Source Code:
#include <stdio.h>
int findMax(int a, int b)
{
if (a > b)
return a;
else
return b;
}
int main()
{
int num1, num2, max;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
max = findMax(num1, num2);
printf("Maximum number = %d", max);
return 0;
}
Output:
Enter two numbers: 5 4
Maximum number = 5
Programming Assignment 14
Programming in C 1BEIT105/205
13. A library stores details of n books using an array of structures. Develop a C program using
structures to display book details.
Program Logic:
1. Define a struct Book with members: book ID, title, and price
2. Read number of books n
3. Read details of n books into an array of structures
4. Display all book details
Source Code:
#include <stdio.h>
struct Book
{
int bookId;
char title[50];
float price;
};
int main()
{
int n, i;
struct Book b[50];
printf("Enter number of books: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter details of Book %d\n", i + 1);
printf("Book ID: ");
scanf("%d", &b[i].bookId);
printf("Title: ");
scanf("%s", b[i].title);
printf("Price: ");
scanf("%f", &b[i].price);
}
printf("\n--- Book Details ---\n");
for (i = 0; i < n; i++)
{
printf("\nBook %d Details", i + 1);
printf("\nBook ID : %d", b[i].bookId);
printf("\nTitle : %s", b[i].title);
printf("\nPrice : %.2f\n", b[i].price);
Programming Assignment 15
Programming in C 1BEIT105/205
return 0;
}
Output:
Enter number of books: 2
Enter details of Book 1
Book ID: 101
Title: CProgramming
Price: 450
Enter details of Book 2
Book ID: 102
Title: DataStructures
Price: 550
--- Book Details ---
Book 1 Details
Book ID : 101
Title : CProgramming
Price : 450.00
Book 2 Details
Book ID : 102
Title : DataStructures
Price : 550.00
Programming Assignment 16
Programming in C 1BEIT105/205
14. A bank uses structures to store account number, customer name, and balance. Write a C
program using structures to perform deposit and withdrawal operations.
Program Logic:
1. Define a structure Account with account number, name, and balance
2. Read initial account details
3. Display a menu:
• Deposit
• Withdraw
4. Perform the selected operation
5. Update and display the balance
Source Code:
#include <stdio.h>
struct Account
{
int accNo;
char name[30];
float balance;
};
int main()
{
struct Account a;
int choice;
float amount;
printf("Enter Account Number: ");
scanf("%d", &[Link]);
printf("Enter Customer Name: ");
scanf("%s", [Link]);
printf("Enter Initial Balance: ");
scanf("%f", &[Link]);
printf("\n1. Deposit");
printf("\n2. Withdraw");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter deposit amount: ");
scanf("%f", &amount);
[Link] += amount;
Programming Assignment 17
Programming in C 1BEIT105/205
printf("Amount Deposited Successfully");
break;
case 2:
printf("Enter withdrawal amount: ");
scanf("%f", &amount);
if (amount <= [Link])
{
[Link] -= amount;
printf("Amount Withdrawn Successfully");
}
else
{
printf("Insufficient Balance");
}
break;
default:
printf("Invalid Choice");
}
printf("\n\nUpdated Account Details:");
printf("\nAccount Number: %d", [Link]);
printf("\nCustomer Name : %s", [Link]);
printf("\nBalance : %.2f", [Link]);
return 0;
}
Output:
Enter Account Number: 346782
Enter Customer Name: Virat
Enter Initial Balance: 5000
1. Deposit
2. Withdraw
Enter your choice: 2
Enter withdrawal amount: 2000
Amount Withdrawn Successfully
Updated Account Details:
Account Number: 346782
Customer Name : Virat
Balance : 3000.00
Programming Assignment 18
Programming in C 1BEIT105/205
15. Develop a C program that stores date information (day, month, year) using a structure.
Program Logic:
1. Define a structure Date with members: day, month, year
2. Read date values from the user
3. Display the stored date in DD-MM-YYYY format
Source Code:
#include <stdio.h>
struct Date
{
int day;
int month;
int year;
};
int main()
{
struct Date d;
printf("Enter day: ");
scanf("%d", &[Link]);
printf("Enter month: ");
scanf("%d", &[Link]);
printf("Enter year: ");
scanf("%d", &[Link]);
printf("\nStored Date: %02d-%02d-%04d", [Link], [Link], [Link]);
return 0;
}
Output:
Enter day: 15
Enter month: 08
Enter year: 2025
Stored Date: 15-08-2025
Programming Assignment 19
Programming in C 1BEIT105/205
16. An organization stores either salary (float) or employee grade (char) using a union.
Develop a C program to read and display these values using union.
Program Logic:
1. Define a union Employee with members salary (float) and grade (char)
2. Provide a menu to the user to choose which data to enter
3. Read the chosen value
4. Display the stored value
Source Code:
#include <stdio.h>
union Employee
{
float salary;
char grade;
};
int main()
{
union Employee emp;
int choice;
printf("Enter your choice:\n");
printf("1. Enter Salary\n");
printf("2. Enter Grade\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter Salary: ");
scanf("%f", &[Link]);
printf("Stored Salary = %.2f\n", [Link]);
break;
case 2:
printf("Enter Grade (A/B/C): ");
scanf(" %c", &[Link]); // space before %c to avoid newline
printf("Stored Grade = %c\n", [Link]);
break;
default:
printf("Invalid Choice\n");
}
Programming Assignment 20
Programming in C 1BEIT105/205
return 0;
}
Output:
Enter your choice:
1. Enter Salary
2. Enter Grade
1
Enter Salary: 75000
Stored Salary = 75000.00
Enter your choice:
1. Enter Salary
2. Enter Grade
2
Enter Grade (A/B/C): B
Stored Grade = B
Programming Assignment 21
Programming in C 1BEIT105/205
17. A system stores either a character code or a numeric code for a device using a union.
Develop a C program to read and display the stored value using union.
Program Logic:
1. Define a union with members charCode (char) and numCode (int)
2. Ask the user which type of code to enter
3. Read the value into the union
4. Display the stored value
Source Code:
#include <stdio.h>
union DeviceCode
{
char charCode;
int numCode;
};
int main()
{
union DeviceCode code;
int choice;
printf("Enter your choice:\n");
printf("1. Enter Character Code\n");
printf("2. Enter Numeric Code\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter Character Code: ");
scanf(" %c", &[Link]); // space before %c to skip newline
printf("Stored Character Code = %c\n", [Link]);
break;
case 2:
printf("Enter Numeric Code: ");
scanf("%d", &[Link]);
printf("Stored Numeric Code = %d\n", [Link]);
break;
default:
printf("Invalid Choice\n");
}
return 0;
Programming Assignment 22
Programming in C 1BEIT105/205
Output:
Enter your choice:
1. Enter Character Code
2. Enter Numeric Code
1
Enter Character Code: A
Stored Character Code = A
Enter your choice:
1. Enter Character Code
2. Enter Numeric Code
2
Enter Numeric Code: 005
Stored Numeric Code = 005
Programming Assignment 23
Programming in C 1BEIT105/205
18. A computer system stores student data as either roll number (int) or marks (float) using a
union. Then Develop a C Program using union to,
• Read the value
• Display which field is currently stored
Program Logic:
1. Define a union Student with members rollNo (int) and marks (float)
2. Ask the user which type of data to enter
3. Read the chosen value into the union
4. Display the stored value and indicate which field is stored
Source Code:
#include <stdio.h>
union Student
{
int rollNo;
float marks;
};
int main()
{
union Student s;
int choice;
printf("Enter your choice:\n");
printf("1. Enter Roll Number\n");
printf("2. Enter Marks\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter Roll Number: ");
scanf("%d", &[Link]);
printf("Stored Field: Roll Number = %d\n", [Link]);
break;
case 2:
printf("Enter Marks: ");
scanf("%f", &[Link]);
printf("Stored Field: Marks = %.2f\n", [Link]);
break;
default:
printf("Invalid Choice\n");
Programming Assignment 24
Programming in C 1BEIT105/205
return 0;
}
Output:
Enter your choice:
1. Enter Roll Number
2. Enter Marks
1
Enter Roll Number: 5
Stored Field: Roll Number = 5
Enter your choice:
1. Enter Roll Number
2. Enter Marks
2
Enter Marks: 14.5
Stored Field: Marks = 14.50
Programming Assignment 25