DEPARTMENT OF COMPUTER SCIENCE
AND ENGINEERING
PROGRAMMING FUNDAMENTALS
CO 101
LAB FILE
SUBMITTED TO: SUBMITTED BY:
INDEX
S.NO Objective Date Sign
.
1. Write a C program to take input two integers and print their 22/08/23
sum
2. Write a C program to take input four integers and print their 22/08/23
average.
3. Write a C program to implement Relational Operators. 29/08/23
4. Write a C program to implement Logical Operators. 29/08/23
5. Write a C program to implement Bitwise Operators. 29/08/23
6. Write a C program to find the sum of a 3-digit number. 29/08/23
7. Write a C program to reverse a 3-digit number. 29/08/23
8. Write a program to implement Linear Search Algorithm. 05/09/23
9. Write a program to implement Binary Search Algorithm ( 05/09/23
recursion).
10. Write a program to take input a String Using scanf, gets and 05/09/23
fgets.
11. Write a Program to Print the following pattern 12/09/23
12. Write a Program to convert decimal to binary and vice versa. 19/09/23
13. Write a Program to implement the switch case statement. 19/09/23
14. Write a Program to generate the Fibonacci sequence using 19/09/23
recursion.
15. Write a Program to create an exponential function. 19/09/23
16. Write a Program to count the number of vowels in a given
string.
17. Write a Program to check if a given string is a palindrome or
not.
18. Write a Program to string concatenation.
EXPERIMENT 1
AIM: Write a C program to take input two integers and print their sum.
THEORY:
ALGORITHM:
Step 1: we will take two numbers num1 and num2 as input
Step 2: then applying sum = num1 + num2
Step 3: returning sum as the output
CODE:
#include<stdio.h>
Int main(){
int num1,num2,sum;
printf(“enter two integers”);
scanf(“%d %d”, &num1, &num2);
sum= num1 + num2;
printf(“%d + %d = %d”, num1 , num2 , sum);
return 0;
}
OUTPUT:
Flowchart:
Start
Read num1
and num2
Sum =
num1+num2
Print sum
End
EXPERIMENT 2
AIM: Write a C program to take input four integers and print their average.
Write algorithm and draw flowchart for the above programs as well.
THEORY:
ALGORITHM:
Step 1: we will take four integers from input.
Step 2: now finding the sum of all four integers.
Step 3: now to finding the average using sum/4.
Step 4: printing average as output.
CODE:
#include<stdio.h>
Int main(){
int a,b,c,d;
printf("enter the four numbers : ");
scanf("%d %d %d %d",&a,&b,&c,&d);
int sum = a+b+c+d;
printf("average of given four numbers is %d", sum/4);
return 0;
}
OUTPUT:
EXPERIMENT 3
AIM:Write a C program to implement Relational Operators.
THEORY:
ALGORITHM:
Step 1:
CODE:
#include<stdio.h>
int main(){
printf("%d \n", 2>2);
printf("%d \n", 2>=2);
printf("%d \n", 2<2);
printf("%d \n", 2<=2);
printf("%d \n", 2<3);
printf("%d \n", 2>3);
printf("%d \n", 4==4);
printf("%d \n", 4!=4);
printf("%d \n", 4!=3);
return 0;
}
OUTPUT:
EXPERIMENT 4
AIM:Write a C program to implement Logical Operators.
THEORY:
ALGORITHM:
Step 1:
CODE:
#include <stdio.h>
//implementing logical operators
int main(){
// and(&&) operator
printf("%d \n", 5>2 && 6<9);
printf("%d \n", 5>2 && 6>9);
printf("%d \n", 5<2 && 6<9);
printf("%d \n", 5<2 && 6>9);
// OR(||) operator
printf("%d \n", 5<2 || 6>9);
printf("%d \n", 5<2 || 6>9);
printf("%d \n", 5>2 || 6>9);
printf("%d \n", 5>2 || 6<9);
// NOT(!)operator
printf("%d \n", !(5<2));
printf("%d \n", !(5>2));
return 0;
}
OUTPUT :
EXPERIMENT 5
AIM: Write a C program to implement bitwise operator.
THEORY: Bitwise Operator works on two or more operands and checks every single binary bit of
those operands to give the desired result.
ALGORITHM:
Step 1:Read the operands.
Step 2: Select any bitwise operator.
Step 3: Perform the operation and note the result.
CODE:
#include <stdio.h>
int main()
unsigned char a = 5, b = 9;
printf("a = %d, b = %d\n", a, b);
printf("a&b = %d\n", a & b);
printf("a|b = %d\n", a | b);
printf("a^b = %d\n", a ^ b);
printf("~a = %d\n", a = ~a);
printf("b<<1 = %d\n", b << 1);
printf("b>>1 = %d\n", b >> 1);
OUTPUT:
EXPERIMENT 6
AIM: Write a C program to find the sum of digits of a 3 digit number.
THEORY: The concept of loop is used here along with the concept of division by 10. Division by 10
gives the last digit of a number.
ALGORITHM:
1. Read a number.
2. Divide the number by 10 and get its last digits.
3. Add the last digit to get the sum of the digits.
CODE:
#include<stdio.h>
int main(){
int n;
printf("enter the no. :");
scanf("%d",&n);
int sum=0;
while(n!=0){
int t=n%10;
sum+=t;
n/=10;
printf("sum of the digits of the given number is : %d",sum );
return 0;
OUTPUT:
EXPERIMENT 7
AIM: Write a C program to reverse a 3 digit number.
THEORY: Looping is used here along with division by 10 which gives the last digit as remainder.
ALGORITHM:
1. Read a number n.
2. Divide it by 10 and get the last digit.
3. Multiply it by 10 and add the remainder.
4. Repeat until the number becomes 0.
CODE:
#include <stdio.h>
int main()
{
printf(“Enter a number: ”);
int n;
int rev=0;
int r;
scanf(“%d”,&n);
while (n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf(“%d”,rev);
OUTPUT:
EXPERIMENT 8
AIM:
Write a program to implement Linear Search Algorithm.
THEORY:
ALGORITHM:
CODE:
#include<stdio.h>
int main(){
int i;
int arr[9]={2,4,6,9,14,17,19,25,34};
int key;
printf("key = ");
scanf("%d", &key);
for(i=0; i<9 ; i++){
if(key==arr[i]){
printf("%d is present at position %d in the array\n",key,i);
break;
}
}
if(i==9){
printf("%d is not present in the array\n", key);
}
return 0;
OUTPUT:
EXPERIMENT 9
AIM:Write a program to implement Binary Search Algorithm ( recursion).
THEORY:
ALGORITHM:
CODE:
#include <stdio.h>
int binrec(int array[], int s, int e, int element){
if (e >= s){
int mid = s + (e - s )/2;
if (array[mid] == element)
return mid;
if (array[mid] > element)
return binrec(array, s, mid-1, element);
return binrec(array, mid+1, e, element);
return -1;
int main(void){
int array[] = {3 , 5 , 10 , 34 , 36 , 78 , 90 , 102 , 234};
int n = 9;
int element = 78;
int found_index = binrec(array, 0, n-1, element);
if(found_index == -1 ) {
printf("Element not found in the array ");
else {
printf("%d found at index : %d",element ,found_index);
return 0;
OUTPUT:
EXPERIMENT 10
AIM: Write a program to take input a String Using scanf, gets and fgets.
THEORY:
ALGORITHM:
CODE: OUTPUT:
#include<stdio.h>
int main(){
char name[15];
printf("enter name : ");
scanf("%s", &name);
printf("my name is %s", name);
return 0;
#include<stdio.h>
int main(){
char name[15];
printf("enter name : ");
gets(name);
puts(name);
#include<stdio.h>
int main(){
char name[15];
printf("enter name :");
fgets(name,15,stdin);
printf("my name is: %s", name);
return 0; }
EXPERIMENT 11
AIM:
THEORY:
ALGORITHM:
CODE:
OUTPUT:
EXPERIMENT 12
AIM:Write a Program to convert decimal to binary and vice versa.
THEORY:
ALGORITHM:
Binary to decimal
CODE:
#include <stdio.h>
#include <math.h>
int convert(long long);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
//function definition
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
OUTPUT:
Decimal to Binary
CODE:
#include <stdio.h>
#include <math.h>
long long convert(int);
int main() {
int n;
long long bin;
printf("Enter a decimal number: ");
scanf("%d", &n);
bin = convert(n);
printf("%d in decimal = %lld in binary", n, bin);
return 0;
}
// function to convert decimal to binary
long long convert(int n) {
long long bin = 0;
int rem, i = 1;
while (n != 0) {
rem = n % 2;
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
OUTPUT:
EXPERIMENT 13
AIM: Write a Program to implement the switch case statement.
THEORY:
ALGORITHM:
CODE:
#include<stdio.h>
int main(){
int rs;
printf("enter the amount : ");
scanf("%d",&rs);
int rs100,rs50,rs20;
switch(1){
case 1 : rs100 = rs/100;
rs = rs - rs100*100;
printf("no. of notes of rs100 are %d \n", rs100);
case 2 : rs50 = rs/50;
rs = rs - rs50*50;
printf("no. of notes of rs50 are %d \n", rs50);
case 3 : rs20 = rs/20;
rs = rs - rs20*20;
printf("no. of notes of rs20 are %d \n", rs20);
case 4 : printf("no. of coins of rs1 are %d \n", rs);
}
return 0;
OUTPUT:
EXPERIMENT 14
AIM: Write a Program to generate the Fibonacci sequence using recursion.
THEORY:
ALGORITHM:
CODE:
#include<stdio.h>
void printFibonacci(int m){
static int m1=0,m2=1,m3;
if(m>0){
m3 = m1 + m2;
m1 = m2;
m2 = m3;
printf("%d ",m3);
printFibonacci(m-1);
}
}
int main(){
int m;
printf("Enter your preferred number of elements here: ");
scanf("%d",&m);
printf("The Fibonacci Series will be: ");
printf("%d %d ",0,1);
printFibonacci(m-2);
return 0;
}
OUTPUT:
EXPERIMENT 15
AIM:
THEORY:
ALGORITHM:
CODE:
OUTPUT:
EXPERIMENT 16
AIM: Write a Program to count the number of vowels in a given string.
THEORY:
ALGORITHM:
CODE:
#include<stdio.h>
int main()
{
int a= 0, count = 0;
char str[500];
printf("Input a string\n");
gets(str);
while (str[a] != '\0') {
if (str[a] == 'a' || str[a] == 'A' || str[a] == 'e' || str[a] == 'E' || str[a] == 'i' || str[a] == 'I' || str[a] =='o' ||
str[a]=='O' || str[a] == 'u' || str[a] == 'U')
count++;
a++;
}
printf("Number of vowels in the string: %d", count);
return 0;
}
OUTPUT:
EXPERIMENT 17
AIM: Write a Program to check if a given string is a palindrome or not.
THEORY:
ALGORITHM:
CODE:
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = { "tushar" };
int l = 0;
int h = strlen(str) - 1;
while (h > l) {
if (str[l++] != str[h--]) {
printf("%s is not a palindrome\n", str);
return 0;
}
}
printf("%s is a palindrome\n", str);
return 0;
}
OUTPUT:
EXPERIMENT 18
AIM: Write a Program to string concatenation.
THEORY:
ALGORITHM:
CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str1[50], str2[50];
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
strcat(str1, str2);
printf("\n String after concatenation is:\n%s", str1);
getch();
return 0;
}
OUTPUT:
EXPERIMENT 19
AIM: Write a Program to string comparison.
THEORY:
ALGORITHM:
CODE:
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "tushar", str2[] = "TushAr", str3[] = "tushar";
int result;
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
return 0;
}
OUTPUT:
EXPERIMENT 20
AIM: Write a Program to string reverse.
THEORY:
ALGORITHM:
CODE:
#include <stdio.h>
#include <string.h>
int main()
{
char str[200];
printf("Enter a string to reverse\n");
gets(str);
strrev(str);
printf("Reverse of the string: %s\n", str);
return 0;
}
OUTPUT:
EXPERIMENT 21
AIM: Write a Program to convert a string from lower case to upper case and vice versa.
THEORY:
ALGORITHM:
Lower case to upper case :
CODE:
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
char str[MAX_SIZE];
int i;
printf("Enter your text : ");
gets(str);
for(i=0; str[i]!='\0'; i++)
{
if(str[i]>='a' && str[i]<='z')
{
str[i] = str[i] - 32;
}
}
printf("Uppercase string : %s",str);
return 0;
}
OUTPUT:
Upper case to lower case :
CODE:
#include<stdio.h>
#include<string.h>
int main(){
char str[50];
int i;
printf("Enter the string: ");
scanf("%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nLower Case String is: %s",str);
return 0;
}
OUTPUT:
EXPERIMENT 22
AIM:Program for the addition of two 3 x 3 matrices.
THEORY:
ALGORITHM:
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int mat1[3][3], mat2[3][3], i, j, mat3[3][3];
printf("Enter 3 by 3 matrix 1 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d",&mat1[i][j]);
}
printf("Enter 3 by 3 matrix 2 elements :");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
scanf("%d",&mat2[i][j]);
}
printf("\n Adding the two matrix.....");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
mat3[i][j]=mat1[i][j]+mat2[i][j];
}
printf("\n Both matrix added successfully!");
printf("\nFinal matrix : \n");
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
printf("%d ",mat3[i][j]);
printf("\n");
}
getch();
return 0;
}
OUTPUT:
EXPERIMENT 23
AIM: Program to multiply two 3 x 3 matrices
THEORY:
ALGORITHM:
CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++){
scanf("%d",&b[i][j]);
}
}
printf("multiply of the matrix=\n");
for(i=0;i<r;i++) {
for(j=0;j<c;j++){
mul[i][j]=0;
for(k=0;k<c;k++){
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
EXPERIMENT 24
AIM: Program to find factorial of a number using recursion.
THEORY:
ALGORITHM:
CODE:
#include<stdio.h>
long int fact(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, fact(n));
return 0;
}
long int fact(int n) {
if (n>=1){
return n*fact(n-1);
}
else{
return 1;
}
}
OUTPUT:
EXPERIMENT 25
AIM: Write a C program to sort an array using Bubble sort.
THEORY:
ALGORITHM:
CODE: OUTPUT:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
EXPERIMENT 26
AIM: Write a C program to sort an array using selection sort.
THEORY:
ALGORITHM:
CODE:
#include <stdio.h>
int main() {
int arr[10]={23,56,1,4,8,3,2,34,345,12};
int n=10;
int i, j, pos, swap;
for (i = 0; i < (n - 1); i++) {
pos = i;
for (j = i + 1; j < n; j++) {
if (arr[pos] > arr[j])
pos = j;
}
if (pos != i) {
swap = arr[i];
arr[i] = arr[pos];
arr[pos] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}
OUTPUT:
EXPERIMENT 27
AIM: Write a C program to sort an array using insertion sort.
THEORY:
ALGORITHM:
CODE: OUTPUT:
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
return 0;
}