Task 01: take a value for each of the variables a and b, and show the
summation of the variables as output.
Program Code:
#include<stdio.h>
int main ()
{
int a, b, sum;
a=40;
b=48;
sum = a+b;
printf("sum is equal to=%d", sum);
}
Fig 1: Picture of the program window (task 01)
Task 02: take two variables a and b, and show the summation of the variables
as output.
Program Code:
#include<stdio.h>
int main ()
{
int a, b, sum;
printf("Enter the value of a=");
scanf("%d",&a);
printf("Enter the value of b=");
scanf("%d",&b);
sum=a+b;
printf("sum is equal to=%d",sum);
}
Fig 2: Picture of the program window (task 02)
Task 03: Write a program that takes three variables, a, b, and c, representing
the coefficients of a quadratic equation of the form ax2+bx+c=0. Calculate and
display the two roots of the equation using the quadratic formula.
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");
}
Fig 5: Picture of the program window (task 05)
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 a number=");
scanf("%d",&n);
Printf displays the message "enter a number=" on the screen, prompting the user to enter an
integer. Scanf takes an integer input from the user and stores it in the variable n. The & symbol
before n gives the address of n where the input will be stored.
5) if (n%2==0)
Checks if n is divisible by 2 (i.e. if n % 2 equals 0). The % operator computes the remainder
when n is divided by 2. If the remainder is 0, n is an even number. The == operator checks if the
left side is equal to the right side.
6) printf("Even number");
If the condition in the if statement is true (i.e., n is even), this line prints "Even number" to the
screen.
7) else
printf("odd number");
If the if condition is false (i.e., n is odd), the else block executes and prints an "odd number" to
the screen.
Task 06: Write a program to Determine if a Number is Even or Odd using the
inequality operator.
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");
}
Fig 6: Picture of the program window (task 06)
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 a number=");
scanf("%d",&n);
Printf displays the message "enter a number=" on the screen, prompting the user to enter an
integer. Scanf takes an integer input from the user and stores it in the variable n. The & symbol
before n gives the address of n where the input will be stored.
5) if (n%2!=0)
Checks if n is not divisible by 2 (i.e., n % 2 does not equal 0). The % operator computes the
remainder when n is divided by 2. If the remainder is not 0, then n is an odd number.
The != operator checks if the left side is not equal to the right side.
6) printf("Odd number");
If the condition in the if statement is true (i.e., n is odd), this line prints "Odd number" to the
screen.
7) else
printf("Even number");
If the if condition is false (i.e., n is even), the else block executes and prints an "Even number"
to the screen.
Task 07: Write a program to determine Letter Grades based on the obtained
marks.
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.
4) printf("Enter the year =");
scanf("%d",&y);
Printf prints the message "Enter the year =" to prompt the user to input a year. Scanf takes an
integer input from the user and stores it in the variable y. The & before y represents the
address of y where the input will be stored.
5) If (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
printf("The year is Leap Year");
This if statement checks if the entered year y is a leap year.
First Condition: (y % 4 == 0) && (y % 100 != 0)
- y % 4 == 0 checks if y is divisible by 4 (a requirement for most leap years).
- y % 100 != 0 checks that y is not divisible by 100, which excludes years like 1900, 1800, etc.,
which are not leap years even though they are divisible by 4.
Second Condition: y % 400 == 0
- This part checks if y is divisible by 400. Centuries like 1600 and 2000 are leap years, as they
are divisible by 400.
If either the first or second condition is true, the program concludes that y is a leap year and
prints "The year is Leap Year".
6) else
printf ("The year is not Leap Year");
If none of the conditions in the if statement are met, the program executes this else block and prints
"The year is not Leap Year", indicating that y is not a leap year.
Fig 8.1: Picture of the program window of leap year
Fig 8.2: Picture of the program window of not leap year
Task 09: Write a program to calculate and display the cumulative sum of integers.
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.
6) n = 1; It initializes n to 1, marking the starting value for the summation loop.
7) while(n <= 12) { }
Begins a while loop that continues as long as n is less than or equal to 16. This loop will
calculate the cumulative sum and display it after each iteration.
8) sum = sum + n;
Adds the current value of n to sum, updating the cumulative total.
9) n = n + 1;
Increments n by 1, moving to the next integer in the sequence.
10) Printf ("Summation Is Equal To=%d\n", sum);
Prints the current value of sum, showing the cumulative total after adding each integer.
Fig 9: Picture of the program window (Task 09)
Task 10: Write a program to calculate and display the sum of fractions:
2 4 6
+ +
3 5 7
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;
}
Fig 10: Picture of the program window (task 10)
Task 11: Write a program to calculate and display the sum of the series:
2 4 6
+ + +...
3 5 7
Program Code:
#include<stdio.h>
int main()
{
float n,i,sum;
sum=0;
printf("Enter the number of terms to calculate the sum: ");
scanf("%f", &n);
for (i = 1; i <= n; i++)
{
sum +=(2 * i) /(2 * i + 1);
}
printf("The sum of the series is: %.4f\n", sum);
return 0;
}
Fig 11: Picture of the program window (task 11)
Task 12: Write a program to calculate and display the sum of the series:
1 2 3
+ + +...
2 3 4
Program Code:
#include<stdio.h>
int main()
{
float n,i,sum;
sum=0;
printf("Enter the number of terms to calculate the sum: ");
scanf("%f", &n);
for (i = 1; i <= n; i++)
{
sum +=i /( i + 1);
}
printf("The sum of the series is: %.4f\n", sum);
return 0;
}
Fig 12: Picture of the program window (task 12)
Task 13: Write a program to calculate and display the sum of even and odd
numbers up to n.
Program Code:
#include <stdio.h>
int main()
{
int n, evensum = 0, oddsum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
if (i % 2 == 0)
{
evensum += i;
}
else
{
oddsum += i;
}
}
printf("Sum of even numbers: %d\n", evensum);
printf("Sum of odd numbers: %d\n", oddsum);
}
Fig 13: Picture of the program window (task 13)
Task 14: Write a program to calculate the maximum shear and bending moment
of the beam at every L/10 distance from the free end.
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)
Fig 14.2: Picture of the program window (task 14)
Task 15: Write a program to calculate the maximum shear and bending moment
of the beam.
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;
}
Fig 18: Picture of the program window (task 18)
Task 19: Write a program to find the prime numbers up to a given number.
Program Code:
1
#include<stdio.h> 2
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)