Problem Statements C programming
Problem Statements C programming
Objective:
Design and implement a C program that accepts a 2D matrix of integers, computes its transpose,
and displays both the original and transposed matrices. Transposing a matrix involves swapping
its rows and columns, such that the element at position [i][j] in the original matrix is moved to
position [j][i] in the transposed matrix.
Problem Description:
1. Input Requirements:
o The program should prompt the user to input the dimensions of the matrix
(number of rows and columns), both of which must be positive integers within a
predefined range.
o The user should provide integer values to populate the matrix elements.
2. Processing Requirements:
o The program should compute the transpose of the input matrix.
o During transposition, the program swaps rows and columns of the matrix.
Specifically, the element at position [i][j] in the input matrix should be placed
at position [j][i] in the transposed matrix.
3. Output Requirements:
o Display the original matrix in its entered format.
o Display the transposed matrix after processing, ensuring it adheres to the correct
transposed structure.
Functional Requirements:
• Input Validation:
o The number of rows and columns must be within the range 1 to 10.
o The program should gracefully handle any invalid input values (e.g., non-integer
or out-of-range dimensions).
• Matrix Transposition:
o If the input matrix has m rows and n columns, the resulting transposed matrix will
have n rows and m columns.
Constraints:
1. The matrix dimensions (number of rows and columns) must be integers between 1 and
10.
2. The elements of the matrix must be integers.
3. The maximum size of the matrix is limited to 10x10 to ensure efficient processing and
avoid excessive memory usage.
Test Cases:
Expected Solution:
• The program should correctly read the matrix dimensions and elements, process the
transposition, and output both the original and transposed matrices.
• The implementation should be efficient and modular, with clear separation of input
handling, transposition logic, and output formatting.
• Edge cases such as a single row/column matrix or a square matrix should be handled
appropriately.
Solution:
#include <stdio.h>
#define MAX_ROWS 10
#define MAX_COLS 10
// Perform transposition
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
transposed[j][i] = arr[i][j]; // Swap rows and columns
}
}
int main() {
int arr[MAX_ROWS][MAX_COLS];
int i, j, rows, cols;
clrscr();
// Input number of rows and columns
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
return 0;
}
Problem Statement-2
Objective:
Develop a C program to check whether a given unsigned integer number is a palindrome. A
palindrome number is one that reads the same backward as forward. The program should include
input validation, ensuring the entered number falls within a specified range and displays
appropriate error messages for invalid inputs.
Problem Description
1. Input Requirements:
o The program should prompt the user to input an unsigned integer number.
o The input number must lie within the range 0 to 1,000,000,000 (inclusive).
2. Processing Requirements:
o The program should reverse the given number and compare it to the original
number.
o If the reversed number matches the original number, the input is a palindrome.
o Input validation must ensure that:
▪ The input is a valid unsigned integer.
▪ The number is within the specified range.
3. Output Requirements:
o If the input is invalid (non-integer or out of range), display an appropriate error
message.
o If the input is valid:
▪ Print whether the number is a palindrome or not.
Functional Requirements
1. Input Validation:
o Ensure that the input is an unsigned integer.
o Check if the input is within the range [0, 1,000,000,000].
o If the input fails validation, provide an error message and terminate the program.
2. Palindrome Check:
o Reverse the digits of the given number.
o Compare the reversed number with the original number.
o Return a result indicating whether the number is a palindrome.
3. Error Handling:
o Display appropriate error messages for:
▪ Non-integer inputs.
▪ Numbers outside the valid range.
Constraints
Test Cases
• For valid inputs, the program will determine if the number is a palindrome and display
the result.
• For invalid inputs, appropriate error messages will guide the user to enter valid data.
Justification
This program ensures robust handling of user input and adheres to constraints, making it suitable
for checking palindrome numbers in real-world applications. The use of modular programming
promotes readability and maintainability.
Solution:
#include <stdio.h>
// Function to check if a number is a palindrome
int isPalindrome(unsigned long number) {
unsigned long reversed = 0, original = number, remainder;
int main() {
unsigned long number;
const long MIN_RANGE = 0; // Minimum valid input
const long MAX_RANGE = 1000000000; // Maximum valid input (10
digits)
// Prompt user for input
printf("Enter a number between %ld and %ld: ", MIN_RANGE,
MAX_RANGE);
Problem Statement-3
Title: Simple Calculator with Integer and Float Data Type Selection
Objective:
Design and implement a C program that functions as a simple calculator to perform arithmetic
operations on integer or float numbers. The program should allow users to choose the data type
(integer or float) and input two numbers along with an operator. It then computes the result
based on the specified operation and displays it, while also handling errors such as invalid inputs
and division/modulo by zero.
Problem Description:
1. Input Requirements:
o The user is prompted to select the data type:
▪ i for integer calculations.
▪ f for float calculations.
o Depending on the selected data type:
▪ For integers, the program accepts two integers and an arithmetic operator.
▪ For floats, the program accepts two floating-point numbers and an
arithmetic operator.
2. Processing Requirements:
o Perform the specified arithmetic operation (+, -, *, /, %):
▪ % (modulus) is only valid for integers.
o Validate the inputs and handle errors:
▪Check for valid operator symbols.
▪Ensure the second operand is non-zero for division or modulus operations.
3. Output Requirements:
o Display the computed result formatted according to the selected data type.
o Display error messages for:
▪ Invalid operator.
▪ Division or modulus by zero.
▪ Invalid data type selection.
Functional Requirements:
Constraints:
1. The program should handle input values within the range of integers and float numbers
supported by the system.
2. For floats, results should be displayed up to two decimal places.
3. Modulus operation (%) should only be allowed when integers are selected.
4. Input validation should ensure proper functioning of the program for all valid cases.
Test Cases:
Expected Behavior:
• The program correctly performs arithmetic operations based on the selected data type.
• Proper error messages guide users in cases of invalid input or errors.
• Supports modular design for maintainability and clarity.
Solution:
#include <stdio.h>
void calculator()
{
char operator;
char data_type;
printf("Choose the data type (i for integer, f for float): ");
scanf(" %c", &data_type); // Notice the space before %c to ignore
newline
if (data_type == 'i') {
int num1, num2, result;
printf("Enter an integer expression (e.g., 5 + 3): ");
scanf("%d %c %d", &num1, &operator, &num2);
switch (operator)
{
case '+':
result = num1 + num2;
printf("Result: %d\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %d\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %d\n", result);
break;
case '/':
if (num2 != 0)
{
printf("Result: %d\n", num1 / num2);
}
else
{
printf("Error: Division by zero is not allowed.\n");
}
break;
case '%':
if (num2 != 0)
{
printf("Result: %d\n", num1 % num2);
} else
{
printf("Error: Modulo by zero is not allowed.\n");
}
break;
default:
printf("Error: Invalid operator.\n");
}
}
else
if (data_type == 'f')
{
float num1, num2, result;
printf("Enter a float expression (e.g., 5.5 + 3.2): ");
scanf("%f %c %f", &num1, &operator, &num2);
switch (operator)
{
case '+':
result = num1 + num2;
printf("Result: %.2f\n", result);
break;
case '-':
result = num1 - num2;
printf("Result: %.2f\n", result);
break;
case '*':
result = num1 * num2;
printf("Result: %.2f\n", result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2f\n", result);
}
else
{
printf("Error: Division by zero is not allowed.\n");
}
break;
default: printf("Error: Invalid operator.\n");
}
} else
{
printf("Error: Invalid data type selected.\n");
}
}
int main()
{
calculator();
return 0;
}
Problem Statement-4
Objective:
Create a C program to determine whether a given year is a leap year or not. The program should
accept an integer input representing a year, validate the input, and use logical conditions to verify
the leap year status. Appropriate messages should be displayed for invalid inputs and the result
of the calculation.
Problem Description:
1. Input Requirements:
o The user must input an integer representing the year.
o Input validation must ensure that the entered value is an integer.
2. Processing Requirements:
o A year is considered a leap year if:
▪ It is divisible by 4 but not divisible by 100, OR
▪ It is divisible by 400.
o The program must verify the leap year status using these conditions.
3. Output Requirements:
o If the year is a leap year, display:
"<year> is a leap year."
o If the year is not a leap year, display:
"<year> is not a leap year."
o For invalid inputs (non-integer values), display an appropriate error message.
Functional Requirements:
1. Input Validation:
o Check if the entered value is an integer.
o For invalid inputs, display an error message and terminate the program.
2. Leap Year Check:
o Implement a function isLeapYear(int year) to determine if the year satisfies
leap year conditions.
o Use the function in the main program for modularity and clarity.
3. Error Handling:
o Ensure non-integer inputs are identified and handled gracefully.
Constraints:
Test Cases:
Expected Behavior:
• The program should accurately identify leap years based on the specified conditions.
• Non-integer inputs should trigger an appropriate error message.
• Modular design ensures the separation of input validation, leap year computation, and
result display.
Solution:
#include <stdio.h>
// Function to determine if a year is a leap year
int isLeapYear(int year) {
// Leap year conditions
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1; // Leap year
}
return 0; // Not a leap year
}
int main() {
int year;
Problem Statement-5
Title: Calculate the Factorial of a Given Integer using Call by Value and Call by Reference
Objective:
Design and implement a C program to calculate the factorial of a positive integer using two
approaches: Call by Value and Call by Reference. The program should validate user input,
compute the factorial using both methods, and display the results.
Problem Description:
1. Input Requirements:
o The user must input a positive integer (n) for which the factorial is to be
calculated.
o Input validation must ensure that the entered value is a positive integer (including
zero).
2. Processing Requirements:
o Implement two separate functions:
1. Call by Value: A function that takes the input number and returns its
factorial without modifying the original variable.
2. Call by Reference: A function that takes the input number and a pointer
to store the result, directly modifying the variable passed by the caller.
o The factorial calculation should be performed iteratively in both methods.
3. Output Requirements:
o Display the calculated factorial using both Call by Value and Call by Reference
methods.
o If the input is invalid (negative or non-integer), display an appropriate error
message.
Functional Requirements:
1. Input Validation:
o Accept input using scanf and validate whether it is a positive integer.
o Handle invalid inputs gracefully by displaying an error message and terminating
the program.
2. Factorial Calculation:
o Call by Value: Return the factorial value computed within the function.
o Call by Reference: Modify the value at the memory location of the provided
pointer.
3. Output Formatting:
o Clearly distinguish between results computed using the two methods.
o Use descriptive and user-friendly messages to display results.
Constraints:
1. Input must be a positive integer (n >= 0).
2. Ensure the program handles large inputs within the range of the data type int in C.
3. Modular design with separate functions for each calculation approach is required.
Test Cases:
Expected Behavior:
• The program computes the factorial using two methods: Call by Value and Call by
Reference.
• Input validation ensures only valid positive integers are processed.
• Modular design separates input validation, factorial computation, and result display for
better readability and maintainability.
Solution:
#include <stdio.h>
int main() {
int number;
unsigned long factValue, factRef;
return 0;
}
Problem Statement: 6
Objective:
Problem Description:
The program accepts a password as input and validates it based on the following rules:
The program:
Constraints:
Expected Outcomes:
Applications:
This problem statement and the corresponding test cases define the program’s requirements and
ensure that the validation is strictly according to the rules for password strength.
Solution:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Check length
if (length < 8) {
printf("Error: Password must be at least 8 characters long.\n");
return 0;
}
int main() {
char password[100];
// Input password
printf("Enter your password: ");
scanf("%s", password);
return 0;
}
Problem Statement -7
The objective of this program is to demonstrate the usage of structures in C for storing and
displaying student records, including personal information such as name, roll number, date of
birth, and division.
Problem Description:
You are tasked with writing a C program that stores and displays the details of a student using
structures. The program should define a struct for the student's date of birth and another
struct for storing the student's details, including the student's name, roll number, date of birth,
and division. The program should allow the user to input these details and then display them
back.
1. Input Details:
o The user is prompted to input the following details for the student:
▪ Student's name (string up to 50 characters)
▪ Student's roll number (integer)
▪ Student's date of birth:
▪ Day (integer between 1 and 31)
▪ Month (integer between 1 and 12)
▪ Year (integer)
▪ Student's division (string of length up to 5 characters)
2. Output:
o The program should display the following information about the student:
▪ Student's name
▪ Student's roll number
▪ Student's date of birth in the format DD/MM/YYYY
▪ Student's division
3. Constraints:
o The date should be entered as day, month, and year as integers.
o All inputs must be validated to ensure that they are within the correct ranges (for
example, valid days and months).
o The program should handle memory for strings and manage input using the fgets()
function to avoid overflow.
Functionality:
1. Structure Definition:
o A structure Date to store the day, month, and year of the student's date of birth.
o A structure Student that includes the student's name, roll number, a Date structure
for the date of birth, and division.
2. Input:
o The program should read and store the student's details using a function
inputStudentDetails(), which accepts a pointer to a Student structure.
3. Output:
o The program should display the student details using the function
displayStudentDetails().
Example:
Sample Input:
Enter student name: John Doe
Enter roll number: 123
Enter day of birth (1-31): 15
Enter month of birth (1-12): 5
Enter year of birth: 2000
Enter division: A
Sample Output:
Student Information:
Name: John Doe
Roll Number: 123
Date of Birth: 15/5/2000
Division: A
Expected Outcomes:
1. The program should correctly read and store the student's information.
2. The program should display the student's information correctly, ensuring proper formatting for
the date of birth.
3. The program should manage input and output cleanly without any issues of buffer overflow or
incorrect data display.
This problem statement outlines the requirements for a student record system that demonstrates
the use of structures in C programming for organizing and managing student-related data.
Solution:
#include <stdio.h>
#include <string.h>
int main() {
// Declare a structure variable to store student details
struct Student student1;
return 0;
}
Problem Statement – 8
Objective:
Create a simple Notepad-like application in C that allows users to perform basic text editing
operations. The program should support writing text to a file, reading text from a file, and
displaying a menu interface for user interaction.
Features:
1. Write Text to File: The program should allow users to type text, which will be saved to a file
(notepad.txt). If the file already exists, it should be overwritten with the new text. The text
input should support multiple lines, and the user should be able to press Enter to finish the
input.
2. Read Text from File: The program should allow users to view the contents of the file
(notepad.txt). If the file exists, it should read and display the text stored in it.
3. Exit the Program: The program should provide an option for the user to exit the application.
Functional Requirements:
1. Menu System: The program should display a menu with three options:
o Option 1: Write text to a file
o Option 2: Read text from a file
o Option 3: Exit the program
2. Text Input Handling: The program should read text from the user and handle it efficiently using
fgets for multi-line input.
3. File Handling:
o The program should create or open the file notepad.txt for both reading and writing.
o If the user chooses to write text, the file should be opened in write mode (w), and any
existing content should be replaced with the new text.
o If the user chooses to read from the file, the program should open the file in read mode
(r) and display the contents on the screen.
4. Error Handling: If the file cannot be opened (e.g., due to permission issues or if the file does not
exist), the program should display an error message.
5. Continuous Operation: The program should run in a loop, allowing the user to choose multiple
options until they decide to exit.
Inputs:
• User inputs text to be saved in the file when selecting the "Write text to file" option.
• The user selects one of the menu options (1, 2, or 3).
Outputs:
• The program will print messages to the console indicating the result of each operation:
o Confirmation that text has been successfully saved to notepad.txt.
o The contents of the notepad.txt file when read.
o Error messages if the file cannot be opened or found.
o A menu prompt to guide the user through operations.
Example Interaction:
1. Menu Display:
• The text input should not exceed MAX_TEXT_SIZE (1000 characters) to avoid buffer overflow.
• The file to be read (notepad.txt) should exist and contain readable text for the "Read text
from file" option to function correctly.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
int choice;
while (1) {
displayMenu(); // Display menu options
scanf("%d", &choice); // Get user input for menu option
switch (choice) {
case 1:
writeToFile();//Call the function to write text to file
break;
case 2:
readFromFile();//Call the function to read text from file
break;
case 3:
printf("Exiting the notepad...\n");
return 0; // Exit the program
default:
printf("Invalid option! Please select a valid menu
option.\n");
}
}
return 0;
}