0% found this document useful (0 votes)
34 views42 pages

Program 1:: #Include #Include

The document contains 8 C programs that demonstrate various programming concepts: 1. A program that reverses a user-input string and prints it. 2. A program that initializes a 2D array, prints it normally and reversed, and calculates the total sum. 3. A program that finds the largest and second largest numbers in a user-input array. 4. A program that searches a user-input array for a target number and prints the positions. 5. A program that performs matrix addition and calculates max, min, average of the result. 6. A program that defines a book struct, inputs book data, and prints specific book information. 7. A program that stores student grades

Uploaded by

Detail Legend
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)
34 views42 pages

Program 1:: #Include #Include

The document contains 8 C programs that demonstrate various programming concepts: 1. A program that reverses a user-input string and prints it. 2. A program that initializes a 2D array, prints it normally and reversed, and calculates the total sum. 3. A program that finds the largest and second largest numbers in a user-input array. 4. A program that searches a user-input array for a target number and prints the positions. 5. A program that performs matrix addition and calculates max, min, average of the result. 6. A program that defines a book struct, inputs book data, and prints specific book information. 7. A program that stores student grades

Uploaded by

Detail Legend
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/ 42

Program 1 :

#include <stdio.h>
#include <string.h>

int main() {
char str[100];
int i, len;

printf("Enter a string: ");


scanf("%s", str);

len = strlen(str);

printf("Reverse of the string: ");

for (i = len - 1; i >= 0; i--) {


printf("%c", str[i]);
}

return 0;
}
Program 2 :
#include <stdio.h>

int main()
{
int arr[5][5];
int i, j, total = 0;

// Fill the 2D array


for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
arr[i][j] = j + 5*i + 1;
}
}

// Display all data


printf("Original array:\n");
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
printf("%-3d", arr[i][j]);
}
printf("\n");
}

// Display reverse order for each row


printf("Reversed array:\n");
for (i = 0; i < 5; i++) {
for (j = 4; j >= 0; j--) {
printf("%-3d", arr[i][j]);
}
printf("\n");
}

// Sum all data and display the sum


for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
total += arr[i][j];
}
}
printf("Total sum: %d\n", total);

return 0;
}

Program 3 :
#include <stdio.h>
int main() {

int arr[7], max, max2nd;

printf("Enter 7 integers: ");

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

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

// find the largest element in the array

max = arr[0];

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

if (arr[i] > max) {

max = arr[i];

// find the second largest element in the array

max2nd = arr[0];

for (int k = 1; k < 7; k++) {

if (arr[k] > max2nd && arr[k] < max) {

max2nd = arr[k];

printf("Largest element: %d\n", max);

printf("Second largest element: %d\n", max2nd);

return 0;

Program 4 :
#include <stdio.h>
#define ARRAY_SIZE 8

int main() {
int myArray[ARRAY_SIZE];
int i, n, count = 0;
int positions[ARRAY_SIZE];

// Ask the user to input 8 numbers


printf("Enter 8 numbers between 1 and 9:\n");
for (i = 0; i < ARRAY_SIZE; i++) {
scanf("%d", &myArray[i]);
}

// Ask the user to input another number


printf("Enter a number to search for:\n");
scanf("%d", &n);

// Search for the position of n in myArray


for (i = 0; i < ARRAY_SIZE; i++) {
if (myArray[i] == n) {
positions[count] = i;
count++;
}
}

// Display how many n are appearing in myArray and its positions


if (count == 0) {
printf("No data found!\n");
} else {
printf("There are %d times in array.\n", count);
printf("They are located in positions ");
for (i = 0; i < count; i++) {
printf("%d", positions[i] + 1);
if (i != count - 1) {
printf(" and ");
}
}
printf(".\n");
}

return 0;
}
Program 5 :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
// Initialize random seed
srand(time(NULL));

// Initialize matrices
int m1[3][3];
int m2[3][3];

// Fill matrices with random numbers between 0 and 9


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
m1[i][j] = rand() % 10;
m2[i][j] = rand() % 10;
}
}
// Calculate m3, the sum of m1 and m2
int m3[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
m3[i][j] = m1[i][j] + m2[i][j];
}
}
// Find max and min numbers in m3
int max = m3[0][0];
int min = m3[0][0];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (m3[i][j] > max) {
max = m3[i][j];
}
if (m3[i][j] < min) {
min = m3[i][j];
}
}
}
// Calculate average of m3
int sum = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
sum += m3[i][j];
}
}
float avg = (float) sum / 9;

// Print matrices m1, m2, and m3


printf("Matrix m1:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d\t", m1[i][j]);
}
printf("\n");
}
printf("Matrix m2:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d\t", m2[i][j]);
}
printf("\n");
}
printf("Matrix m3:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d\t", m3[i][j]);
}
printf("\n");
}
// Print max, min, and average of m3
printf("Max number in m3: %d\n", max);
printf("Min number in m3: %d\n", min);
printf("Average of m3: %f\n", avg);
return 0;
}
Program 6 :
#include <stdio.h>
#include <string.h>

#define MAX_BOOKS 5

struct book {
char title[50];
char author[50];
int code;
int published_year;
float price;
};

int main() {
struct book books[MAX_BOOKS];

// Get input from user for each book


int i ;
for ( i = 0; i < MAX_BOOKS; i++) {
printf("Enter the data for book %d:\n", i+1);
printf("Title: ");
scanf("%s", books[i].title);
printf("Author: ");
scanf("%s", books[i].author);
printf("Code: ");
scanf("%d", &books[i].code);
printf("Published year: ");
scanf("%d", &books[i].published_year);
printf("Price: ");
scanf("%f", &books[i].price);
}

// Show all data of books


printf("\nAll data of books:\n");

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


printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Code: %d\n", books[i].code);
printf("Published year: %d\n", books[i].published_year);
printf("Price: %.2f\n\n", books[i].price);
}

// Show info of books which published after 2010 onwards


printf("Books published after 2010:\n");
for (i = 0; i < MAX_BOOKS; i++) {
if (books[i].published_year >= 2010) {
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Code: %d\n", books[i].code);
printf("Published year: %d\n", books[i].published_year);
printf("Price: %.2f\n\n", books[i].price);
}
}

// Show info of books which has the most expensive price


float max_price = books[0].price;
int max_price_index = 0;
for (i = 1; i < MAX_BOOKS; i++) {
if (books[i].price > max_price) {
max_price = books[i].price;
max_price_index = i;
}
}
printf("Book with the most expensive price:\n");
printf("Title: %s\n", books[max_price_index].title);
printf("Author: %s\n", books[max_price_index].author);
printf("Code: %d\n", books[max_price_index].code);
printf("Published year: %d\n", books[max_price_index].published_year);
printf("Price: %.2f\n\n", books[max_price_index].price);

// Show info of books whose author is John


printf("Books whose author is John:\n");
for (i = 0; i < MAX_BOOKS; i++) {
if (strcmp(books[i].author, "John") == 0) {
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Code: %d\n", books[i].code);
printf("Published year: %d\n", books[i].published_year);
printf("Price: %.2f\n\n", books[i].price);
}
}
}
Program 7 :
#include <stdio.h>

#define NUM_STUDENTS 5 // number of students


#define NUM_ASSIGNMENTS 3 // number of assignments

int main() {
// Declare and initialize the 2D array for storing grades
int grades[NUM_STUDENTS][NUM_ASSIGNMENTS] = {
{90, 85, 92},
{70, 75, 68},
{80, 82, 88},
{95, 89, 97},
{60, 55, 63}
};

// Calculate the average grade for each student


for (int i = 0; i < NUM_STUDENTS; i++) {
int sum = 0;
for (int j = 0; j < NUM_ASSIGNMENTS; j++) {
sum += grades[i][j];
}
double average = (double) sum / NUM_ASSIGNMENTS;
printf("Student %d's average grade is %.2f\n", i+1, average);
}
// Calculate the average grade for each assignment
for (int j = 0; j < NUM_ASSIGNMENTS; j++) {
int sum = 0;
for (int i = 0; i < NUM_STUDENTS; i++) {
sum += grades[i][j];
}
double average = (double) sum / NUM_STUDENTS;
printf("Assignment %d's average grade is %.2f\n", j+1, average);
}

return 0;
}
Program 8 :
#include <stdio.h>
#include <string.h>

#define MAX_EMPLOYEES 100


#define MAX_NAME_LENGTH 50
#define MAX_TITLE_LENGTH 50

struct Employee {
char name[MAX_NAME_LENGTH];
int age;
float salary;
char title[MAX_TITLE_LENGTH];
};

int main() {
struct Employee employees[MAX_EMPLOYEES];
int numEmployees = 0;
int choice;

do {
printf("1. Add new employee\n");
printf("2. Update employee information\n");
printf("3. View all employees\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1: {
if (numEmployees < MAX_EMPLOYEES) {
struct Employee newEmployee;

printf("Enter name: ");


scanf("%s", newEmployee.name);

printf("Enter age: ");


scanf("%d", &newEmployee.age);

printf("Enter salary: ");


scanf("%f", &newEmployee.salary);

printf("Enter job title: ");


scanf("%s", newEmployee.title);

employees[numEmployees] = newEmployee;
numEmployees++;

printf("Employee added.\n");
} else {
printf("Maximum number of employees reached.\n");
}
break;
}
case 2: {
int index;

printf("Enter employee index: ");


scanf("%d", &index);

if (index >= 0 && index < numEmployees) {


struct Employee updatedEmployee;

printf("Enter name: ");


scanf("%s", updatedEmployee.name);

printf("Enter age: ");


scanf("%d", &updatedEmployee.age);

printf("Enter salary: ");


scanf("%f", &updatedEmployee.salary);

printf("Enter job title: ");


scanf("%s", updatedEmployee.title);

employees[index] = updatedEmployee;

printf("Employee information updated.\n");


} else {
printf("Invalid employee index.\n");
}
break;
}
case 3: {
printf("Employees:\n");

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


struct Employee employee = employees[i];

printf("%s\t%d\t%.2f\t%s\n", employee.name, employee.age, employee.salary,


employee.title);
}
break;
}
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 4);

return 0;
}
Program 9 :
#include <stdio.h>

#define MAX_STUDENTS 100


#define MAX_COURSES 10

float grades[MAX_STUDENTS][MAX_COURSES];
char student_names[MAX_STUDENTS][50];
char course_names[MAX_COURSES][50];
int num_students = 0;
int num_courses = 0;

void add_student(char name[50]) {


if (num_students == MAX_STUDENTS) {
printf("Error: Maximum number of students reached.\n");
return;
}
strcpy(student_names[num_students], name);
num_students++;
}

void add_course(char name[50]) {


if (num_courses == MAX_COURSES) {
printf("Error: Maximum number of courses reached.\n");
return;
}
strcpy(course_names[num_courses], name);
num_courses++;
}

void enter_grade(int student_idx, int course_idx, float grade) {


grades[student_idx][course_idx] = grade;
}

float calculate_student_average(int student_idx) {


float total_grade = 0;
for (int i = 0; i < num_courses; i++) {
total_grade += grades[student_idx][i];
}
return total_grade / num_courses;
}

float calculate_gpa() {
float total_gpa = 0;
for (int i = 0; i < num_students; i++) {
total_gpa += calculate_student_average(i);
}
return total_gpa / num_students;
}

int main() {
add_student("Alice");
add_student("Bob");
add_course("Math");
add_course("English");

enter_grade(0, 0, 80.0);
enter_grade(0, 1, 90.0);
enter_grade(1, 0, 85.0);
enter_grade(1, 1, 95.0);

printf("Student averages:\n");
for (int i = 0; i < num_students; i++) {
printf("%s: %.2f\n", student_names[i], calculate_student_average(i));
}

printf("Overall GPA: %.2f\n", calculate_gpa());

return 0;
}
Program 10 :
#include <stdio.h>
#include <string.h>

#define MAX_PRODUCTS 100


#define MAX_NAME_LEN 50

struct product {
char name[MAX_NAME_LEN];
float price;
int quantity;
};

void print_menu();
void add_product(struct product products[], int *num_products);
void update_product(struct product products[], int num_products);
void list_products(struct product products[], int num_products);

int main() {
struct product products[MAX_PRODUCTS];
int num_products = 0;
int choice;

do {
print_menu();
scanf("%d", &choice);
switch (choice) {
case 1:
add_product(products, &num_products);
break;
case 2:
update_product(products, num_products);
break;
case 3:
list_products(products, num_products);
break;
case 4:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 4);

return 0;
}

void print_menu() {
printf("\n");
printf("1. Add product\n");
printf("2. Update product\n");
printf("3. List products\n");
printf("4. Exit\n");
printf("Enter your choice: ");
}

void add_product(struct product products[], int *num_products) {


if (*num_products >= MAX_PRODUCTS) {
printf("Maximum number of products reached.\n");
return;
}

struct product new_product;


printf("Enter product name: ");
scanf("%s", new_product.name);
printf("Enter product price: ");
scanf("%f", &new_product.price);
printf("Enter product quantity: ");
scanf("%d", &new_product.quantity);

products[*num_products] = new_product;
(*num_products)++;
}

void update_product(struct product products[], int num_products) {


char name[MAX_NAME_LEN];
printf("Enter product name: ");
scanf("%s", name);

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


if (strcmp(name, products[i].name) == 0) {
printf("Enter new product price: ");
scanf("%f", &products[i].price);
printf("Enter new product quantity: ");
scanf("%d", &products[i].quantity);
return;
}
}

printf("Product not found.\n");


}

void list_products(struct product products[], int num_products) {


printf("Product Name\tPrice\tQuantity\n");
for (int i = 0; i < num_products; i++) {
printf("%s\t%.2f\t%d\n", products[i].name, products[i].price, products[i].quantity);
}
}
Program 11:
#include <stdio.h>

#define NUM_SALESPEOPLE 5
#define NUM_MONTHS 12

int main() {
int sales[NUM_SALESPEOPLE][NUM_MONTHS];
int person_total[NUM_SALESPEOPLE] = {0};
int month_total[NUM_MONTHS] = {0};
int company_total = 0;

// Read sales data into 2D array


for (int i = 0; i < NUM_SALESPEOPLE; i++) {
printf("Enter sales for salesperson %d: ", i+1);
for (int j = 0; j < NUM_MONTHS; j++) {
scanf("%d", &sales[i][j]);
person_total[i] += sales[i][j];
month_total[j] += sales[i][j];
company_total += sales[i][j];
}
}

// Display report
printf("\nSalesperson\tTotal Sales\n");
for (int i = 0; i < NUM_SALESPEOPLE; i++) {
printf("%d\t\t%d\n", i+1, person_total[i]);
}

printf("\nMonth\tTotal Sales\n");
for (int i = 0; i < NUM_MONTHS; i++) {
printf("%d\t%d\n", i+1, month_total[i]);
}

printf("\nTotal company sales: %d\n", company_total);

return 0;
}
Program 12 :
#include <stdio.h>

int main() {
int scores[20][3];
char names[20][20];
float class_avg = 0;

// input scores and names


for (int i = 0; i < 20; i++) {
printf("Enter name of student %d: ", i + 1);
scanf("%s", names[i]);
printf("Enter scores for %s (out of 100): ", names[i]);
for (int j = 0; j < 3; j++) {
scanf("%d", &scores[i][j]);
}
}

// calculate and print average scores


printf("\nName\tAverage Score\n");
for (int i = 0; i < 20; i++) {
float avg = 0;
for (int j = 0; j < 3; j++) {
avg += scores[i][j];
}
avg /= 3;
printf("%s\t%.2f\n", names[i], avg);
class_avg += avg;
}
class_avg /= 20;
printf("Class Average Score: %.2f\n", class_avg);

return 0;
}
Program 13 :
#include <stdio.h>

#define MAX_PRODUCTS 100 // maximum number of products that can be stored in the
inventory

int main() {
char products[MAX_PRODUCTS][50]; // 2D array to store the product names
float prices[MAX_PRODUCTS]; // array to store the prices
int quantities[MAX_PRODUCTS]; // array to store the quantities
int n, i; // n is the number of products

// take input for the number of products


printf("Enter the number of products: ");
scanf("%d", &n);

// take input for the product details and store them in the arrays
for (i = 0; i < n; i++) {
printf("Enter the details for product %d:\n", i+1);
printf("Product name: ");
scanf("%s", products[i]);
printf("Price: ");
scanf("%f", &prices[i]);
printf("Quantity: ");
scanf("%d", &quantities[i]);
}
// display the report of items that are out of stock
printf("\nItems that are out of stock:\n");
for (i = 0; i < n; i++) {
if (quantities[i] == 0) {
printf("%s\n", products[i]);
}
}

return 0;
}
Program 14 :
#include <stdio.h>

#include <stdlib.h>

#define MAX_STUDENTS 100

#define MAX_ASSIGNMENTS 100

int main() {

int num_students, num_assignments, i, j;

char student_names[MAX_STUDENTS][100];

int grades[MAX_STUDENTS][MAX_ASSIGNMENTS];

float student_averages[MAX_STUDENTS] = {0};

float class_average = 0;

printf("Enter number of students: ");

scanf("%d", &num_students);

printf("Enter number of assignments: ");

scanf("%d", &num_assignments);

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

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

scanf("%s", student_names[i]);

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

printf("Enter grade for assignment %d: ", j + 1);

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

student_averages[i] += grades[i][j];
class_average += grades[i][j];

student_averages[i] /= num_assignments;

class_average /= (num_students * num_assignments);

printf("\nStudent grades:\n");

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

printf("%s: ", student_names[i]);

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

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

printf("(average: %.2f)\n", student_averages[i]);

printf("\nClass average: %.2f\n", class_average);

return 0;

}
Program 15 :
#include <stdio.h>

#define NUM_PRODUCTS 3
#define NUM_DAYS 7

int main() {
int sales[NUM_PRODUCTS][NUM_DAYS];
int total_sales_per_day[NUM_DAYS] = {0};
int total_sales_per_product[NUM_PRODUCTS] = {0};

// Enter sales for each day of the week for each product
for (int i = 0; i < NUM_PRODUCTS; i++) {
printf("Enter sales for product %d:\n", i+1);
for (int j = 0; j < NUM_DAYS; j++) {
printf("Day %d: ", j+1);
scanf("%d", &sales[i][j]);
}
}

// Calculate total sales per day


for (int i = 0; i < NUM_DAYS; i++) {
for (int j = 0; j < NUM_PRODUCTS; j++) {
total_sales_per_day[i] += sales[j][i];
}
}
// Calculate total sales per product
for (int i = 0; i < NUM_PRODUCTS; i++) {
for (int j = 0; j < NUM_DAYS; j++) {
total_sales_per_product[i] += sales[i][j];
}
}

// Display total sales per day


printf("\nTotal sales per day:\n");
for (int i = 0; i < NUM_DAYS; i++) {
printf("Day %d: %d\n", i+1, total_sales_per_day[i]);
}

// Display total sales per product


printf("\nTotal sales per product:\n");
for (int i = 0; i < NUM_PRODUCTS; i++) {
printf("Product %d: %d\n", i+1, total_sales_per_product[i]);
}

return 0;
}
Program 16 :
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#define MAX_PRODUCTS 100

typedef struct {

int id;

char name[100];

int quantity;

} Product;

Product products[MAX_PRODUCTS];

int num_products = 0;

void add_product() {

if (num_products == MAX_PRODUCTS) {

printf("Error: Maximum number of products reached.\n");

return;

Product new_product;

printf("Enter product ID: ");

scanf("%d", &new_product.id);

printf("Enter product name: ");

scanf("%s", new_product.name);
printf("Enter quantity: ");

scanf("%d", &new_product.quantity);

products[num_products++] = new_product;

printf("Product added successfully.\n");

void remove_product() {

int id, i;

printf("Enter product ID: ");

scanf("%d", &id);

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

if (products[i].id == id) {

products[i] = products[--num_products];

printf("Product removed successfully.\n");

return;

printf("Error: Product not found.\n");

void update_quantity() {

int id, i, quantity;

printf("Enter product ID: ");

scanf("%d", &id);
for (i = 0; i < num_products; i++) {

if (products[i].id == id) {

printf("Enter new quantity: ");

scanf("%d", &quantity);

products[i].quantity = quantity;

printf("Quantity updated successfully.\n");

return;

printf("Error: Product not found.\n");

void list_products() {

int i;

printf("%-10s%-30s%-10s\n", "ID", "Name", "Quantity");

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

printf("%-10d%-30s%-10d\n", products[i].id, products[i].name, products[i].quantity);

int main() {

int choice;

while (1) {

printf("\n1. Add product\n");

printf("2. Remove product\n");

printf("3. Update quantity\n");

printf("4. List products\n");


printf("5. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

add_product();

break;

case 2:

remove_product();

break;

case 3:

update_quantity();

break;

case 4:

list_products();

break;

case 5:

printf("Exiting program.\n");

exit(0);

default:

printf("Error: Invalid choice.\n");

return 0;

You might also like