You are on page 1of 8

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Science and Engineering
Semester: (Summer, Year: 2022), B.Sc. in CSE (Day)

LAB REPORT
Course Title: Structured Programming Lab

Course Code: CSE-104 Section: D-17

Lab Experiment Name: Lab Manual 6,7&8

Student Details

Name ID

Mushfikur Rahman Zisan


221002145

Submission Date : 06/09/2022


Course Teacher’s Name : Amena Zahan

[For Teachers use only: Don’t Write Anything inside this box]

Lab Report Status


Marks: ………………………………… Signature: .....................
Comments: .............................................. Date: ..............................
Lab Manual 6
1.Write a C program to convert Decimal to Binary number system.

#include<stdio.h>
int main()
{
int a[10],n,i;
printf("Enter the number : ");
scanf("%d",&n);
for(i=0; n>0; i++)
{
a[i]=n%2;
n=n/2;
}
printf("\n The Binary number is = ");
for(i=i-1; i>=0; i--)
{
printf("%d",a[i]);
}
return 0;
}

3. Write a C program to count the frequency of each element in an array.


#include <stdio.h>
void main()
{
int arr1[100], fr1[100];
int n, i, j, ctr;
printf("Input array size:");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0; i<n; i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
fr1[i] = -1;
}
for(i=0; i<n; i++)
{
ctr = 1;
for(j=i+1; j<n; j++)
{
if(arr1[i]==arr1[j])
{
ctr++;
fr1[j] = 0;
}
}
if(fr1[i]!=0)
{
fr1[i] = ctr;
}
}
printf("\nThe frequency : \n");
for(i=0; i<n; i++)
{
if(fr1[i]!=0)
{
printf("%d occurs %d times\n", arr1[i], fr1[i]);
}
}
}
4. Write a C Program to Find Transpose of a Matrix.

#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);

printf("\nEnter matrix elements:\n");


for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\n The matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
{
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}

for (int i = 0; i < r; ++i)


for (int j = 0; j < c; ++j)
{
transpose[j][i] = a[i][j];
}

printf("\nTranspose of the matrix:\n");


for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j)
{
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
Lab Manual 7 : User Defined Function
1.Write a C Program to convert a decimal number to an equivalent binary
number using function.
#include <stdio.h>
int isPerfect(int num);
void printPerfect(int start, int end);
int main()
{
int start, end;
printf("Enter lower limit : ");
scanf("%d", &start);
printf("Enter upper limit : ");
scanf("%d", &end);
printf("All perfect numbers between %d to %d are: \n", start, end);
printPerfect(start, end);
return 0;
}
int isPerfect(int num)
{
int i, sum;
sum = 0;
for(i=1; i<num; i++)
{
if(num % i == 0)
{
sum += i;
}
}
if(sum == num)
return 1;
else
return 0;
}
void printPerfect(int start, int end)
{

while(start <= end)


{
if(isPerfect(start))
{
printf("%d, ", start);
}
start++;
}
}
2. Write a C program to create menu driven calculator that performs basic
arithmetic operations (add, subtract, multiply and divide) using functions.
#include <stdio.h>
void main()
{
int num1,num2,opt;
printf("Enter the first Integer :");
scanf("%d",&num1);
printf("Enter the second Integer :");
scanf("%d",&num2);
printf("\nInput your option :\n");
printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("The Addition of %d and %d is: %d\n",num1,num2,num1+num2);
break;
case 2:
printf("The Substraction of %d and %d is: %d\n",num1,num2,num1-num2);
break;
case 3:
printf("The Multiplication of %d and %d is: %d\n",num1,num2,num1*num2);
break;
case 4:
if(num2==0)
{
printf("The second integer is zero. Devide by zero.\n");
}
else
{
printf("The Division of %d and %d is : %d\n",num1,num2,num1/num2);
}
break;
case 5:
break;
default:
printf("Input correct option\n");
break;
}
}
4.Write a C program to calculate sum of all digits of a number using recursion.
#include <stdio.h>
int sumOfDigits(int num);
int main()
{
int num, sum;

printf("Enter any number to find sum of digits: ");


scanf("%d", &num);
sum = sumOfDigits(num);

printf("Sum of digits of %d = %d", num, sum);

return 0;
}
int sumOfDigits(int num)
{
if(num == 0)
return 0;

return ((num % 10) + sumOfDigits(num / 10));


}
Lab Manual 8
1. Write a C Program to Copy a String.
#include <stdio.h>
#include <string.h>
int main()
{
char source[1000], destination[1000];
printf("Input a string\n");
gets(source);
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
return 0;
}
2. Write a C Program to Find the Length of a String.
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
printf("Enter a string to calculate its length\n");
gets(a);
length = strlen(a);
printf("Length of the string = %d\n", length);
return 0;
}
3. Write a C Program to Find the Frequency of Characters in a String.
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for (int i = 0; str[i] != '\0'; ++i) {
if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
4. Write a C Program to Reverse a Sentence by Recursion.
#include <stdio.h>
void reverse();
void main()
{
printf("Please enter a sentence: ");
reverse();
}
void reverse()
{
char c;
scanf("%c", &c); if (c != '\n') {
reverse();
printf("%c", c);
}
}

You might also like