C Lab
C Lab
Ex. PAGE
DATE NAME OF THE EXPERIMENTS MARKS SIGN
No. No.
Flowchart using Raptor tool
Simple interest calculation
1. Greatest among three numbers
Find the sum of digits of
a number
Operators
Arithmetic operator
2. Logical operator
Relational operator
Ternary operator
Iterative statements
For loop
While loop
Do-while loop
3. Conditional Statements
if
if-else
Nested if-else
if-else-if ladder
Arrays
One-dimensional numeric
4. array
Two-dimensional numeric
array
EX. NO:1
DATE:
AIM:
To draw a flowchart for adding two numbers using RAPTOR tool
SYMBOLS DEFINITION
Yes
No
SIMPLE INTEREST CALCULATION
P= 2
R= 3
N=4
OUTPUT:
I=24
GREATEST AMONG THREE NUMBERS
A= 7
B= 9
C=4
OUTPUT:
B=9
FIND THE SUM OF DIGITS OF A NUMBER
Number=453
OUTPUT:
Sum=12
MARKS ALLOCATION
Preparation& conduct of (50)
experiments
Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)
RESULT:
Thus the flow chart was drawn successfully using Raptor Tool.
OPERATORS
EX NO:2
DATE:
ARITHMETIC OPERATORS
AIM:
ALGORITHM:
SOURCE CODE:
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
SAMPLE OUTPUT:
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
PROBLEMSTATEMENT:
Write a program that converts a temperature from Fahrenheit to Celsius. The formula to convert
Fahrenheit to Celsius is:
FORMULA:
The user should input the temperature in Fahrenheit, and the program should then calculate and display
the temperature in Celsius.
PROGRAM:
#include <stdio.h>
int main()
{
float Fahrenheit, Celsius;
printf(“Enter temperature in Fahrenheit: “);
scanf(“%f”, &Fahrenheit);
Celsius = (5.0 / 9.0) * (Fahrenheit – 32);
printf(“Temperature in Celsius is %.2f\n”, Celsius);
return 0;
}
INPUT:
OUTPUT:
AIM:
To write c program to perform logical operators
ALGORITHM:
SOURCE CODE:
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c >
b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}
SAMPLE OUTPUT:
(a == b) && (c > b) is
1 (a == b) || (c < b) is 1
!(a == b) is 0
PROBLEM STATEMENT:
Write a program that determines if a student has passed or failed an exam based on their scores in two
subjects. A student passes if they score at least 40 in both subjects. The user should input the scores for
the two subjects, and the program should then determine if the student has passed or failed using only
logical and relational operators.
PROGRAM:
#include <stdio.h>
int main()
{
int score1, score2;
int passed;
printf(“Enter the score for subject 1:
“); scanf(“%d”, &score1);
printf(“Enter the score for subject 2:
“); scanf(“%d”, &score2);
Passed = (score1 >= 40) && (score2 >= 40);
printf(“You have %s the exam.\n”, Passed ? “Passed” : “Failed”);
return 0;
}
INPUT:
OUTPUT:
AIM:
To write c program to perform relational operators
ALGORITHM:
SOURCE CODE:
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;
printf("%d == %d is %d \n", a, b, a == b);
printf("%d == %d is %d \n", a, c, a == c);
printf("%d > %d is %d \n", a, b, a > b);
printf("%d > %d is %d \n", a, c, a > c);
printf("%d < %d is %d \n", a, b, a < b);
printf("%d < %d is %d \n", a, c, a < c);
printf("%d != %d is %d \n", a, b, a != b);
printf("%d != %d is %d \n", a, c, a != c);
printf("%d >= %d is %d \n", a, b, a >= b);
printf("%d >= %d is %d \n", a, c, a >= c);
return 0;
}
SAMPLE OUTPUT:
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
PROBLEMSTATEMENT:
Write a program that takes two numbers as input and checks which one is greater, using only relational
and arithmetic operators (no conditional statements or loops). The program should then display which
number is greater or if both numbers are equal.
PROGRAM:
#include <stdio.h>
int main()
{
int num1, num2;
intisGreater, isEqual;
printf(“Enter the first number: “);
scanf(“%d”, &num1);
printf(“Enter the second number: “);
scanf(“%d”, &num2);
isGreater = (num1 > num2);
isEqual = (num1 == num2);
printf(“The first number is Greater.\n” * isGreater + “The second number is Greater.\n” *
(!isGreater&& !isEqual) + “Both numbers are equal.\n” * isEqual);
return 0;
}
INPUT:
OUTPUT:
ALGORITHM:
SOURCE CODE:
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
(age >= 18) ? printf("You can vote") : printf("You cannot
vote"); return 0;
}
SAMPLE OUTPUT:
Write a program that determines the greatest of two numbers using the ternary operator. The user
should input two integers, and the program should use the ternary operator to find and display the
greatest number.
PROGRAM:
#include <stdio.h>
int main()
{
int num1, num2, Greatest;
printf(“Enter the first number: “);
scanf(“%d”, &num1);
printf(“Enter the second number: “);
scanf(“%d”, &num2);
Greatest = (num1 > num2) ? num1 : num2;
printf(“The Greatest number is %d\n”, Greatest);
return 0;
}
INPUT:
OUTPUT:
.
VIVA QUESTIONS:
ADDITIONAL PROGRAM:
MARKS ALLOCATION
Preparation& conduct of (50)
experiments
Observation & result (30)
Record (10)
Viva–voce (10)
Total (100)
RESULT:
DATE:
FOR LOOP
AIM:
ALGORITHM:
Step 1: Start.
Step 2: initialize i=2
Step 3: check condition
Step 4: i=i+2
Step 5: print i
Step 6: Stop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
inti;
clrscr();
for(i=2;i<=10;i+=2)
{
printf("%d\t",i);
}
getch();
}
SAMPLE OUTPUT:
2 4 6 8 10
PROBLEM STATEMENT
Write a program that prompts the user to input a positive integer. It should then print the multiplication
table of that number.
PROGRAM:
#include <stdio.h>
int main() {
intnum ;
printf ("Enter a positive integer: ");
scanf("%d", &num);
if (num<= 0) {
printf("Invalid input. Please enter a positive integer.\n");
return 1; }
printf("Multiplication table of %d:\n", num);
for (inti = 1; i<= 5; i++) {
printf("%d * %d = %d\n", num, i, num * i);
} return 0;
}
OUTPUT
Enter a positive integer: 15
Multiplication table of 15:
15 * 1 = 15
15 * 2 = 30
15 * 3 = 45
15 * 4 = 60
15 * 5 = 75
WHILE LOOP
AIM:
To write a c program to print even numbers using while loop.
ALGORITHM:
Step 1: Start.
Step 2: initialize i=2
Step 3: check condition
Step 4: i=i+2
Step 5: print i
Step 6: Stop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
Inti=2;
clrscr();
while(i<=10)
{
printf("%d\t",i);
i=i+2;
}
getch();
}
SAMPLE OUTPUT:
2 4 6 8 10
PROBLEM STATEMENT
Write a program to compute sinx for given x. The user should supply x and a positive integer n. We
compute the sine of x using the series and the computation should use all terms in the series up through
the term involving xn
sin x = x - x3/3! + x5/5! - x7/7! + x9/9! ......
PROGRAM
#include <stdio.h>
#include <math.h>
int main() {
double x, result = 0;
int n, i = 1;
scanf("%lf", &x);
1;
while (i<= n) {
result += sign *
OUTPUT
sin(24.00) = 64075.2000000000
DO WHILE LOOP
AIM:
To write a c program to print even numbers using do while loop.
ALGORITHM:
Step 1: Start.
Step 2: initialize i=2
Step 3: i=i+2
Step 4: print i
Step 5: check condition and do repeat
Step 6: Stop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
inti=2;
clrscr();
do
{
printf("%d\t",i);
i=i+2;
} while(i<10);
getch();
}
SAMPLE OUTPUT:
2 4 6 8 10
PROBLEM STATEMENT
Write a program to enter the numbers till the user wants and at the end the program should display the
largest and smallest numbers entered
PROGRAM
#include <stdio.h>
int main() {
do {
} else {
largest = number;}}
OUTPUT
Enter a number: 41
Smallest number: 41
Largest number: 41
MARKS ALLOCATION
Preparation& conduct of (50)
experiments
Viva–voce (10)
Total (100)
RESULT:
IF CONDITION
AIM:
To write a c program to print even numbers using if condition.
ALGORITHM:
Step 1: Start.
Step 2: initialize a and b
Step 3: a>b
Step 4: print a
Step 5: check condition and do repeat
Step 6: Stop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
inta=10,b=12;
clrscr();
if(a>b)
{
printf(“%d=a”);
}
getch();
}
SAMPLE OUTPUT:
10
PROBLEM STATEMENT
Write a basic C program using conditional statement to check number is valid or not. If number is
divisible by 2 then valid print on screen otherwise nothing will print.
PROGRAM
#include <stdio.h>
int main()
int n;
scanf(“%d”,&n);
if (n/2==0)
printf(“valid number”);
return 0;
Output
valid number
RESULT
Thus,if number is divisible by 2 then valid print on screen otherwise nothing will print.
PROBLEM STATEMENT
Imagine you're writing a program to determine the grade of a student based on their score. Write a C
program that takes the score as input and displays the corresponding grade according to the following
criteria:
Implement the program using only if statements, without using else statements.
PROGRAM
#include <stdio.h>
int main() {
int score;
printf("Enter the student's score: ");
scanf("%d", &score);
if (score >= 90 && score <= 100)
{ printf("Grade: A\n");
}
if (score >= 80 && score <= 89)
{ printf("Grade: B\n");
}
if (score >= 70 && score <= 79)
{ printf("Grade: C\n");
}
if (score >= 60 && score <= 69)
{ printf("Grade: D\n");
}
if (score < 60) {
printf("Grade: F\n");
}
return 0;
}
OUTPUT
AIM:
To write a c program to print even numbers using if-else condition.
ALGORITHM:
Step 1: Start.
Step 2: initialize a and b
Step 3: a>b
Step 4: print a
Step 5: check condition and do repeat
Step 6: Stop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
inta=10,b=12;
clrscr();
if(a>b)
{
printf(“%d=a”);
}
else
{
printf(“%d=b”);
getch();
}
SAMPLE OUTPUT:
10
PROBLEM STATEMENT
Write a basic C program using if else statement to check if a number is even or odd.
PROGRAM
#include <stdio.h>
int main()
int n;
scanf(“%d”,&n);
if (n%2==0)
else
return 0;
Output
Imagine you're writing a program to determine the discount rate for customers based on their total
purchase amount. Write a C program that takes the total purchase amount as input and calculates the
discount rate according to the following criteria:
Implement the program using only if-else statements without nesting or using a ladder structure.
PROGRAM
#include <stdio.h>
int main() {
float totalAmount, discountRate = 0;
printf("Enter the total purchase amount: $");
scanf("%f", &totalAmount);
if (totalAmount > 1000) {
discountRate = 0.15;
}
if (totalAmount >= 501 && totalAmount <= 1000)
{ discountRate = 0.10;
}
if (totalAmount >= 100 && totalAmount <= 500)
{ discountRate = 0.05;
}
if (discountRate == 0) {
printf("No discount is given.\n");
} else {
printf("A %.0f%% discount is given.\n", discountRate * 100);
}
return 0;
}
OUTPUT
AIM:
To write a c program to print even numbers using nested if-else condition.
ALGORITHM:
Step 1: Start.
Step 2: initialize a and b,c
Step 3: a>b>c
Step 4: print a
Step 5: check condition and do repeat
Step 6: Stop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main(){
inta,b,c;clrscr();
printf("\nEnter 3 number");
scanf("%d%d%d",&a,&b,&c);
if(a>b){
if(a>c)
printf(“%d is great",a);else
printf(“%d is grear”,c);}
else{if(b>c)
printf(“%d is great”,b);else
printf(“%d is great”,c);}
getch();}
SAMPLE OUTPUT:
Enter 3 number: 6 2 1
6 is great
PROBLEM STATEMENT
You are a developer tasked with creating a program for a small weather station that records
temperatures from three different sensors placed at different locations in a city. The program needs to
determine which sensor recorded the highest temperature on a given day.
Program
#include <stdio.h>
int main() {
scanf("%f", &temp1);
scanf("%f", &temp2);
scanf("%f", &temp3);
} else {
} else {
} else {
return 0;
}
Input
Output
AIM:
To write a c program to print even numbers using if-else-if ladder condition.
ALGORITHM:
Step 1: Start.
Step 2: initialize a and b,c
Step 3: a>b>c
Step 4: print a
Step 5: check condition and do repeat
Step 6: Stop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main(){
intmark;clrscr();
scanf("%d",&mark);
if(mark>90)printf("S Grade");
elseprintf("Fail Mark");
getch();
SAMPLE OUTPUT:
A Grade
PROBLEM STATEMENT
To determine the bill amount based on the units consumed.Scenario:The first 100 units are charged at
$1.50 per unit.The next 100 units are charged at $2.00 per unit.The next 300 units are charged at
$2.50 per unit.Any units above 500 are charged at $3.00 per unit.
PROGRAM
#include <stdio.h>
int main() {
scanf("%f", &units);
bill = units *
1.50;
} else {
bill = (100 * 1.50) + (100 * 2.00) + (300 * 2.50) + ((units - 500) * 3.00);
OUTPUT
ADDITIONAL PROGRAM:
MARKS ALLOCATION
Preparation & conduct of (50)
experiments
Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)
RESULT:
DATE:
ONE DIMENSIONAL ARRAY
AIM:
To write a c program to print find average on ‘n’ marks
ALGORITHM:
Step 1: Start.
Step 2: Declare one dimensional array
Step 3: Get input values
Step 4: calculate sum and average
Step 5: Display it
Step 6: Stop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
intmark[10],n,i,sum=0;
float avg;
clrscr();
printf("\nEnter number of marks:");
scanf("%d",&n);
printf("Enter %d marks one by one:",n);
for(i=1;i<=n;i++)
{
scanf("%d",&mark[i]);
sum=sum+mark[i];
}
printf("\nSum=%d",sum);
avg=sum/n; printf("\nAverage=
%f",avg); getch();
}
SAMPLE OUTPUT:
You are developing an inventory management system for a small store. The system needs a program to
help sort a list of product prices to determine the cheapest and most expensive items easily. Product
prices are integer values. You need to implement a C program that will take the prices as input, sort
them in ascending order, and display the sorted list.
PROGRAM:
include <stdio.h>
int main() {
int prices[100];
int numProducts;
OUTPUT:
Enter the number of products: 5
Enter the prices of the products:
Price of product 1: 25
Price of product 2: 10
Price of product 3: 50
Price of product 4: 30
Price of product 5: 15
Sorted list of product prices:
10 15 25 30 50
TWO DIMENSIONAL ARRAY
AIM:
To write a c program to add two matrix.
ALGORITHM:
Step 1: Start.
Step 2: Enter rows and columns using two dimensional array
Step 3: Get A and B matrix integers
Step 4: Add A and B matrix and store in C matrix
Step 5: Display it
Step 6: Stop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
inta[10][10],b[10][10],c[10][10],row,col,i,j;
clrscr();
printf("Enter the no.of rows:");
scanf("%d",&row);
printf("Etner the no.of columns:");
scanf("%d",&col);
printf("Enter the matrix A element:");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the matrix B element:");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Addition matrix is:\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
SAMPLE OUTPUT:
You are tasked with developing a C program to assist a small data analysis firm in processing two-
dimensional arrays (matrices) of numerical data. The firm needs a tool that can add corresponding
elements from two given 2D arrays and produce a resultant .The matrices to be added will always have
the same dimensions.
PROGRAM
include <stdio.h>
#define ROWS 3
#define COLS 3
int main() {
int matrix1[ROWS]
[COLS]; int
matrix2[ROWS][COLS];
int result[ROWS][COLS];
printf("Enter elements of the first matrix (%d x %d):\n", ROWS, COLS);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("Enter element [%d][%d]: ", i,
j);
scanf("%d", &matrix1[i][j]);
}
}
printf("\nEnter elements of the second matrix (%d x %d):\n", ROWS, COLS);
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("Enter element [%d][%d]: ", i,
j);
scanf("%d", &matrix2[i][j]);
}
}
printf("\nResultant matrix after addition:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
VIVA QUESTIONS:
1. Define Arrays.
2. List some operations performed by array.
ADDITIONAL PROGRAM:
MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments
Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)
RESULT:
DATE:
AIM:
To write a c program to execute module programming using function.
ALGORITHM:
SOURCE CODE:
Output
Enter 2 numbers: 2 3
Sum = 5
2. Function with argument and no return value
#include<stdio.h>
#include<conio.h>
void sum(int,int);
void main()
{
inta,b;
clrscr();
printf("\nEnter two values:");
scanf("%d%d",&a,&b);
sum(a,b);
getch();
}
void sum(inta,int b)
{
int c;
c=a+b;
printf("Sum=%d",c);
}
Output
Enter 2 numbers: 4 3
Sum = 7
Enter 2 numbers: 4 7
Sum = 11
4 Function with argument and with return value.
#include<stdio.h>
#include<conio.h>
int sum(int,int);
void main()
{
inta,b,c;
clrscr();
printf("\nEnter two values:");
scanf("%d%d",&a,&b);
c=sum(a,b); printf("\nSum=
%d",c); getch();
}
int sum(inta,int b)
{
int c;
c=a+b;
return(c);
}
Output
Enter 2 numbers: 4 7
Sum = 11
#include<stdio.h>
int fact(int);
void main()
{
int n;
clrscr();
printf("Enter number: ");
scanf("%d",&n);
printf("Factorial of %d = %d",n,fact(n));
getch();
}
int fact(int n)
{
if(n!=1)
n=n*fact(n-1);
return n;
}
Output
Enter number: 5
Factorial of 5 is 120
PROBLEM STATEMENT:
1. You are working as a software developer for a company that designs digital displays for household
appliances. Your current project involves programming a thermostat that displays temperature
readings in both Celsius and Fahrenheit. You need to write a function in C that reads a temperature in
Celsius from the user, converts it to Fahrenheit, and displays both values. The function should not take
any arguments and should not return any value. It will simply perform the input, conversion, and
output
PROGRAM:
#include <stdio.h>
void displayTemperature()
{
float celsius, fahrenheit;
printf("Enter temperature in Celsius:
"); scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("Temperature in Celsius: %.2f\n", celsius);
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
}
int main() {
displayTemperature();
return 0;
}
OUTPUT:
2. Imagine you're developing a program for a library management system. You need to create a function
that calculates the late fee for returning a book after the due date. Write a C program that includes a
function calculateLateFee which takes two parameters: the due date of the book and the actual
return date. The function should calculate and return the late fee based on the following criteria:
If the book is returned on or before the due date, the late fee is $0.
If the book is returned after the due date, the late fee is calculated as $0.25 for each day
overdue.
Your program should prompt the user to enter the due date and the actual return date of the book, call
the calculateLateFee function to determine the late fee, and then display the late fee to the user.
PROGRAM
#include <stdio.h>
float calculateLateFee(int dueDate, int returnDate) {
if (returnDate <= dueDate) {
return 0.0;
}
else {
return 0.25 * (returnDate - dueDate);
}
}
int main() {
int dueDate, returnDate;
float lateFee;
printf("Enter the due date (in days from today):
"); scanf("%d", &dueDate);
printf("Enter the actual return date (in days from today):
"); scanf("%d", &returnDate);
lateFee = calculateLateFee(dueDate, returnDate);
printf("The late fee for returning the book is: $%.2f\n",
lateFee); return 0;
}
OUTPUT:
ADDITIONAL PROGRAM:
MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments
Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)
RESULT:
DATE:
AIM:
To write a c program to concatenation of two strings.
ALGORITHM:
Step 1: Start.
Step 2: Get two strings using char datatype
Step 3: use while loop to concat two
strings Step 4: Display it
Step 5: Stop.
SOURCE CODE:
#include<stdio.h>
void main(void)
{
char str1[25],str2[25];
inti=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}
SAMPLE OUTPUT:
AIM:
To write a c program to concatenation of two strings.
ALGORITHM:
Step 1: Start.
Step 2: Get two strings using char
datatype Step 3: use strcat to concat two
strings Step 4: Display it
Step 5: Stop.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("\nEnter the string1:");
gets(s1);
printf("\nEnter the string2:");
gets(s2);
strcat(s1,s2);
printf("\nConcatenated String:%s",s1);
getch();
}
SAMPLE OUTPUT:
You are working on a text processing application in C that requires frequent manipulation of strings.
One of the core features needed is concatenating two strings.
Your task is to implement this feature in two ways:
PROGRAM
#include <stdio.h>
#define MAX_LENGTH 101
int main() {
char str1[MAX_LENGTH], str2[MAX_LENGTH];
int I, j;
printf(“Enter the first string: “);
fgets(str1, MAX_LENGTH, stdin);
printf(“Enter the second string: “);
fgets(str2, MAX_LENGTH, stdin);
str1[strcspn(str1, “\n”)] = 0;
str2[strcspn(str2, “\n”)] = 0;
for (I = 0; str1[i] != „\0‟; i++);
for (j = 0; str2[j] != „\0‟; j++, i++)
{ str1[i] = str2[j];
}
str1[i] = „\0‟;
printf(“Concatenated string: %s\n”, str1);
return 0;
}
OUTPUT
Concatenated string: HelloWorld
VIVA QUESTIONS:
ADDITIONAL PROGRAM:
MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments
Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)
RESULT:
EX.NO.7
DATE:
AIM:
ALGORITHM:
SOURCE CODE:
#include <stdio.h>
int main()
{
int first, second, *p, *q, sum;
printf("Enter two integers to add\n");
scanf("%d%d", &a, &b);
p = &a;
q = &b;
sum = *p + *q;
printf("Sum of entered numbers = %d\n",sum);
return 0;
}
SAMPLE OUTPUT:
Enter two integers to add
4
5
Sum of entered numbers=9
PROBLEM STATEMENT:
Write a program that adds two numbers using pointers. The user should input two integers, and the
program should use pointers to store these integers and compute their sum. The program should then
display the sum.
PROGRAM:
#include <stdio.h>
int main()
{
int num1, num2, sum;
int *ptr1, *ptr2, *ptrSum;
printf(“Enter the first number: “);
scanf(“%d”, &num1);
printf(“Enter the second number: “);
scanf(“%d”, &num2);
ptr1 = &num1;
ptr2 = &num2;
ptrSum = ∑
*ptrSum = *ptr1 + *ptr2;
printf(“The sum of %d and %d is %d\n”, *ptr1, *ptr2,
*ptrSum); return 0;
}
INPUT:
OUTPUT:
1. What is Pointer?
2. How to use Pointers?
3. What is NULL Pointer in C?
ADDITIONAL PROGRAM:
MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments
Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)
RESULT:
AIM:
To print student details using structure.
ALGORITHM:
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
intrno;
int m1,m2,m3, m4;
int tot;
float avg;
}s;
void main()
{
int n;
clrscr();
printf("\nENTER NAME,RNO AND MARKS \n\n");
scanf("%s%d",.s.name, &s.rno);
scanf("%d",& s.m1);
scanf("%d",&s.m2);
scanf("%d",&s.m3);
scanf("%d",&s.m4);
printf("\n\n\n");
printf("\t\t******************STUDENT DETAILS***************\n");
printf("\t \n"); printf("\
tNAME\tRNO\tM1\tM2\tM3\tM4\tTOTAL\tAVERAGE\n"); printf("\t \
n"); s.tot=s.m1+s.m2+s.m3+s.m4;
s.avg=(s.tot)/4; printf("\t%s\t%d\t%d\t%d\t%d\t%d\t%d\t%.2f\
n",s.name,s.rno,s.m1,s.m2,s.m3,s.m4,s.tot,s.avg); printf("\t \n");
getch();
}
SAMPLE OUTPUT:
ENTER NAME, RNO AND MARKS
Sugir 23
95
85
85
95
***********************STUDENT DETAILS******************
NAME RNO M1 M2 M3 M4 TOTAL AVERAGE
PROBLEM STATEMENT
Imagine you are developing a program to manage student information for a school. Each student record
contains the following information: student ID, name, age, and grade. Define a structure to represent a
student, and write a C program that allows users to add new students and display their information.
You can also extend the program to include additional functionalities such as searching for a student by
their ID, updating student information, and removing a student from the system.
PROGRAM
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main() {
int choice = 0;
while (choice != 3) {
printf("\nStudent Information Management System\n");
printf("1. Add Student\n");
printf("2. Display Students\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice == 1) {
// Add a new student
if (numStudents == MAX_STUDENTS) {
printf("Error: Maximum capacity reached. Cannot add more
students.\n");
continue;
}
students[numStudents++] = newStudent;
printf("Student Records:\n");
printf("ID\tName\tAge\tGrade\n");
for (int i = 0; i < numStudents; i++)
{
printf("%d\t%s\t%d\t%d\n", students[i].studentID,
students[i].name, students[i].age, students[i].grade);
}
} else if (choice == 3) {
// Exit the program printf("Exiting...\
n");
} else {
printf("Invalid choice. Please enter a number between 1 and 3.\n");
}
}
return 0;
}
OUTPUT
1. Define structure.
2. Difference between array and structure.
3. Difference between union and structure.
ADDITIONAL PROGRAM:
Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)
RESULT:
EX.NO.9
DATE:
AIM:
ALGORITHM:
SOURCE CODE:
include<stdio.h>
#include<conio.h>
struct stud
{
int roll;
char name[12];
int percent;
}
s = {10,"SMJC",80};
void main()
{
FILE *fp;
struct stud s1;
clrscr();
fp = fopen("ip.txt","w");
/* write struct s to file */
fwrite(&s, sizeof(s), 1,fp);
fclose(fp);
fp = fopen("ip.txt","r");
/* Readstruct s to file */
fRead(&s1, sizeof(s1), 1,fp);
fclose(fp);
printf("nRoll : %d",s1.roll);
printf("nName : %s",s1.name);
printf("nPercent : %d",s1.percent);
}
SAMPLE OUTPUT
Roll : 10
Name : SMJC
Percent : 80
PROBLEM STATEMENT :
Program to write characters a to z into a file and read the file and print the characters in lowercase.
PROGRAM:
#include<stdio.h>
Voidmain()
{
file *f1;
char c;
clrscr();
printf("Writing characters to file... \n");
f1 = fopen("alpha.txt","w");
for(ch=65;ch<=90;ch++)
fputc(ch,f1);
fclose(f1);
printf("\nRead data from file: \n");
f1 = fopen("alpha.txt","r");
/reads character by character in a
file/ while((c=getc(f1))!=eof)
printf("%c",c+32); /prints characters in lower
case/ fclose(f1);
getch();
}
OUTPUT:
Writing characters to file….
Read data from file: abcdefghijklmnopqrstuvwxyz
VIVA QUESTIONS:
ADDITIONAL PROGRAM:
MARKS ALLOCATION
Preparation & conduct (50)
ofexperiments
Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)
RESULT:
DATE:
AIM:
To write a C program for create memory for int, char and float variable at run time using
dynamic memory allocation.
ALGORITHM:
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *iVar;
char *cVar;
float *fVar;
iVar=(int*)malloc(1*sizeof(int));
cVar=(char*)malloc(1*sizeof(char));
fVar=(float*)malloc(1*sizeof(float));
return 0;
}
SAMPLE OUTPUT
1 FOR THE FIRST 100 UNITS, THE RATE IS $1.50 PER UNITS
2 FOR THE NEXT 200 UNITS, THE RATE IS $2.00 PER UNIT
3 FOR ANY UNITS ABOVE 300, THE RATE IS $2.50 PER UNIT
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
int units;
scanf("%d", &units);
return 1;
if (i< 100) {
rates[i] = 1.50;
} else {
rates[i] = 2.50;
bill += rates[i];
free(rates);
return 0;
OUTPUT:
ADDITIONAL PROGRAM:
MARKS ALLOCATION
Preparation& conduct (50)
ofexperiments
Observation&result (30)
Record (10)
Viva–voce (10)
Total (100)
RESULT:
1. Write a C program to develop simple arithmetic calculator using Switch Case statement.