Assiggnment F CP
Assiggnment F CP
Assiggnment F CP
BASIC PROGRAMS
Program 1:
Aim: Introduction to C programming with a simple program to print "Hello, World!"
Theory:
1. #include:
#include instructs the compiler to add the content of stdio. Above program has hash
define statements which is included within the (standard input-output header) file so
on.
2. Purpose of stdio. h: This file contains declarations for the standard I/O functions such as
printf, scanf etc. This program uses printf() to print the output so stdio. h must be
included.
3. int main():
Main Function: The execution of every C program begins from the main() function. The
int before main means that the function returns int values (in this case, 0 to indicate
successful execution).
main() function: It is the main and always mandatory to write this; that is entry point of
any c program, So Your Program start with it.
4. { }:
Curly Braces: The curly braces { } are used to denote the main() function nodes. If the
program is run, all of the code inside these braces will be executed.
5. printf("Hello World!"):
printf() Function : This is an library function from stdio. Output (print) text to the console
were what h used for.
print() function followed by parentheses () and inside those, the string Hello World! is
provided as an argument. We will print that string to the screen
Semicolon;:.making in C, after every statement we put a semicolon. This signals the
compiler that this statement is now complete.
6. return 0;
Return statement – When program ends this statement returns integer 0 to operating
system A return of 0 means the program executed successfully with no errors being
produced. This is because in int main(), a function which was declared as an integer, we
need to return some value from int. Here, 0 is used as the success completion
benchmark.
Program code:
#include<stdio.h>
int main()
printf("Hello World!");
Output:
Conclusion:
The "Hello, World!" program serves as a foundational exercise in C programming, allowing
beginners to grasp fundamental concepts in a simple and clear manner. It sets the stage for
more complex programming tasks, such as working with variables, loops, conditionals, and
functions. Through this program, one learns the importance of correct syntax, the inclusion of
necessary libraries, and the basic structure of a C program.
Program 2:
Aim: Program to perform basic arithmetic operations (addition, subtraction, multiplication,
division).
Theory:
1. Variable Declaration:
int num1, num2, sum, sub, mul: These integer variables are declared to store the input
numbers (num1, num2) and the results of the arithmetic operations (sum, sub, mul).
float div: The div variable is declared as a float to store the result of the division
operation. Since division may result in decimal values, a floating-point variable is
necessary.
char operator: This variable is declared to store the arithmetic operator (+, -, *, /) entered
by the user.
2. User Input:
The program uses the scanf() function to take input from the user:
scanf("%d", &num1); takes the first integer.
scanf("%d", &num2); takes the second integer.
scanf("%c", &operator); takes the operator, but note that there are multiple scanf() calls
for the operator due to the newline character issue explained below.
3. Switch Statement:
The switch statement evaluates the value of the operator variable and executes the
corresponding case:
case '+': Adds the two numbers and stores the result in sum.
Program Code:
#include<stdio.h>
int main()
int num1,num2,sum,sub,mul;
float div;
char operator;
scanf("%d",&num1);
scanf("%d",&num2);
scanf("%c",&operator);
scanf("%c",&operator);
scanf("%c",&operator);
switch (operator){
break;
break;
break;
if(num2==0)
printf("Not divisible");
else
break;
return 0;
Output:
1. Addition:
2. Subtraction:
4. Division:
Conclusion:
This program showcases the use of a switch statement for handling multiple cases, user input,
arithmetic operations, and error handling for division by zero. It demonstrates the fundamental
control structures of C, which are important for solving complex problems in a structured
manner.
int p, r, t;: These variables are integers that represent the principal amount (p), the rate of
interest (r), and the time (t).
float si;: This variable is declared as a float type to store the calculated value of the
simple interest (si). A float is used here because the result of the division could be a
decimal value.
scanf("%d", &p);: Reads an integer input from the user and stores it in the variable p
(principal amount).
scanf("%d", &r);: Reads an integer input for the rate of interest and stores it in r.
scanf("%d", &t);: Reads an integer input for the time period and stores it in t.
The format specifier %d is used to indicate that the input is of integer type, and the &
symbol is used to provide the memory address of the variables where the input values
will be stored.
The principal (p), rate of interest (r), and time (t) are multiplied together and then divided
by 100 to obtain the simple interest
The result is stored in the floating-point variable si since division can result in a decimal
value.
int main(){
int p,r,t;
float si;
scanf("%d",&p);
scanf("%d",&r);
scanf("%d",&t);
si=((p*r*t)/100);
return 0;
Output:
Conclusion:
This program demonstrates how to calculate simple interest based on user input using basic
arithmetic operations in C. It introduces the use of scanf() for taking input and printf() for
displaying the output, along with fundamental concepts like variable declaration, data types
(integer and float), and simple mathematical operations. By mastering this type of program, you
can expand to more complex calculations and financial applications in C.
int choice;: This variable is used to store the user's selection for the type of conversion
(Fahrenheit to Celsius or Celsius to Fahrenheit).
int f;: Used to store the Fahrenheit temperature entered by the user.
float c;: Used to store the calculated Celsius temperature, as it may contain decimal
values.
int C;: Used to store the Celsius temperature entered by the user.
Press 1 for Fahrenheit to Celsius and press 2 for Celsius to Fahrenheit: : The user is
prompted to choose the type of conversion.
scanf("%d", &choice);: Reads the user’s choice as an integer and stores it in the choice
variable.
A switch statement is used to evaluate the value of choice and execute the appropriate
block of code based on the user’s selection.
If the user chooses 1:The program asks the user to input the temperature in Fahrenheit.
Scanf("%d", &f);: Reads the Fahrenheit temperature and stores it in the variable f.
Case 2:
If the user chooses 2:The program asks the user to input the temperature in Celsius.
scanf("%d", &C);: Reads the Celsius temperature and stores it in the variable C.
Default Case:
If the user enters an invalid choice (not 1 or 2), the default case will handle it. In this
program, the default block does not print any error message or perform any action.
int main()
int choice;
printf("Press 1 for Fahrenheit to Celsius and press 2 for Celsius to Fahrenheit: ");
scanf("%d",&choice);
switch(choice){
case 1:
int f;
float c;
scanf("%d",&f);
c=((f - 32)*(5/9));
break;
case 2:
int C;
float F;
scanf("%d",&C);
F= ((C*(9/5))+32);
break;
default:
break;
return 0;
Output:
2. Celsius to Fahrenheit:
Conclusion:
This program demonstrates the use of conditional logic with a switch statement to perform two
different tasks (temperature conversions) based on user input. It introduces fundamental
concepts such as handling user input, arithmetic operations, and correcting potential errors
related to integer division. This type of problem-solving structure is common in programs that
offer multiple options based on user input.
Program 5:
Aim: Calculating the sum of natural numbers up to a given limit using loops.
Theory:
1. Variable Declaration
int n;: This stores the user input, representing the upper limit for the sum (i.e.,
the total number of natural numbers to be added).
int sum = 0;: This variable holds the accumulated sum of the numbers. It is
initialized to 0 because the sum starts from zero.
printf("Enter the number n: ");: The program prompts the user to enter a value for n.
3. For Loop
for(i = 1; i <= n; i++): This loop runs from i = 1 to i = n, incrementing i by 1 each time (using
i++ The loop controls how many times the code inside the loop body will be executed.
sum = sum + i;: This is the core operation of the program, where the current
value of i is added to the sum, and the result is stored back into sum.
Iteration 1: sum = 0 + 1 = 1
Iteration 2: sum = 1 + 2 = 3
Iteration 3: sum = 3 + 3 = 6
Iteration 4: sum = 6 + 4 = 10
Iteration 5: sum = 10 + 5 = 15
After the loop finishes, the accumulated value of sum will be printed using the printf statement.
Program Code:
#include<stdio.h>
int main()
int i,n,sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
sum=sum+i;
printf("Sum=%d",sum);
return 0;
Output:
Program 6:
Aim: Program to determine if a number is even or odd using if-else statements.
Theory:
1. variable declaration:
the program declares one variable:int n;: this variable is used to store the number input
by the user.
Program Code:
#include<stdio.h>
int main()
int n;
scanf("%d",&n);
if(n%2==0)
printf("N is even");
else
printf("N is odd");
return 0;
Output:
Conclusions:
This program is a simple demonstration of how to check whether a number is even or odd using
basic arithmetic and conditional statements. it illustrates the use of the modulus operator,
conditional logic, and input/output operations in c.
Program 7:
Aim: Finding the factorial of a number using for loop.
for(i = 1; i <= n; i++): the loop iterates through the numbers from 1 to n.
rlt = rlt * i;: in each iteration, the current value of i is multiplied with the current
value of rlt, which stores the result of the factorial calculation.
▪ iteration 1: rlt = 1 * 1 = 1
▪ iteration 2: rlt = 1 * 2 = 2
▪ iteration 3: rlt = 2 * 3 = 6
▪ iteration 4: rlt = 6 * 4 = 24
Program Code:
#include<stdio.h>
int main()
scanf("%d",&n);
if(n==0)
else
for(i=1;i<=n;i++){
rlt=rlt*i;
return 0;
Output:
Conclusion:
This program provides a simple and efficient way to calculate the factorial of a given number
using loops and conditional statements. it demonstrates basic programming concepts such as
Program 8:
Aim: Program to define, initialize, and display a 1-D array.
Theory:
1. Array Declaration and Initialization:
The 1-D array array[5] is defined to hold 5 integers. The array is initialized with the values
{10, 20, 30, 40, 50}.
A for loop is used to iterate over the array and print each element along with its index.
The loop runs from i = 0 to i = 4 (5 iterations, as array indexing starts at 0).
Program Code:
#include <stdio.h>
int main() {
return 0;
Output:
Program 9:
Aim: To find smallest, largest, average, max and min from list of n numbers
Theory:
1. Array Declaration:
The array arr[] is declared and initialized with 10 integer values: {2, 4, 5, 1, 7, 10, 3, 6, 8,
9}. The number of elements in the array (n) is calculated by dividing the total size of the
array (sizeof(arr)) by the size of one element (sizeof(arr[0])).
2. Initialization of Variables:
o smallest and largest are set to the first element of the array (arr[0]).
The program uses a for loop to iterate through the array, calculating the sum of the
elements, and comparing each element to find the smallest and largest values.
The program begins by initializing the smallest and largest values to the first element of the
array. As it iterates through each element of the array:
• It checks if the current element is smaller than smallest or larger than largest, and
updates those values accordingly.
Program Code:
#include <stdio.h> int
main() {
float average;
sum += arr[i];
// Find the smallest and largest numbers
smallest = arr[i];
largest = arr[i];
average = (float)sum / n;
return 0;
}
Output:
Conclusion:
This program effectively calculates the smallest, largest, and average values of a given array
using basic iteration and conditional checks. It showcases fundamental array operations, such
as accessing elements, calculating sums, and finding minimum and maximum values.
Program 10:
Aim: Swapping two numbers using and without using third variable
Theory:
1. Variable Declaration:
int a, b, c;:a and b are the two numbers whose values will be swapped. c is a temporary
variable used to hold the value of one of the variables during the swapping process.
printf("Enter the number a: ");: The program prompts the user to enter the first number,
which will be stored in a.
scanf("%d", &a);: This function reads the integer input and stores it in a. The format
specifier %d is used because a is an integer.
scanf("%d", &b);: This function reads the integer input and stores it in b.
Program Code:
1. Using third variable:
#include<stdio.h>
int main()
int a,b,c;
scanf("%d",&a);
scanf("%d",&b);
c=a;
a=b;
b=c;
return 0;
Output:
1. With using third variable:
int rows, cols, i, j;: These variables are declared to store the number of rows and columns of the
matrices, as well as for loop counters (i and j).
a[rows][cols], b[rows][cols], and sum[rows][cols]: These are the 2D arrays used to store the two
input matrices and the resultant matrix after addition. The size of the matrices is determined by
the user input for rows and cols.
4. Matrix Addition:
The program performs the element-wise addition of the two matrices using a nested
loop. Each element in a[i][j] is added to the corresponding element in b[i][j], and the
result is stored in sum[i][j].
Program code:
#include<stdio.h>
int main() {
scanf("%d", &rows);
scanf("%d", &cols);
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
printf("%d\t", sum[i][j]);
printf("\n");
Output:
Conclusion:
This program provides a clear example of how to perform matrix addition in C. It takes two
matrices as input, adds them element-by-element, and prints the resulting matrix. Matrix
addition is a fundamental operation in linear algebra and computer science, and this program
serves as a foundation for more advanced matrix operations like multiplication and
transposition.
Program 2:
Aim: Program to perform multiplication of two 2-D arrays
Program Code:
#include<stdio.h>
int main() {
scanf("%d", &rows1);
scanf("%d", &cols1);
scanf("%d", &rows2);
scanf("%d", &cols2);
// Check if matrix multiplication is possible (number of columns in first matrix must equal
number of rows in second matrix)
if (cols1 != rows2) {
return 0;
resultMatrix[i][j] = 0;
scanf("%d", &matrix1[i][j]);
scanf("%d", &matrix2[i][j]);
printf("%d\t", resultMatrix[i][j]);
printf("\n");
return 0;
Output:
Program 3:
Aim: Program to create a multi-level menu using switch cases that perform various basic
mathematical operations (Add, Subtract, Divide, Multiply, Exponent).
Theory:
1. Variable Declaration:
2. User Input:
The program uses the scanf() function to take input from the user:
scanf("%d", &num1); takes the first integer.
scanf("%d", &num2); takes the second integer.
scanf("%c", &operator); takes the operator, but note that there are multiple scanf() calls
for the operator due to the newline character issue explained below.
3. Switch Statement:
The switch statement evaluates the value of the operator variable and executes the
corresponding case:
case '+': Adds the two numbers and stores the result in sum.
case '-': Subtracts the second number from the first and stores the result in sub.
case '*': Multiplies the two numbers and stores the result in mul.
case '/': Divides the first number by the second, storing the result in div. Before
performing the division, the program checks if the second number (num2) is zero to
prevent a division by zero error.
Each case includes a break; statement to exit the switch block once the operation is
performed.
Division by Zero Check
if (num2 == 0): Before performing the division, the program checks if the second number
is zero. If it is, it prints "Not divisible," preventing a runtime error.
If num2 is not zero, the program performs the division and prints the result.
Program Code:
#include<stdio.h>
int main()
int num1,num2,sum,sub,mul;
float div;
char operator;
scanf("%d",&num1);
scanf("%d",&num2);
scanf("%c",&operator);
scanf("%c",&operator);
scanf("%c",&operator);
switch (operator){
break;
break;
break;
if(num2==0)
printf("Not divisible");
else
break;
case ‘%’:
printf(“expo: %d”,&expo);
return 0;
Output:
Program 4:
Aim: Program to implement linear search in a 1-D array
Theory:
1.Array Declaration and Initialization:
The loop starts at index 0 and continues until it reaches the last index, 9 (since array indexing
starts from 0).
Inside the loop, the program checks whether the current element in the array (marks[i]) is
equal to 35.
If the condition is true, the program prints the index i where the element 35 is located.
The program searches through the array marks, which contains 10 elements. It compares
each element with 35 and prints the index where 35 is found. In this example, 35 is present at
index 4, so the output will be: 4
Program Code:
#include
int main()
int marks[10] = {78, 56, 65, 24, 35, 54, 21, 29, 89, 95};
if (marks[i] == 35)
return 0;
Output: