0% found this document useful (0 votes)
7 views8 pages

C Programming Basics: Five Lab Tasks

The document outlines five basic C programming tasks aimed at beginners, including calculating the sum of two numbers, finding the maximum of three numbers, checking for prime numbers, calculating factorials, and printing the Fibonacci series. Each task introduces fundamental programming concepts such as input/output operations, conditional logic, loops, and handling edge cases. The overall goal is to build a strong foundation in C programming before advancing to more complex topics.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

C Programming Basics: Five Lab Tasks

The document outlines five basic C programming tasks aimed at beginners, including calculating the sum of two numbers, finding the maximum of three numbers, checking for prime numbers, calculating factorials, and printing the Fibonacci series. Each task introduces fundamental programming concepts such as input/output operations, conditional logic, loops, and handling edge cases. The overall goal is to build a strong foundation in C programming before advancing to more complex topics.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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.

Common questions

Powered by AI

Yes, the concepts from basic C tasks such as input/output operations, arithmetic calculations, and data types provide a foundation that is directly applicable to advanced topics. For instance, memory management in arrays entails understanding and manipulating indices similar to input/output using loops for accessing data. Arithmetic operations are integral for iterating over objects in structures like arrays or modifying data in pointers. A solid understanding of basic data types aids in appropriately using and manipulating complex data structures efficiently .

Variable swapping in generating the Fibonacci series adjusts the order of number storage between iterations. By assigning the next term calculated from the previous two terms to variables, it prepares the variables for the next iteration without needing additional storage or complex operations. This aids in understanding how iterative processes function by showing how current state values directly influence future calculations, reinforcing the handling of series and sequences logically .

It is vital for beginners to focus on control structures because they form the backbone of program logic. Understanding conditional logic through tasks like finding the maximum of three numbers is essential for making decisions based on varying conditions. Similarly, loops, as seen in prime checking or Fibonacci series generation, are crucial for executing repeated operations efficiently. Mastery of these core concepts enables programmers to handle complex logic flow and iterative processing before engaging with arrays and functions, which rely on a solid understanding of basic logic and flow control .

Printing a Fibonacci series serves several educational purposes for novice C programmers. It demonstrates sequence generation through loops, requiring an understanding of iterative processes where each term depends on previous ones. This task helps improve pattern recognition, logical sequencing, and the use of variable swapping to maintain program flow . It reinforces the ability to think about processes algorithmically, which is crucial in programming .

The process involves using if-else statements to evaluate logical conditions that compare three numbers to each other. The program determines which number is the maximum by checking if one number is greater than or equal to the others successively. The significance of this lies in teaching programmers to apply decision-making structures, enabling the program to branch out and choose outcomes based on input data conditions, which is fundamental for solving real-world programming problems .

Calculating the factorial of a number highlights the importance of choosing appropriate data types through the use of an unsigned long long variable to store the factorial, accommodating potentially large results that exceed the typical range of standard integer types . Additionally, handling edge cases like negative numbers requires logical condition checks before proceeding with calculations, demonstrating the importance of robust programming practices .

Implementing a program to determine if a number is prime fosters an understanding of logical structure by requiring the programmer to execute division checks efficiently and conclude with a valid outcome. It solidifies knowledge in loop construction and exit conditions (e.g., breaking the loop when a factor is found). Additionally, it teaches the value of control flow in determining and presenting the final state of boolean-like flags, enhancing logical rigor and procedural thinking .

The task of checking if a number is prime requires the use of loops to iterate through potential divisors and logical checks to determine if the number has any divisors other than 1 and itself. This enhances understanding of efficient iteration by demonstrating how to limit loops to necessary checks (i.e., up to num/2) and employs flags to track and change the program's state based on conditions met during iteration .

Beginners develop foundational skills such as understanding basic input/output operations using functions like scanf and printf . They also learn about data types and simple arithmetic operations while calculating the sum of two numbers. The task of finding the maximum of three numbers provides an introduction to conditional logic through if-else statements, which is essential for decision-making and handling real-world conditions .

Edge case handling in calculating the factorial is illustrated by checking if the input number is negative. If so, the program outputs a message stating that factorial is not defined for negative numbers, preventing calculation and possible erroneous results. This illustrates the importance of verifying input validation and appropriate handling of special cases to maintain program stability and correctness .

You might also like