Some C programming problems and solutions for Civil Engineering C program course
Some C programming problems and solutions for Civil Engineering C program course
Program Code:
#include<stdio.h>
#include <math.h>
int main ()
{
float a,b,c,x1,x2;
printf("Enter the value of a=");
scanf("%f",&a);
printf("Enter the value of b=");
scanf("%f",&b);
printf("Enter the value of c=");
scanf("%f",&c);
x1=(-b+sqrt(pow(b,2)-(4*a*c)))/(2*a);
x2=(-b-sqrt(pow(b,2)-(4*a*c)))/(2*a);
printf("1st root of x is equal=%f\n",x1);
printf("2nd root of x is equal=%f",x2);
}
Fig 3: Picture of the program window (task 03)
Task 04: Write a program that takes four variables a, b, c, and d. Calculate and
display the ratio of the sum of a and b to the difference of c and d. Ensure that
the program checks if (c - d) is not zero before performing the division to avoid
division by zero.
Program Code:
#include<stdio.h>
int main ()
{
float a,b,c,d,ratio;
printf("Enter the value of a=");
scanf("%d",&a);
printf("Enter the value of b=");
scanf("%d",&b);
printf("Enter the value of c=");
scanf("%d",&c);
printf("Enter the value of c=");
scanf("%d",&d);
if(c-d!=0)
{
ratio=(a+b)/(c-d);
printf("Ratio is equal=%f\n",ratio);
}
}
Fig 4: Picture of the program window (task 04)
Task 05: Write a program to Determine if a Number is Even or Odd using the
equality operator.
Program Code:
#include<stdio.h>
int main()
{
int n;
printf("enter a number=");
scanf("%d",&n);
if (n%2==0)
printf("Even number");
else
printf("odd number");
}
Program Code:
#include<stdio.h>
int main()
{
int n;
printf("enter a number=");
scanf("%d",&n);
if (n%2!=0)
printf("Odd number");
else
printf("Even number");
}
Program Code:
#include<stdio.h>
int main()
{ int n;
printf("Enter mark=");
scanf ("%d",&n);
if(n>79)
printf("Letter Grade: A+");
else if(n>59)
printf("Letter Grade: B+");
else if(n>49)
printf("Letter Grade: C+");
else if(n>39)
printf("Letter Grade: D+");
else
printf("Letter Grade: F");
}
Code Explanation:
1) #include<stdio.h>
This line includes the Standard Input Output library (stdio.h), which allows the program to use
functions like printf and scanf for input and output operations.
2) int main() {}
The main function is the entry point of the program. Execution starts from here.
3) int n; It declares an integer variable n which will store the user input.
4) printf("Enter marks=");
scanf("%d",&n);
Printf prints the message "Enter mark=" to prompt the user to input their mark. Scanf takes an
integer input from the user and stores it in the variable n. The & before n represents the
address of n where the input will be stored.
5) If (n > 79)
printf("Letter Grade: A+");
Checks if n is greater than 79. If true, it prints "Letter Grade: A+", assigning an A+ for scores
above 79.
6) else if(n > 59)
printf("Letter Grade: B+");
Checks if n is greater than 59 but less than or equal to 79. If true, it prints "Letter Grade: B+",
assigning a B+ for scores between 60 and 79.
7) else if(n > 49)
printf("Letter Grade: C+");
Checks if n is greater than 49 but less than or equal to 59. If true, it prints "Letter Grade: C+",
assigning a C+ for scores between 50 and 59.
8) else if(n > 39)
printf("Letter Grade: D+");
Checks if n is greater than 39 but less than or equal to 49. If true, it prints "Letter Grade: D+",
assigning a D+ for scores between 40 and 49.
9) else
printf("Letter Grade: F");
If none of the above conditions are met (i.e., n is 39 or below), the program prints "Letter
Grade: F", assigning an F for failing marks.
Fig 7: Picture of the program window (task 07)
Task 08: Write a program to Check if a Year is a Leap Year.
Program Code:
#include <stdio.h>
int main()
{ int y;
printf("Enter the year =");
scanf("%d",&y);
if(((y%4==0)&&(y%100!=0))||(y%400==0))
printf("The year is leap Year");
else
printf("The year is not Leap Year");
}
Code Explanation:
1) #include<stdio.h>
This line includes the Standard Input Output library (stdio.h), which allows the program to use
functions like printf and scanf for input and output operations.
2) int main() {}
The main function is the entry point of the program. Execution starts from here.
3) int y; It declares an integer variable y which will store the user input.
Program Code:
#include<stdio.h>
int main()
{
int sum,n;
printf ("Cumulative sum of the integers from 01 to 12\n");
sum=0;
n=1;
while(n<=16)
{
sum=sum+n;
n=n+1;
printf("Summation Is Equal To=%d\n",sum);
}
}
Code Explanation:
1) #include<stdio.h>
This line includes the Standard Input Output library (stdio.h), which allows the program to use
functions like printf and scanf for input and output operations.
2) int main() {}
The main function is the entry point of the program. Execution starts from here.
3) int sum, n;
Declares two integer variables: “sum” stores the cumulative total and n serves as a counter for
the loop.
4) Printf ("Cumulative sum of the integers from 01 to 12\n");
Prints a message indicating that the program will calculate the cumulative sum of integers.
5) sum = 0; It initializes sum to 0, which will hold the running total of the sum of integers.
Program Code:
#include <stdio.h>
int main()
{
float sum = (2.0 / 3.0) + (4.0 / 5.0) + (6.0 / 7.0);
printf("The sum of 2/3 + 4/5 + 6/7 is: %.4f\n", sum);
return 0;
}
Program Code:
#include <stdio.h>
int main() {
float L, W, P, b, h, I, x, Q, shear, moment, shear_stress, flexural_stress;
int i;
printf("Enter the total length of the beam (L): "); scanf("%f", &L);
printf("Enter the uniformly distributed load per unit length (W): "); scanf("%f", &W);
printf("Enter the point load at the free end (P): "); scanf("%f", &P);
printf("Enter the width of the beam cross-section (b): "); scanf("%f", &b);
printf("Enter the height of the beam cross-section (h): "); scanf("%f", &h);
I = (b * h * h * h) / 12; Q = (b*h*h)/8;
printf("\nDistance from free end\tShear\tMoment\tShear Stress\tFlexural Stress\n");
for (i = 0; i <= 10; i++) {
x = (10 - i) * L / 10.0;
shear = P + W * x;
moment = P * x + (W * x * x) / 2;
shear_stress = shear*Q / (b * h);
flexural_stress = moment * (h / 2) / I;
printf("%10.2f\t\t%5.2f\t%5.2f\t%7.2f\t%20.2f\n", x, shear, moment, shear_stress,
flexural_stress);
} return 0;
}
Fig 14.1: Picture of the program window (task 14)
A B
Program Code:
#include <stdio.h>
int main() {
float L = 10.0; printf("Total length of the beam (in feet)=%.2f\n",L);
float P = 10.0; printf("Applied load (in kips)=%.2f\n",P);
float M = 10.0; printf("Applied moment (in kip-ft)=%.2f\n",M);
float a = 5.0; printf("Distance of the applied load from the left support (in
feet)=%.2f\n",a);
float RA, RB; printf("Reaction forces at the left and right support is RA and RB\n\n");
float max_shear, max_moment;
RB = (P * a + M) / L; RA = P - RB;
if (a < L / 2) { max_shear = RA; }
else { max_shear = RB; }
max_moment = (RA * a) - M;
printf("Results:\n");
printf("Reaction at the left support (RA): %.2f kips\n", RA);
printf("Reaction at the right support (RB): %.2f kips\n", RB);
printf("Maximum Shear Force: %.2f kips\n", max_shear);
printf("Maximum Bending Moment: %.2f kip-ft\n", max_moment);
return 0;
}
Fig 15: Picture of the program window (task 15)
Task 16: Write a program to calculate the maximum shear and bending moment
of the beam.
Program Code:
#include <stdio.h>
int main() {
float L = 30.0; printf("Total length of the beam (in feet): %.2f\n",L);
float L_udl = 10.0; printf("Length of the UDL (in feet): %.2f\n",L_udl);
float w = 10.0; printf("UDL intensity (in kips/ft): %.2f\n",w);
float P = 10.0; printf("Concentrated load (in kips): %.2f\n",P);
float a = 20.0; printf("Location of concentrated load from the fixed end (in feet): %.2f\n\n",a);
float max_shear, max_moment, V_udl, M_udl, V_P, M_P;
/*Shear and moment due to the UDL*/
V_udl = w * L_udl; /*Total shear force due to the UDL*/
M_udl = (w * L_udl * L_udl) / 2.0; /*Bending moment due to the UDL at the fixed end*/
/*Shear and moment due to the point load*/
V_P = P;
M_P = P * a;
max_shear = V_udl + V_P;
max_moment = M_udl + M_P;
printf("Results:\n");
printf("Maximum Shear Force: %.2f kips\n", max_shear);
printf("Maximum Bending Moment: %.2f kip-ft\n", max_moment);
return 0;
}
Fig 16: Picture of the program window (task 16)
Task 17: Write a program to calculate the shear and bending moment at each
section of the beam.
1 2
Program Code:
#include <stdio.h>
int main() {
float X1, X2, X3, P1, P2, W, R1, R2;
printf("Enter the value of X1 = "); scanf("%f",&X1);
printf("Enter the value of X2 = "); scanf("%f",&X2);
printf("Enter the value of X3 = "); scanf("%f",&X3);
printf("Enter the value of P1 = "); scanf("%f",&P1);
printf("Enter the value of P2 = "); scanf("%f",&P2);
printf("Enter the value of W = "); scanf("%f",&W);
float L = X1 + X2 + X3;
R2 = (P1 * X1 + W * X2 * (X1 + X2 / 2) + P2 * (X1 + X2)) / L;
R1 = P1 + W * X2 + P2 - R2;
printf("Reactions at supports:\n");
printf("R1 = %.2f kips\n", R1);
printf("R2 = %.2f kips\n\n", R2);
printf("Section\tPosition\tShear Force (kips)\tBending Moment (kip-ft)\n");
/*Section X1*/
for (float x = 0; x <= X1; x += 1.0) {
float shear = R1 - P1;
float moment = R1 * x;
printf("X1\t%.2f\t\t%.2f\t\t\t%.2f\n", x, shear, moment);
}
/*Section X2*/
for (float x = X1; x <= X1 + X2; x += 1.0) {
float x_local = x - X1;
float shear = R1 - P1 - W * x_local;
float moment = R1 * x - P1 * (x - X1) - (W * x_local * x_local) / 2.0;
printf("X2\t%.2f\t\t%.2f\t\t\t%.2f\n", x, shear, moment);
}
/*Section X3*/
for (float x = X1 + X2; x <= L; x += 1.0) {
float x_local = x - (X1 + X2);
float shear = R1 - P1 - W * X2 - P2;
float moment = R1 * x - P1 * (x - X1) - W * X2 * (X1 + X2 / 2) - P2 * x_local;
printf("X3\t%.2f\t\t%.2f\t\t\t%.2f\n", x, shear, moment);
}
return 0;
}
Fig 17.1: Picture of the program window (task 17)
Fig 17.2: Picture of the program window (task 17)
Task 18: Write a program to print a right-angled triangle pattern using stars.
Program Code:
1
#include<stdio.h> 2
int main()
{ int i, j;
for(i=1; i<=8; i++)
{
for(j=1; j<=i; j++)
{
printf("* ");
}
printf("\n");
} return 0;
}
int main()
{ int n, i, j, divisors;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Prime number up to %d are:\n",n);
for(i=2; i<=n; i++)
{
divisors=0;
for(j=1; j<=i; j++)
{
if(i%j==0)
divisors++;
}
if(divisors==2)
printf("%d ", i);
}
printf("\n ");
return 0;
}
Fig 19: Picture of the program window (task 19)