Data Structure Laboratory
Course Code:CSE 1414
NAME : Jahidul Islam Sohel
ROLL : 0222420005101281
SECTION : G
LAB TASK : 01
______________________________________________
T1. Calculate the sum of two numbers.
Sol.
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}
T2. Find the maximum of three numbers.
Sol.
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a >= b && a >= c)
printf("Maximum: %d\n", a);
else if (b >= a && b >= c)
printf("Maximum: %d\n", b);
else
printf("Maximum: %d\n", c);
return 0;
}
T3. Check whether a number is prime or not.
Sol.
#include <stdio.h>
int main() {
int num, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1)
isPrime = 0;
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}
T4. Calculate the factorial of a number.
Sol.
#include <stdio.h>
int main() {
int n, i;
unsigned long long factorial = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n < 0)
printf("Factorial is not defined for negative numbers.\n");
else {
for (i = 1; i <= n; i++)
factorial *= i;
printf("Factorial of %d is %llu\n", n, factorial);
}
return 0;
}
T5. Print Fibonacci series up to a given number.
Sol.
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
printf("\n");
return 0;
}
Discussion:
The five C programs presented are foundational tasks for beginners learning structured
programming. Each task introduces a core concept in a simple and practical way:
1. Sum of Two Numbers
This task introduces basic input/output operations using scanf and printf, as well as simple
arithmetic. It helps learners become familiar with syntax and data types.
2. Find the Maximum of Three Numbers
This program uses conditional logic (if-else) to compare values. It teaches decision-making
structures, which are essential for handling real-world conditions and branching logic.
3. Check Whether a Number is Prime
This task uses loops (for) and logical checking to determine whether a number has any
divisors. It introduces efficient iteration, condition checking, and the use of flags for result
tracking.
4. Calculate the Factorial of a Number
Factorial calculation reinforces the use of loops and cumulative operations. It also highlights
how to handle edge cases, such as negative numbers, and emphasizes the importance of
variable data types for large results.
5. Print Fibonacci Series
This program demonstrates sequence generation using a loop and variable swapping. It is
useful for understanding how each term depends on the previous ones and improves
thinking in terms of patterns and iterative processes.
Overall, these tasks build a strong base in C programming by focusing on input/output,
control structures, loops, and basic problem-solving techniques. Mastery of these concepts
is crucial before moving on to more advanced topics like arrays, functions, and pointers.