Co 101 Lab File
Co 101 Lab File
AND ENGINEERING
PROGRAMMING FUNDAMENTALS
CO 101
LAB FILE
2. Write a C program to take input four integers and print their 22/08/23
average.
10. Write a program to take input a String Using scanf, gets and 05/09/23
fgets.
12. Write a Program to convert decimal to binary and vice versa. 19/09/23
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
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
THEORY: Bitwise Operator works on two or more operands and checks every single binary bit of
those operands to give the desired result.
ALGORITHM:
CODE:
#include <stdio.h>
int main()
unsigned char a = 5, b = 9;
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.
CODE:
#include<stdio.h>
int main(){
int n;
scanf("%d",&n);
int sum=0;
while(n!=0){
int t=n%10;
sum+=t;
n/=10;
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.
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);
}
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>
if (e >= s){
if (array[mid] == element)
return mid;
return -1;
int main(void){
int n = 9;
if(found_index == -1 ) {
printf("Element not found in the array ");
else {
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];
scanf("%s", &name);
return 0;
#include<stdio.h>
int main(){
char name[15];
gets(name);
puts(name);
#include<stdio.h>
int main(){
char name[15];
fgets(name,15,stdin);
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>
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
//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;
return 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);
}
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);
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);
OUTPUT:
EXPERIMENT 17
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;
}
}
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;
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];
strrev(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:
int main()
{
char str[MAX_SIZE];
int i;
printf("Enter your text : ");
gets(str);
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
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]);
}
}
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;
}
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;
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]);
}
return 0;
}