0% found this document useful (0 votes)
8 views

Submission to Programming and Logic Building

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Submission to Programming and Logic Building

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Submission to Programming and Logic

Building
Batch: 2024-28
Sem: I
1. Hello World
INPUT:
#include <stdio.h>

int main() {
printf("Hello, This is my first c program World!\n");
return 0;
}

OUTPUT:
Hello, This is my first c program World!

--------------------------------
Process exited after 0.01401 seconds with return value 0

2.ADDITION PROGRAM
INPUT:
#include <stdio.h>

int main() {
int num1, num2, sum;
// Ask the user to input two numbers
printf("Enter first number: ");
scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

// Calculate the sum of the two numbers


sum = num1 + num2;

// Display the result


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

return 0;
}

OUTPUT:
Enter first number: 5
Enter second number: 8
The sum of 5 and 8 is: 13

--------------------------------
Process exited after 11.83 seconds with return value 0
3.CALCULATOR PROGRAM
INPUT:
#include <stdio.h>

int main() {
float num1, num2, result;
char operator;

// Ask for the first number


printf("Enter first number: ");
scanf("%f", &num1);

// Ask for the operator


printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator); // The space before %c is used
to consume any leftover newline character

// Ask for the second number


printf("Enter second number: ");
scanf("%f", &num2);

// Perform the operation based on the operator


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.\n");
}
break;
default:
printf("Invalid operator!\n");
}

return 0;
}

OUTPUT:
Enter first number: 43
Enter operator (+, -, *, /): *
Enter second number: 8
Result: 344.00

--------------------------------
Process exited after 19.03 seconds with return value 0

4.RESULT SHEET PROGRAM


OUTPUT:
#include <stdio.h>

int main() {
char name[100];
int prn, sub1, sub2, sub3, sub4;
float totalmarks, percentage;
char grade;

// Input student details


printf("Welcome to the program\n");
printf("Enter student name: ");
// Using %[^\n] to read multi-word names
scanf("%[^\n]s", name);

printf("Enter your PRN: ");


scanf("%d", &prn);

// Input marks for 4 subjects


printf("Enter marks for 4 subjects (out of 100):\n");
scanf("%d %d %d %d", &sub1, &sub2, &sub3, &sub4);
// Calculate total marks and percentage
totalmarks = sub1 + sub2 + sub3 + sub4;
printf("Your total marks obtained is %.2f\n",
totalmarks);

percentage = (totalmarks / 400.0) * 100; // Total marks


out of 400

printf("Your percentage is %.2f%%\n", percentage);

// Determine grade based on percentage


if (percentage >= 90) {
grade = 'A';
} else if (percentage >= 80) {
grade = 'B';
} else if (percentage >= 70) {
grade = 'C';
} else if (percentage >= 60) {
grade = 'D';
} else {
grade = 'F';
}

// Display grade
printf("Your grade is: %c\n", grade);

return 0;
}
OUTPUT:
Welcome to the program
Enter student name: ANSH JAIN
Enter your PRN: 24070121005
Enter marks for 4 subjects (out of 100):
70
80
90
85
Your total marks obtained is 325.00
Your percentage is 81.25%
Your grade is: B

--------------------------------
Process exited after 57.44 seconds with return value 0

5. Reaction calculation for Simple Beam


INPUT:
#include <stdio.h>

int main() {
// Declare variables
float L, P1, P2, w; // Length of beam, point loads, and
UDL
float a, b; // Positions of point loads
float RA, RB; // Reaction forces at supports
float totalLoad, momentAboutA;

// Input beam length and point loads


printf("Enter length of the beam (in meters): ");
scanf("%f", &L);

printf("Enter the first point load (P1) in Newtons: ");


scanf("%f", &P1);
printf("Enter the distance of first point load from left
support (a): ");
scanf("%f", &a);

printf("Enter the second point load (P2) in Newtons: ");


scanf("%f", &P2);
printf("Enter the distance of second point load from left
support (b): ");
scanf("%f", &b);

printf("Enter the uniformly distributed load (w) in


Newtons per meter: ");
scanf("%f", &w);

// Step 1: Calculate the total load


totalLoad = P1 + P2 + w * L;

// Step 2: Calculate the moment about point A (taking


moment about A to find RB)
momentAboutA = P1 * a + P2 * b + (w * L * L / 2) / 2;

// Step 3: Calculate reaction at B (RB)


RB = momentAboutA / L;

// Step 4: Calculate reaction at A (RA)


RA = totalLoad - RB;

// Output the results


printf("\nThe reaction at support A (RA) is: %.2f N\n",
RA);
printf("The reaction at support B (RB) is: %.2f N\n", RB);

return 0;
}

OUTPUT:
Enter length of the beam (in meters): 8
Enter the first point load (P1) in Newtons: 4
Enter the distance of first point load from left support (a):
5
Enter the second point load (P2) in Newtons: 9
Enter the distance of second point load from left support
(b): 3
Enter the uniformly distributed load (w) in Newtons per
meter: 5
The reaction at support A (RA) is: 37.13 N
The reaction at support B (RB) is: 15.88 N

--------------------------------
Process exited after 65.04 seconds with return value 0

6. Multiplication Function
INPUT:
#include <stdio.h>

// Function to multiply two numbers


float multiply(float num1, float num2) {
return num1 * num2;
}

int main() {
float num1, num2, result;

// Input two numbers


printf("Enter first number: ");
scanf("%f", &num1);

printf("Enter second number: ");


scanf("%f", &num2);

// Call the multiply function


result = multiply(num1, num2);
// Display the result
printf("The product of %.2f and %.2f is: %.2f\n", num1,
num2, result);

return 0;
}

OUTPUT:
Enter first number: 8
Enter second number: 5
The product of 8.00 and 5.00 is: 40.00

--------------------------------
Process exited after 6.648 seconds with return value 0

7. Calculator Function Program


INPUT:
#include <stdio.h>

// Function declarations
float add(float num1, float num2);
float subtract(float num1, float num2);
float multiply(float num1, float num2);
float divide(float num1, float num2);

int main() {
float num1, num2, result;
int choice;

// Display menu
printf("Simple Calculator\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);

// Input two numbers


printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);

// Perform the operation based on the user's choice


switch (choice) {
case 1:
result = add(num1, num2);
printf("The result of %.2f + %.2f is: %.2f\n", num1,
num2, result);
break;
case 2:
result = subtract(num1, num2);
printf("The result of %.2f - %.2f is: %.2f\n", num1,
num2, result);
break;
case 3:
result = multiply(num1, num2);
printf("The result of %.2f * %.2f is: %.2f\n", num1,
num2, result);
break;
case 4:
if (num2 != 0) {
result = divide(num1, num2);
printf("The result of %.2f / %.2f is: %.2f\n", num1,
num2, result);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Invalid choice! Please enter a valid option (1-
4).\n");
}

return 0;
}

// Function to perform addition


float add(float num1, float num2) {
return num1 + num2;
}

// Function to perform subtraction


float subtract(float num1, float num2) {
return num1 - num2;
}

// Function to perform multiplication


float multiply(float num1, float num2) {
return num1 * num2;
}

// Function to perform division


float divide(float num1, float num2) {
return num1 / num2;
}

OUTPUT:
Simple Calculator
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice (1-4): 4
Enter first number: 55
Enter second number: 90
The result of 55.00 / 90.00 is: 0.61

--------------------------------
Process exited after 19.28 seconds with return value 0

8. Calculation of reactions including moment


INPUT:
#include <stdio.h>

int main() {
double L; // Length of the beam
double P; // Magnitude of the load
double a; // Distance of the load from the left
support
double R1, R2; // Reactions at the left and right
supports
double M; // Moment about a point

// Input the beam length, load position, and magnitude


of the load
printf("Enter the length of the beam (L): ");
scanf("%lf", &L);
printf("Enter the magnitude of the load (P): ");
scanf("%lf", &P);
printf("Enter the distance of the load from the left
support (a): ");
scanf("%lf", &a);

// Validate input
if (a > L || a < 0) {
printf("Invalid load position! It should be between 0
and L.\n");
return 1;
}

// Calculate reactions using static equilibrium equations


R2 = (P * a) / L; // Reaction at the right support
R1 = P - R2; // Reaction at the left support

// Calculate moment about the left support


M = P * a;

// Output the results


printf("\nReaction at the left support (R1): %.2f N\n",
R1);
printf("Reaction at the right support (R2): %.2f N\n",
R2);
printf("Moment about the left support: %.2f N*m\n",
M);

return 0;
}
OUTPUT:
Enter the length of the beam (L): 10
Enter the magnitude of the load (P): 500
Enter the distance of the load from the left support (a): 4

Reaction at the left support (R1): 300.00 N


Reaction at the right support (R2): 200.00 N
Moment about the left support: 2000.00 N*m

--------------------------------
Process exited after 36.57 seconds with return value 0

9. C Structure program for student Marks


INPUT:
#include <stdio.h>

#define MAX_STUDENTS 100

// Structure definition for Student


struct Student {
int prn; // PRN number
char name[50]; // Student name
float marks[3]; // Marks for 3 subjects
float total; // Total marks
float average; // Average marks
};
int main() {
int n, i, j;
struct Student students[MAX_STUDENTS];

// Input the number of students


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

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


printf("Invalid number of students! Must be between
1 and %d.\n", MAX_STUDENTS);
return 1;
}

// Input student details


for (i = 0; i < n; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("PRN: ");
scanf("%d", &students[i].prn);
printf("Name: ");
scanf(" %[^\n]", students[i].name); // Read string
with spaces
students[i].total = 0;

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


printf("Enter marks for subject %d: ", j + 1);
scanf("%f", &students[i].marks[j]);
students[i].total += students[i].marks[j];
}

students[i].average = students[i].total / 3.0;


}

// Display student details


printf("\n%-10s %-20s %-10s %-10s\n", "PRN", "Name",
"Total", "Average");
printf("----------------------------------------------------------\n");
for (i = 0; i < n; i++) {
printf("%-10d %-20s %-10.2f %-10.2f\n",
students[i].prn, students[i].name,
students[i].total, students[i].average);
}

return 0;
}

OUTPUT:
Enter the number of students: 2

Enter details for student 1:


PRN: 105
Name: ANSH
Enter marks for subject 1:
69
Enter marks for subject 2: 96
Enter marks for subject 3: 77

Enter details for student 2:


PRN: 167
Name: SHIKHAR
Enter marks for subject 1: 100
Enter marks for subject 2: 69
Enter marks for subject 3: 70

PRN Name Total Average


----------------------------------------------------------
105 ANSH 242.00 80.67
167 SHIKHAR 239.00 79.67

--------------------------------
Process exited after 621.9 seconds with return value 0

10. C Structure program for Books


INPUT:
#include <stdio.h>

#define MAX_BOOKS 100

// Structure definition for Book


struct Book {
int id; // Book ID
char title[50]; // Title of the book
char author[50]; // Author of the book
float price; // Price of the book
int quantity; // Quantity in stock
};

int main() {
int n, i;
struct Book books[MAX_BOOKS];

// Input the number of books


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

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


printf("Invalid number of books! Must be between 1
and %d.\n", MAX_BOOKS);
return 1;
}

// Input details for each book


for (i = 0; i < n; i++) {
printf("\nEnter details for book %d:\n", i + 1);
printf("Book ID: ");
scanf("%d", &books[i].id);
printf("Title: ");
scanf(" %[^\n]", books[i].title); // Read string with
spaces
printf("Author: ");
scanf(" %[^\n]", books[i].author);
printf("Price: ");
scanf("%f", &books[i].price);
printf("Quantity: ");
scanf("%d", &books[i].quantity);
}

// Display book details


printf("\n%-10s %-30s %-20s %-10s %-10s\n", "Book ID",
"Title", "Author", "Price", "Quantity");

printf("--------------------------------------------------------------------
----------\n");
for (i = 0; i < n; i++) {
printf("%-10d %-30s %-20s %-10.2f %-10d\n",
books[i].id, books[i].title, books[i].author,
books[i].price, books[i].quantity);
}

return 0;
}

OUTPUT:
Enter the number of books: 1

Enter details for book 1:


Book ID: 21
Title: LAILA MAJNU
Author: KAIF
Price: 200
Quantity: 2

Book ID Title Author Price


Quantity
------------------------------------------------------------------------------
21 LAILA MAJNU KAIF 200.00 2

--------------------------------
Process exited after 39.72 seconds with return value 0

You might also like