Programming Lab File
Programming Lab File
Programming Lab File
TECHNOLOGICAL
UNIVERSITY
PROGRAMMING FUNDAMENTALS
LAB FILE
Source Code:
#include<stdio.h>
int main()
{
float principle, time, rate, SI;
printf("Enter principle value: ");
scanf("%f", &principle);
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];
}
}
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;
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;
scanf("%d", &n);
while(n>=1) {
sum += n % 10;
n = n / 10;
return 0;
Output :
Aim 7: Write a program to reverse a 5 digit number.
Source Code:
#include <stdio.h>
int main(){
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
n /= 10;
return 0;
Output :
Aim 8: Write a program to convert decimal to binary.
Source Code:
#include <stdio.h>
int main()
scanf("%d", &num);
rem = num % 2;
place *= 10;
num /= 2;
return 0;
Output :
Aim 9: Write a program to convert binary to decimal.
Source Code:
#include <stdio.h>
int main()
scanf("%d", &binary);
while(temp > 0)
base = base * 2;
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("1)Addition \n");
printf("2)Subtraction\n");
printf("3)Multiplication \n");
printf("4)Division \n");
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;
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",&num[i]);
p= num[i];
num[i] = num[j];
num[j] = p;
}
}
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()
scanf("%d", &Size);
scanf("%d", &arr[i]);
count = 1;
for(j = i + 1; j < Size; j++)
if(arr[i] == arr[j])
count++;
freq [j] = 0;
if(freq [i] != 0)
if(freq [i] != 0)
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;
scanf("%d", &mat1[c][d]);
else
scanf("%d", &mat2[c][d]);
multiply[c][d] = sum;
sum = 0;
}
}
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 the location where you want to insert an new element - \n");
scanf("%d", &position);
array[position-1] = value;
Source Code:
#include <stdio.h>
int main()
{
int array[100], position, i, n;
}
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 ()
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;
return 0;
}
Output:
Aim 21: Write a program in C to swap two numbers using function.
Source code:
#include<stdio.h>
int main()
int a, b;
swap(a, b);
return 0;
int temp;
temp = x;
x = y;
y = temp;
}
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() {
scanf("%d", &num);
scanf("%d", &arr1[i]);
arr2[i] = arr1[i];
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 ;
scanf("%d", &n);
scanf("%d",&a[i]);
if(a[i]!=-1)
if(a[i]==a[j])
{
c++;
a[j]=-1;
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;
scanf("%d",&n1);
scanf("%d",&n2);
n3=n1+n2;
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;
scanf("%d",&b[i]);
c[k]=b[i];
k++;
for(int i=0;i<n3;i++)
printf("%d ",c[i]);
for(int i=0;i<n3;i++)
int temp;
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>
int main(){
scanf("%d", &size);
scanf("%d", &a[i]);
if(a[i] % 2 == 0){
even[Ecount] = a[i];
Ecount++;
else{
odd[Ocount] = a[i];
Ocount++;
}
display(even, Ecount);
display(odd, Ocount);
return 0;
int i;
printf("\n");
Output :
Aim 26: Write a program to display all prime numbers in an interval.
Source code :
#include <stdio.h>
int main()
while (a<b) {
flag = 0;
if (a<= 1) {
++a;
continue;
if (a% i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
++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()
scanf("%d",&num);
fact=find_factorial(num);
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 mid;
if (a > b)
return -1;
mid = (a + b) / 2;
if (arr[mid] == item)
return mid;
int main()
int item = 0;
scanf("%d", &item);
if (index == -1)
else
return 0;
Output:
Aim 29: Write a program in C to calculate the power of any number using
recursion
Source Code:
#include<stdio.h>
if(power==0){
return 1;
else{
return numb*exp(numb,power-1);
int main()
int n,p,result;
scanf("%d", &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]);
int main()
if (n1 != m2)
else
scanf("%d", &a[i][j]);
scanf("%d", &b[i][j]);
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;
if (j < n2)
if (k < n1)
k++;
k = 0;
j++;
j = 0;
i++;
int i, j;
{
for (j = 0; j < n2; 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 main()
int array[10] = { 23, 43, 35, 38, 67, 12, 76, 10, 34, 8 };
return 0;
int max;
if (lower == upper)
return list[lower];
else
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 main() {
return 0;
if (n2 != 0)
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;
scanf("%d", &N);
scanf("%d", ptr);
ptr++;
ptr = arr;
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 main()
return 0;
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;
scanf("%d",&n);
a=calloc(sizeof(int),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;
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';
*c = *c + 1;
return 0;
Output :