Trinity Institute of Innovations in Professional Studies
Affiliated to Guru Gobind Singh Indraprastha University, New Delhi
Department of Computer Science
Lab File
C Programming (Lab)
Submitted To: Submitted By:
Ms. Anamika Goel
Assistant Professor
TIIPS ,Greater Noida
INDEX
S. No. Program Date
1
Write a program to print hello world on console.
2 write a program to perform addition, subtraction,
multiplication and division.
3
write a program to calculate the average of two number.
4
write a program to calculate simple interest.
5
write a program to calculate area and perimeter of a
rectangle
6
write a program to implement Relational operator.
7
write a program to find sizeof int ,char ,float and double
8 write a program to check the no is even or odd.
9
write a program to check leap year.
10
write a program to check no is positive, negative or zero.
11
write a program to find largest of three number.
12 WAP to implement the factorial of a number
without using recursion.
13 WAP to implement the factorial of a number
using recursion.
14 WAP to print a days of a week using switch
case.
15
WAP to reverse a number.
16 WAP to implement Fibonacci series with
recursion.
17 C program to print following Pyramid:
*
**
***
****
*****
18 C program to print following Pyramid:
1
123
12345
1234567
123456789
19 Write a Program to find Largest and Smallest
Element in an Array.
20
Write a program to print sum of two matrix.
21
Write a Program to access Array of
int Pointers
22
WAP to sawp two number using pointers.
23 WAP for the implementation of Array Of
Structure
24
WAP for the implementation of Union.
25 WAP for the implementation of string
operation using string functions.
Signature ……………..
PROGRAM 1
OBJECTIVE:
Write a program to print hello world on console.
CODING:
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
OUTPUT:
Hello, World!
Algorithm:
1.Start
2.Print hello world.
3.Stop
Flow Chart:
PROGRAM 2
OBJECTIVE:
write a program to perform addition, subtraction, multiplication and division.
CODING:
#include <stdio.h>
int main()
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting, you can also write: divide = (float)first/second
printf("Sum = %d\n", add);
printf("Difference = %d\n", subtract);
printf("Multiplication = %d\n", multiply);
printf("Division = %.2f\n", divide); // "%.2lf" to print two decimal digits, by default (%lf) we get
six
return 0;
OUTPUT:
ALGORITHM:
do yourself.
FLOW CHART:
Do yourself.
PROGRAM 3
OBJECTIVE:
write a program to calculate the average of two number.
CODING:
#include <stdio.h>
int main(){
float num1,num2,avg;
printf("Enter the first number ");
scanf("%f",&num1);
printf("Enter the second number ");
scanf("%f",&num2);
avg=(num1+num2)/2;
printf("Average of two numbers = %4.2f", avg);
return 0;
OUTPUT:
ALGORITHM:
FLOW CHART:
Do yourself.
PROGRAM 4
OBJECTIVE:
write a program to calculate simple interest.
CODING:
#include<stdio.h>
int main()
float P , R , T , SI ;
P =34000; R =30; T = 5;
SI = (P*R*T)/100;
printf("\n\n Simple Interest is : %f", SI);
return (0);
OUTPUT:
ALGORITHM:
FLOW CHART:
Do yourself.
PROGRAM 5
OBJECTIVE:
write a program to calculate area and perimeter of a rectangle.
CODING:
#include<stdio.h>
int main() {
int length, breadth, area,perimeter;
printf("\nEnter the Length of Rectangle : ");
scanf("%d", &length);
printf("\nEnter the Breadth of Rectangle : ");
scanf("%d", &breadth);
area = length * breadth;
perimeter=2*(length+breadth);
printf("\nArea of Rectangle : %d", area);
printf("\nPerimeter of Rectangle : %d", perimeter);
return (0);
OUTPUT:
ALGORITHM:
FLOW CHART:
Do yourself.
PROGRAM 6
OBJECTIVE:
write a program to implement Relational operator.
CODING:
#include <stdio.h>
int main()
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
else
printf("m and n are not equal");
OUTPUT:
ALGORITHM:
FLOW CHART:
Do yourself.
PROGRAM 7
OBJECTIVE:
write a program to find sizeof int ,char ,float and double
CODING:
#include <stdio.h>
#include <limits.h>
int main()
{int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
return 0;
OUTPUT:
Storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8
ALGORITHM:
FLOW CHART:
Do yourself.
PROGRAM 8
OBJECTIVE:
write a program to check the no is even or odd.
CODING:
#include<stdio.h>
void main()
int n;
printf ("enter a number:");
scanf ("%d", &n);
if (n%2==0)
printf ("even number");
else
printf("odd number");
OUTPUT:
ALGORITHM:
FLOW CHART:
Do yourself.
PROGRAM 9
OBJECTIVE:
write a program to check leap year.
CODING:
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
return 0;
OUTPUT:
ALGORITHM:
FLOW CHART:
Do yourself.
PROGRAM 10
OBJECTIVE:
write a program to check no is positive, negative or zero.
CODING:
#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num < 0.0)
printf("You entered a negative number.");
else if (num > 0.0)
printf("You entered a positive number.");
else
printf("You entered 0.");
return 0;
OUTPUT:
ALGORITHM:
FLOW CHART:
Do yourself.
PROGRAM 11
OBJECTIVE:
write a program to find largest of three number.
CODING:
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2lf is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
else if (n2 >= n1 && n2 >= n3)
printf("%.2lf is the largest number.", n2);
// if both above conditions are false, n3 is the largest
else
printf("%.2lf is the largest number.", n3);
return 0;
OUTPUT:
ALGORITHM:
FLOW CHART:
Do yourself.
PROGRAM 12
OBJECTIVE:
WAP to implement the factorial of a number without using recursion.
CODING:
#include<stdio.h>
#include<conio.h>
void main()
int fact, i, n;
fact = 1;
printf("Enter the number\t");
scanf("%d" , &n);
for(i = 1; i <= n; i++)
fact = fact*i;
printf("Factorial of %d is %d", n , fact);
getch();
OUTPUT:
ALGORITHM:
FLOW CHART:
PROGRAM 13
OBJECTIVE:
WAP to implement the factorial of a number using recursion.
CODING:
#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
OUTPUT:
Enter a positive integer: 6
Factorial of 6 = 720
ALGORITHM:
FLOW CHART:
PROGRAM 14
OBJECTIVE:
WAP to print a days of a week using switch case.
CODING:
#include <stdio.h>
int main()
int week;
printf("Enter week number(1-7): ");
scanf("%d", &week);
switch(week)
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
return 0;
}
OUTPUT:
ALGORITHM:
FLOW CHART:
PROGRAM 15
OBJECTIVE:
WAP to reverse a number.
CODING:
#include <stdio.h>
int main() {
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n =n/10;
printf("Reversed number = %d", rev);
return 0;
OUTPUT:
ALGORITHM:
FLOW CHART:
Structure of array
PROGRAM 16
OBJECTIVE:
WAP to implement Fibonacci series with recursion.
CODING:
#include<stdio.h>
int f(int);
int main()
{
int n, i = 0, c;
scanf("%d", &n);
printf("Fibonacci series terms are:\n");
for (c = 1; c <= n; c++)
{
printf("%d\n", f(i));
i++;
}
return 0;
}
int f(int n)
{
if (n == 0 || n == 1)
return n;
else
return (f(n-1) + f(n-2));
}
OUTPUT:
ALGORITHM:
FLOW CHART:
PROGRAM 17
OBJECTIVE:
C program to print following Pyramid:
**
***
****
*****
CODING:
#include<stdio.h>
#define MAX 5
int main()
int i,j;
for(i=0; i< MAX; i++) i=3,3<5
for(j=0;j<=i;j++) j=,3<=2
printf("*");
printf("\n");
return 0;
OUTPUT:
ALGORITHM:
FLOW CHART:
PROGRAM 18
OBJECTIVE:
C program to print following Pyramid:
123
12345
1234567
123456789
CODING:
#include<stdio.h>
int main()
int i,j,k,l=1;
for(i=1; i<=5; i++)
for(j=4; j>=i; j--)
printf(" ");
for(k=1; k<=l; k++)
printf("%d",k);
l = l+2;
printf("\n");
return 0;
OUTPUT:
ALGORITHM:
FLOW CHART:
PROGRAM 19
OBJECTIVE:
Write a Program to find Largest and Smallest Element in an Array.
CODING:
#include<stdio.h>
int main()
int a[50], size, i, big, small;
printf("\nEnter the size of the array: ");
scanf("%d", &size);
printf("\n\nEnter the %d elements of the array: \n\n", size);
for(i = 0; i < size; i++)
scanf("%d", &a[i]);
big = a[0]; // initializing
for(i = 1; i < size; i++)
if(big < a[i]) // if larger value is encountered
big = a[i]; // update the value of big
printf("\n\nThe largest element is: %d", big);
small = a[0]; // initializing
for(i = 1; i < size; i++)
if(small>a[i]) // if smaller value is encountered
small = a[i]; // update the value of small
}
}
printf("\n\nThe smallest element is: %d", small);
return 0;
OUTPUT:
ALGORITHM:
FLOW CHART:
PROGRAM 20
OBJECTIVE:
Write a program to print sum of two matrix.
CODING:
#include <stdio.h>
int main()
int a[2][2], b[2][2], result[2][2];
printf("Enter elements of 1st matrix\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
printf("Enter elements of 2nd matrix\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
result[i][j] = a[i][j] + b[i][j];
printf("\nSum Of Matrix:");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
printf("%d \t", result[i][j]);
if (j == 1)
printf("\n");
return 0;
}
OUTPUT:
ALGORITHM:
FLOW CHART:
PROGRAM 21
OBJECTIVE:
Write a Program to access Array of int Pointers
CODING:
#include
<stdio.h>
void main()
int a[3] = {1, 2, 3};
int *p = a;
for (int i = 0; i < 3; i++)
printf("%d", *p);
p++;
return 0;
OUTPUT:
ALGORITHM:
FLOW CHART:
PROGRAM 22
OBJECTIVE:
WAP to sawp two number using pointers.
CODING:
#include<stdio.h>
int main()
int a, b;
int *ptra, *ptrb;
int temp;
printf("Enter value for a: ");
scanf("%d", &a);
printf("\n\nEnter value for b: ");
scanf("%d", &b);
printf("\n\nThe values before swapping are: a = %d b = %d", a, b);
ptra = &a; // to store the location of a
ptrb = &b; // to store the location of b
temp = *ptra; // temp stores the value at location ptra
*ptra = *ptrb; // assigning value at location ptrb to ptra
*ptrb = temp; // assign value of themp to the variable at location ptrb
printf("\n\nThe values after swapping are: a = %d b = %d", a, b);
return 0;}
Output:
OUTPUT:
ALGORITHM:
FLOW CHART:
PROGRAM 23
OBJECTIVE:
WAP for the implementation of Structure of array.
CODING:
/* Array of Structures in C example */
#include <stdio.h>
struct Student
char Student_Name[50];
int C_Marks;
int DataBase_Marks;
int CPlus_Marks;
int English_Marks;
};
int main()
int i;
struct Student Students[4] =
{"Suresh", 80, 70, 60, 70},
{"Tutorial", 85, 82, 65, 68},
{"Gateway", 75, 70, 89, 82},
{"Mike", 70, 65, 69, 92}
};
printf(".....Student Details....");
for(i=0; i<4; i++)
printf("\n Student Name = %s", Students[i].Student_Name);
printf("\n First Year Marks = %d", Students[i].C_Marks);
printf("\n Second Year Marks = %d", Students[i].DataBase_Marks);
printf("\n First Year Marks = %d", Students[i].CPlus_Marks);
printf("\n Second Year Marks = %d", Students[i].English_Marks);
return 0;
OUTPUT:
ALGORITHM:
FLOW CHART:
PROGRAM 24
OBJECTIVE:
WAP for the implementation of Union.
CODING:
#include <stdio.h>
#include <string.h>
union abc
{
int a;
char b;
}var;
int main()
{
var.a = 66;
printf("\n a = %d", var.a);
printf("\n b = %c", var.b);
OUTPUT:
a = 66
b=B
ALGORITHM:
FLOW CHART:
PROGRAM 25
OBJECTIVE:
WAP for the implementation of string operation using string functions.
CODING:
#include<stdio.h>
#include<string.h>
int main()
char ch[20]={'j','a','v','a','t','p','o','i','n','t','\0'};
char ch2[20];
strcpy(ch2,ch);
printf("Value of second string is:%s",ch2);
printf("Length of string is:%d",strlen(ch));
strcat(ch,ch2);
printf("Value of first string is: %s",ch);
return 0;
OUTPUT:
Value of second string is: javatpoint
Length of string is: 10
Value of first string is: javatpointjavatpoint
ALGORITHM:
FLOW CHART: