DELHI
TECHNOLOGICAL
UNIVERSITY
PROGRAMMING FUNDAMENTALS
LAB FILE
Name- Priyanshu Verma
Roll no- 2k21/B9/06
Aim 1: Write a program to calculate simple interest.
Source Code:
#include<stdio.h>
int main()
{
float principle, time, rate, SI;
printf("Enter principle value: ");
scanf("%f", &principle);
printf("Enter time duration(in year): ");
scanf("%f", &time);
printf("Enter the rate: ");
scanf("%f", &rate);
/* Calculate simple interest */
SI = (principle * time * rate) / 100;
printf("Simple Interest = %f", SI);
return 0;
}
Output:
Aim 2: Write a C program to find greatest of 10 numbers.
Source Code:
#include<stdio.h>
int main()
{
int num[10],max,i=0,j=1;
printf("\nEnter 10 numbers\n");
for(i=0;i<10;i++,j++)
{
printf("Enter number %d - ",j);
scanf("%d", &num[i]);
}
max=num[0];
i=0,j=1;
for(i=0;i<9;i++)
{
if(max<num[i+1])
{
max = num[i+1];
}
}
printf("\nThe largest number is %d \n",max);
return 0;
Output:
Aim3: Write a program to find whether the entered number is prime.
Source Code:
#include <stdio.h>
int main() {
int num, i, a = 0;
printf("Enter any number:");
scanf("%d", &num);
for (i = 1; i <= num; i++) {
if (num % i == 0) {
a++;
}
}
if (a == 2) {
printf("Entered number is a Prime number");
}
else {
printf("Entered is not a Prime number");
}
return 0;
}
Output :
Aim 4: Write a program to print half pyramid.
*
**
***
****
*****
Source Code:
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output :
AIM 5: write a program to print inverse half pyramid.
Source Code:
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = rows; i >= 1; --i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output:
Aim 6: Write a program to find sum of a 5 digit number.
Source Code:
#include <stdio.h>
int main() {
int n, sum = 0;
printf(“Enter the number - ”);
scanf("%d", &n);
while(n>=1) {
sum += n % 10;
n = n / 10;
printf("Sum of digits is - %d", sum);
return 0;
Output :
Aim 7: Write a program to reverse a 5 digit number.
Source Code:
#include <stdio.h>
int main(){
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
printf("Reversed number = %d", reverse);
return 0;
Output :
Aim 8: Write a program to convert decimal to binary.
Source Code:
#include <stdio.h>
int main()
int num, rem, binary = 0, place = 1;
printf("Enter any number :");
scanf("%d", &num);
while (num > 0)
rem = num % 2;
binary += rem * place;
place *= 10;
num /= 2;
printf("Binary equivalent of %d is %d", num, binary);
return 0;
Output :
Aim 9: Write a program to convert binary to decimal.
Source Code:
#include <stdio.h>
int main()
int binary, rem, deci = 0, base = 1;
printf("Enter the Binary Number = ");
scanf("%d", &binary);
int temp = binary;
while(temp > 0)
rem = temp % 10;
deci = deci + rem * base;
temp = temp / 10;
base = base * 2;
printf("The Binary number(%d) in Decimal value = %d\n", binary, deci);
return 0;
}
Output :
Aim 10: Write a program to implement switch case statement to generate a
menu-driven program
Source Code:
#include <stdio.h>
int main()
{
int statement;
int a,b,c;
printf("Enter the 1st no. - \n");
scanf("%d", &a);
printf("Enter the 2nd no. - \n");
scanf("%d", &b);
printf("1)Addition \n");
printf("2)Subtraction\n");
printf("3)Multiplication \n");
printf("4)Division \n");
printf("Enter the value:");
scanf("%d", &statement);
switch(statement)
{
case 1:
c=a+b;
break;
case 2:
c=a-b;
break;
case 3:
c=a*b;
break;
case 4:
c=a/b;
break;
}
printf("%d", c);
return 0;
}
Output :
Aim 11: Write a program to generate the Fibonacci sequence.
Source Code:
#include<stdio.h>
int main()
{
int a=0,b=1,c,i,number;
printf("Enter the no. of terms you want:\n");
scanf("%d",&number);
printf("\n%d %d", a,b);
for(i=2;i<number;++i)
{
c=a+b;
printf(" %d",c);
a=b;
b=c;
}
printf("\nThe %d term is %d", number,c);
return 0;
}
Output :
Aim 12: write a program to find maximum and minimum element of an array
Source Code:
#include <stdio.h>
int main()
{
int a[100], i, n, max, min;
printf("Enter the size of an array : ");
scanf("%d",&n);
printf("Enter elements in an array : \n ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
max=min=a[0];
for(i=1; i<n; i++)
{
if(min>a[i]){
min=a[i];
}
if(max<a[i]){
max=a[i];
}
}
printf("\nMaximum value in array is : %d", max);
printf("Minimum value in array is : %d", min);
return 0;
}
Output :
Aim 13: Write a program to sort numbers in ascending or descending order.
Source Code:
//Ascending order
#include <stdio.h>
int main ()
int p, n, num[15];
printf ("Please enter how many numbers you want to sort - ");
scanf ("%d", &n);
printf ("\nPlease enter the numbers to be sorted in ascending order -\n");
for (int i=0; i<n; ++i)
scanf ("%d",&num[i]);
for (int i=0; i<n; ++i)
for (int j=i+1; j<n; ++j)
if (num[i] > num[j])
p= num[i];
num[i] = num[j];
num[j] = p;
}
}
printf ("\nAscending order of entered numbers is -\n");
for (int i=0; i<n; ++i)
printf (" %d ",num[i]);
return 0;
Output :
Aim 14: Write a program to calculate the even number in an array.
Source Code:
#include<stdio.h>
int main()
{
int arr[10], i;
printf("Enter any 10 array elements-\n");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
printf("\nAll Even Array elements are-\n");
for(i=0; i<10; i++)
{
if(arr[i]%2==0)
{
printf(" %d ", arr[i]);
}
}
getch();
return 0;
}
Output :
Aim 15: Write a program to find the number of occurrence of every number
in an array.
Source Code:
#include <stdio.h>
int main()
int arr[100], freq[100], i, j, count, Size;
printf(“Enter Number of elements in an array- ");
scanf("%d", &Size);
printf("\nEnter %d elements of an Array- ", Size);
for (i = 0; i < Size; i++)
scanf("%d", &arr[i]);
freq [i] = -1;
for (i = 0; i < Size; i++)
count = 1;
for(j = i + 1; j < Size; j++)
if(arr[i] == arr[j])
count++;
freq [j] = 0;
if(freq [i] != 0)
freq [i] = count;
printf("\n Frequency of all the elements in this Array is-\n");
for (i = 0; i < Size; i++)
if(freq [i] != 0)
printf("%d - %d Times \n", arr[i], freq[i]);
}
return 0;
Output:
Aim 16: Write a program to find multiplication of two matrix.
Source Code:
#include <stdio.h>
int main()
int m, n, p, q, c, d, k, sum = 0;
int mat1[100][100], mat2[100][100], multiply[100][100];
printf("Enter number of rows and columns of first matrix\n");
scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");
for (c = 0; c < m; c++){
for (d = 0; d < n; d++){
scanf("%d", &mat1[c][d]);
printf("Enter number of rows and columns of second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
printf("The multiplication isn't possible.\n");
else
printf("Enter elements of second matrix\n");
for (c = 0; c < p; c++){
for (d = 0; d < q; d++){
scanf("%d", &mat2[c][d]);
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + mat1[c][k]*mat2[k][d];
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of the matrices:\n");
for (c = 0; c < m; c++) {
for (d = 0; d < q; d++){
printf("%d\t", multiply[c][d]);
printf("\n");
return 0;
}
Output:
Aim 17: Write a program in C to insert New value in the array(unsorted list).
Source Code:
#include <stdio.h>
int main()
{
int array[50], position, c, n, value;
printf("Enter number of elements in the array - \n");
scanf("%d", &n);
printf("Enter %d elements -\n", n);
for (c = 0; c < n; c++){
scanf("%d", &array[c]);
}
printf("Enter the location where you want to insert an new element - \n");
scanf("%d", &position);
printf("Enter the value to be inserted - \n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--){
array[c+1] = array[c];
}
array[position-1] = value;
printf("Resultant array is -\n");
for (c = 0; c <= n; c++){
printf("%d\n", array[c]);
}
return 0;
}
Output:
Aim 18: Write a program in C to delete an element at desired position from
an array.
Source Code:
#include <stdio.h>
int main()
{
int array[100], position, i, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( i = 0 ; i < n ; i++ ){
scanf("%d", &array[i]);
}
printf("Enter the location of deleting element\n");
scanf("%d", &position);
if ( position >= n+1 ){
printf("Deletion not possible.\n");
}
else
{
for ( i = position - 1 ; i < n - 1 ; i++ )
array[i] = array[i+1];
printf("Final array is\n");
for( i = 0 ; i < n - 1 ; i++ ){
printf("%d\n", array[i]);
}
}
return 0;
}
Output :
Aim 19: Write a program in C to find the second largest element in an array.
Source Code:
#include <stdio.h>
int main ()
int temp, n=5,arr[50], i, j;
printf ("Enter the size of array - ");
scanf (""%d"", &n);
printf ("\nPlease enter the numbers to be sorted as ascending order");
for (i=0; i<n; ++i){
scanf ("%d", &arr[i]);
}
for (i=0; i<n; ++i)
for (j=i+1; j<n; ++j)
if (arr [i] <arr [j])
temp= arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int s = arr[1];
printf ("the second largest element is %d “, s);
return 0;
}
Output:
Aim 20:Write a program in C to find sum of right diagonals of a matrix.
Source Code:
#include <stdio.h>
void main()
{
int i,j,arr1[50][50],sum=0,n;
printf("Enter the size of the square matrix : ");
scanf("%d", &n);
printf("Enter elements in the first matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element[%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
if (i==j){
sum= sum+arr1[i][j];
}
}
}
printf("The assumed matrix is :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n ;j++){
printf("%d\t",arr1[i][j]);
}
printf("\n");
printf("Addition of the right Diagonal elements is :%d\n",sum);
return 0;
}
Output:
Aim 21: Write a program in C to swap two numbers using function.
Source code:
#include<stdio.h>
void swap(int, int);
int main()
int a, b;
printf("Enter values for a and b\n");
scanf("%d%d", &a, &b);
printf("\n\nBefore swapping: a = %d and b = %d\n", a, b);
swap(a, b);
return 0;
void swap(int x, int y)
int temp;
temp = x;
x = y;
y = temp;
printf("\nAfter swapping: a = %d and b = %d\n", x, y);
}
Output:
Aim 22: Write a program in C to copy the elements of one array into another
array.
Source Code :
#include<stdio.h>
int main() {
int arr1[30], arr2[30], i, num;
printf("\nEnter no of elements you want in a array :");
scanf("%d", &num);
printf("\nEnter the values in array :");
for (i = 0; i < num; i++) {
scanf("%d", &arr1[i]);
for (i = 0; i < num; i++) {
arr2[i] = arr1[i];
printf("The copied array is :");
for (i = 0; i < num; i++)
printf("\narr2[%d] = %d", i, arr2[i]);
return 0;
Output :
Aim 23: Write a program in C to count the total number of duplicate elements
in an array.
Source Code:
#include <conio.h>
int main()
int a[100],b[100],i,j,n,c=0 ;
printf("Enter size of the array you want : ");
scanf("%d", &n);
printf("Enter elements in array :\n");
for(i=0; i<n; i++)
scanf("%d",&a[i]);
for(i=0; i<n; i++)
if(a[i]!=-1)
for(j=i+1; j<n; j++)
if(a[i]==a[j])
{
c++;
a[j]=-1;
printf("duplicate numbers in the array: %d",c);
return 0;
Output :
Aim 24: Write a program in C to merge two arrays of the same size sorted in
descending order.
Source Code :
#include <stdio.h>
int main()
int n1,n2,n3;
printf("\nEnter the size of first array- ");
scanf("%d",&n1);
printf("\nEnter the size of second array- ");
scanf("%d",&n2);
n3=n1+n2;
printf("\nEnter the sorted array elements-\n");
int a[n1],b[n2],c[n3];
for(int i=0;i<n1;i++)
scanf("%d",&a[i]);
c[i]=a[i];
int k=n1;
printf("\nEnter the sorted array elements-\n");
for(int i=0;i<n2;i++)
scanf("%d",&b[i]);
c[k]=b[i];
k++;
printf("\nThe merged array - \n");
for(int i=0;i<n3;i++)
printf("%d ",c[i]);
printf("\nAfter sorting - \n");
for(int i=0;i<n3;i++)
int temp;
for(int j=i+1; j<n3 ;j++)
if(c[i]<c[j])
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
for(int i=0 ; i<n3 ; i++)
printf(" %d ",c[i]);
return 0;
Output :
Aim 25: Write a program in C to separate odd and even integers in separate
arrays.
Source Code:
#include<stdio.h>
void display(int a[], int size);
int main(){
int size, i, a[10], even[20], odd[20];
int Ecount = 0, Ocount = 0;
printf("Enter the size of array :\n");
scanf("%d", &size);
printf("Enter the array elements:\n");
for(i = 0; i < size; i++){
scanf("%d", &a[i]);
for(i = 0; i < size; i ++){
if(a[i] % 2 == 0){
even[Ecount] = a[i];
Ecount++;
else{
odd[Ocount] = a[i];
Ocount++;
}
printf("no. of even elements are = %d \n", Ecount);
printf("The elements that are present in an even array is: ");
display(even, Ecount);
printf("no: of odd elements are = %d \n", Ocount);
printf("The elements that are present in an odd array is: ");
display(odd, Ocount);
return 0;
void display(int a[], int size){
int i;
for(i = 0; i < size; i++){
printf("%d ", a[i]);
printf("\n");
Output :
Aim 26: Write a program to display all prime numbers in an interval.
Source code :
#include <stdio.h>
int main()
int a,b, i, flag;
printf("Enter two intervals numbers: ");
scanf("%d %d", &a, &b);
printf("Prime numbers between %d and %d are: ",a,b);
while (a<b) {
flag = 0;
if (a<= 1) {
++a;
continue;
for (i = 2; i <= a/2; ++i) {
if (a% i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d ", a);
++a;
return 0;
Output :
Aim 27: Write a program to calculate factorial of a number using recursion.
Source Code:
#include<stdio.h>
int find_factorial(int);
int main()
int num, fact;
printf("Enter any integer number: ");
scanf("%d",&num);
fact=find_factorial(num);
printf("\nfactorial of %d is: %d",num, fact);
return 0;
int find_factorial(int n)
if(n==0)
return(1);
return(n*find_factorial(n-1));
}
Output :
Aim 28: Write a program in C for binary search using recursion.
Source Code:
#include <stdio.h>
int binarySearch(int arr[], int a, int b, int item)
int mid;
if (a > b)
return -1;
mid = (a + b) / 2;
if (arr[mid] == item)
return mid;
else if (arr[mid] > item)
binarySearch(arr, a, mid - 1, item);
else if (arr[mid] < item)
binarySearch(arr, mid + 1, b, item);
int main()
int arr[] = { 10, 22, 33, 48, 70 };
int index = 0;
int item = 0;
printf("Enter item to search: ");
scanf("%d", &item);
index = binarySearch(arr, 0, 5, item);
if (index == -1)
printf("Item not found in array\n");
else
printf("Item found at index %d\n", index);
return 0;
Output:
Aim 29: Write a program in C to calculate the power of any number using
recursion
Source Code:
#include<stdio.h>
int exp(int numb,int power)
if(power==0){
return 1;
else{
return numb*exp(numb,power-1);
int main()
int n,p,result;
printf("Enter the number-\n");
scanf("%d", &n);
printf("Enter the power-\n");
scanf("%d", &p);
result = exp(n,p);
printf("The value is- %d", result);
return 0;
Output:
Aim 30: Write a program in C to multiply two matrices using recursion
Source Code:
#include <stdio.h>
void multiply(int, int, int [][10], int, int, int [][10], int [][10]);
void display(int, int, int[][10]);
int main()
int a[10][10], b[10][10], c[10][10] = {0};
int m1, n1, m2, n2, i, j, k;
printf("Enter rows and columns for Matrix A respectively: ");
scanf("%d%d", &m1, &n1);
printf("Enter rows and columns for Matrix B respectively: ");
scanf("%d%d", &m2, &n2);
if (n1 != m2)
printf("Matrix multiplication not possible.\n");
else
printf("Enter elements in Matrix A:\n");
for (i = 0; i < m1; i++)
for (j = 0; j < n1; j++)
scanf("%d", &a[i][j]);
printf("\nEnter elements in Matrix B:\n");
for (i = 0; i < m2; i++)
for (j = 0; j < n2; j++)
scanf("%d", &b[i][j]);
multiply(m1, n1, a, m2, n2, b, c);
printf("On matrix multiplication of A and B the result is:\n");
display(m1, n2, c);
void multiply (int m1, int n1, int a[10][10], int m2, int n2, int b[10][10], int c[10][10])
static int i = 0, j = 0, k = 0;
if (i >= m1)
return;
else if (i < m1)
{
if (j < n2)
if (k < n1)
c[i][j] += a[i][k] * b[k][j];
k++;
multiply(m1, n1, a, m2, n2, b, c);
k = 0;
j++;
multiply(m1, n1, a, m2, n2, b, c);
j = 0;
i++;
multiply(m1, n1, a, m2, n2, b, c);
void display(int m1, int n2, int c[10][10])
int i, j;
for (i = 0; i < m1; i++)
{
for (j = 0; j < n2; j++)
printf("%d ", c[i][j]);
printf("\n");
Output:
Aim 31:Write a program in C to get the largest element of an array using
recursion.
Source Code :
#include <stdio.h>
int largest(int list[], int lower, int upper);
int main()
int array[10] = { 23, 43, 35, 38, 67, 12, 76, 10, 34, 8 };
printf("The largest element in array: %d", largest(array, 0, 9));
return 0;
int largest(int list[], int lower, int upper)
int max;
if (lower == upper)
return list[lower];
else
max = largest(list, lower + 1, upper);
if (list[lower] >= max)
return list[lower];
else
return max;
Output :
Aim 32: Write a program in C to find the GCD of two numbers using
recursion.
Source Code :
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("GCD of %d and %d is %d", n1, n2, hcf(n1, n2));
return 0;
int hcf(int n1, int n2) {
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
Output :
Aim 33: Write a program in C to store n elements in an array and print all
elements using array.
Source Code:
int main()
int arr[MAX_SIZE];
int N, i;
int * ptr = arr;
printf("Enter size of array: ");
scanf("%d", &N);
printf("Enter elements in array:\n");
for (i = 0; i < N; i++)
scanf("%d", ptr);
// Move pointer to next array element
ptr++;
ptr = arr;
printf("Array elements: ");
for (i = 0; i < N; i++)
// Print value pointed by the pointer
printf("%d, ", *ptr);
// Move pointer to next array element
ptr++;
return 0;
Output:
Aim 34 : Write a program in C to find out maximum and minimum of some
values using function which will return an array.
Source Code :
#include <stdio.h>
int max(int num1, int num2);
int min(int num1, int num2);
int main()
int num1, num2, maximum, minimum;
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);
maximum = max(num1, num2); // Call maximum function
minimum = min(num1, num2); // Call minimum function
printf("\nMaximum = %d\n", maximum);
printf("Minimum = %d", minimum);
return 0;
int max(int num1, int num2)
{
return (num1 > num2 ) ? num1 : num2;
int min(int num1, int num2)
return (num1 > num2 ) ? num2 : num1;
Output:
Aim 35: Write a program in C to sort an array using pointers.
Source Code :
#include<stdio.h>
int main()
int *a,n,i,j,temp;
printf("Enter size of an array:");
scanf("%d",&n);
a=calloc(sizeof(int),n);
printf("Enter %d Elements: \n",n);
for(i=0;i<n;i++)
scanf("%d",a+i);
for(i=0;i<n-1;i++)
for(j=0;j<n-1;j++)
if(*(a+j)>*(a+j+1))
temp=*(a+j);
*(a+j)=*(a+j+1);
*(a+j+1)=temp;
printf("After sorting it in ascending order: ");
for(i=0;i<n;i++)
printf("%d\t",*(a+i));
return 0;
Output :
Aim 36: Write a program in C to print all alphabets using pointers.
Source Code :
#include<stdio.h>
int main()
char* c;
c = (char*)malloc(sizeof(char));
*c = 'A';
while (*c <= 'Z') {
printf("%c ", *c);
*c = *c + 1;
return 0;
Output :