0% found this document useful (0 votes)
41 views27 pages

Programming Assignment

The document contains C program code snippets to solve various programming problems. These include programs to check palindromes, reverse arrays, count character occurrences in a string, calculate GCD and LCM, sort arrays using bubble sort, check perfect numbers, count words in a sentence, find matrix transpose, copy file contents, read binary student records, define and read student structures, calculate age by comparing dates, and modify ISBN using structures and pointers.

Uploaded by

s.mohaiminmahin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
41 views27 pages

Programming Assignment

The document contains C program code snippets to solve various programming problems. These include programs to check palindromes, reverse arrays, count character occurrences in a string, calculate GCD and LCM, sort arrays using bubble sort, check perfect numbers, count words in a sentence, find matrix transpose, copy file contents, read binary student records, define and read student structures, calculate age by comparing dates, and modify ISBN using structures and pointers.

Uploaded by

s.mohaiminmahin
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

Bangladesh University of Business and Technology

(BUBT)

Assignment-2

Submitted by : Submitted to :
Name : Sumon Saha
Name : Rakib Hossan
Intake : 44(DH) Designation : Assistant Professor.
ID : 20235203073
Section :2 Department : CSE
Department : CSE
1. Create a C program to check whether a given string is a palindrome or
not.
#include<stdio.h>
int main()
{
int i,len,flag=0;
char str[20];
printf("Enter a string to check palindrome:");
gets(str);
len = strlen(str);

for(i=1;i<len;i++){
if (str[i]!=str[len-i-1]){
flag=1;
break;
}
}
if(flag==0){
printf("this is an pelindrome");
}
else
printf("this is not a pelindrome");

}
Output-

2. Implement a C program to reverse an array in-place.


#include<stdio.h>
int main()
{
int i, a[7];
printf("Enter array elements: ");
for(i=0; i<=6;i++){
scanf("%d",&a[i]);
}
for(i=6;i>=0;i--){
printf("%d ",a[i]);
}
}}
Output-

3.Write a C program to count the occurrence of a specific character in a given


string.
#include<stdio.h>
#include<string.h>

int main()
{
char str[40],search;
int i,count;
printf("Enter some string :");
gets(str);
printf("Enter a charcter that you want to search : ");
search = getchar();
count =0;
i=0;
for(i=0;str[i]!='\0';i++){
if(str[i]==search){
count++;
}
}
printf("total occurrence %c = %d",search,count);
}
Output-

4. Implement a C program to find the GCD (Greatest Common Divisor) and


LCM (Least Common Multiple) of two numbers.
#include<stdio.h>
int main()
{
int num1,num2,count =1,small,gcd,lcm;
printf("Enter two numbers :");
scanf("%d%d",&num1,&num2);

small = (num1<num2)?num1:num2;
while(count<=small){
if (num1%count ==0 && num2%count==0){
gcd=count;
}
count++;
}
lcm = (num1*num2)/gcd;
printf("GCD = %d && LCM=%d",gcd,lcm);
}
Output-

5. Write a C program to sort an array of integers using the Bubble Sort


algorithm.
#include<stdio.h>

void bubbleSort(int arr[], int size) {


int i, j, temp;
for (i = 0; i < size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

bubbleSort(arr, size);

printf("\nSorted array: ");


for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

return 0;
}
Output-

6. Create a C program to check if a given number is a perfect number or not.


#include<stdio.h>
int main()
{
int i,n,sum=0;
printf("Enter any perfect number :");
scanf("%d", &n);

for(i=1;i<n;i++){
if(n%i==0){
sum = sum +i;
}
}
if (sum == n){
printf("This is perfect number");
}
else printf("This is not perfect number");

}
Output-
7. Write a C program to count the number of words in a given sentence.

#include <stdio.h>

int main() {
char sentence[1000];
int wordCount = 0;
int i;

printf("Enter a sentence: ");


fgets(sentence, sizeof(sentence), stdin);
for (i = 0; sentence[i] != '\0'; i++) {

if (sentence[i] == ' ' || sentence[i] == '\n' || sentence[i] == '\t') {

wordCount++;
}
}

wordCount++;

printf("Number of words in the sentence: %d\n", wordCount);

return 0;
}
Output-

8. Write a C program to find the transpose of a matrix.


#include <stdio.h>

#define MAX_ROWS 100


#define MAX_COLS 100

int main() {
int rows, cols;
int matrix[MAX_ROWS][MAX_COLS];

printf("Enter the number of rows (max %d): ", MAX_ROWS);


scanf("%d", &rows);

printf("Enter the number of columns (max %d): ", MAX_COLS);


scanf("%d", &cols);

printf("Enter the elements of the matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

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


for (int j = 0; j < cols; j++) {
for (int i = 0; i < rows; i++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}

return 0;
}
Output-
9. Write a C program to read a text file and display its content on the
console.
#include <stdio.h>

int main() {
FILE *file;
file = fopen("data.txt", "w");
if (file == NULL) {
printf("Error creating the file.\n");
return 1;
}

fprintf(file, "Hello, this is a text file.\n");


fprintf(file, "It contains some lines of text.\n");
fprintf(file, "We will read this file and display its content on the console.\n");

fclose(file);

file = fopen("data.txt", "r");


if (file == NULL) {
printf("Error opening the file.\n");
return 1;
}

printf("Content of the file:\n");

char ch;
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}

fclose(file);

return 0;
}
Output:
10. Implement a C program to copy the content of one text file to another.
#include <stdio.h>

#define MAX_LENGTH 1000

int main() {
FILE *sourceFile;
FILE *destinationFile;
char buffer[MAX_LENGTH];
size_t bytesRead;

sourceFile = fopen("astrophysics.txt", "r");

if (sourceFile == NULL) {
printf("Error opening the source file. Make sure the file 'astrophysics.txt'
exists and is accessible.\n");
return 1;
}

destinationFile = fopen("blackhole.txt", "w");

if (destinationFile == NULL) {
printf("Error opening the blackhole file.\n");
fclose(sourceFile);
return 1;
}

printf("Copying content from 'astrophysics.txt' to blackholet.xt'...\n");

while ((bytesRead = fread(buffer, 1, sizeof(buffer), sourceFile)) > 0) {


fwrite(buffer, 1, bytesRead, destinationFile);
}

printf("Content copied successfully.\n");

fclose(sourceFile);
fclose(destinationFile);

return 0;
}
Output-
11. Create a C program to append data to an existing text file. Write a C program
to read a binary file containing student records and display them on the
console.
#include <stdio.h>

struct Student {
char name[100];
int age;
int rollNumber;
};

int main() {
FILE *file;
struct Student student;

file = fopen("data.txt", "rb");

if (file == NULL) {
printf("Error opening the file.\n");
return 1;
}

while (fread(&student, sizeof(struct Student), 1, file) == 1) {


printf("Name: %s\n", student.name);
printf("Age: %d\n", student.age);
printf("Roll Number: %d\n", student.rollNumber);
printf("\n");
}

fclose(file);

return 0;
}
Output-
12. Write a C program to define a structure representing a student&#39;s
information (Roll Number, Name, Marks). Read and display the details of a
student using that structure.
#include <stdio.h>

struct Student {
int rollNumber;
char name[100];
float marks;
};

int main() {
struct Student student;

printf("Enter Roll Number: ");


scanf("%d", &student.rollNumber);

printf("Enter Name: ");


scanf("%s", student.name);

printf("Enter Marks: ");


scanf("%f", &student.marks);
printf("\nStudent Details:\n");
printf("Roll Number: %d\n", student.rollNumber);
printf("Name: %s\n", student.name);
printf("Marks: %.2f\n", student.marks);

return 0;
}
Output-

13. Write a C program to create a structure representing a date (day, month,


year). Calculate the age of a person by comparing the birthdate with the
current date.
#include <stdio.h>
#include <stdbool.h>
struct Date {
int day;
int month;
int year;
};

bool isLeapYear(int year) {


return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}

int calculateAge(struct Date birthdate, struct Date currentdate) {


int age = currentdate.year - birthdate.year;

if (currentdate.month < birthdate.month || (currentdate.month ==


birthdate.month && currentdate.day < birthdate.day)) {
age--;
}

return age;
}

int main() {
struct Date birthdate, currentdate;
int age;

printf("Enter Birthdate (day month year): ");


scanf("%d %d %d", &birthdate.day, &birthdate.month, &birthdate.year);

printf("Enter Current Date (day month year): ");


scanf("%d %d %d", &currentdate.day, &currentdate.month,
&currentdate.year);

age = calculateAge(birthdate, currentdate);


printf("Age: %d years\n", age);

return 0;
}
Output:
15. Create a C program to create a structure representing a book (Title,
Author, ISBN) and pass it to a function using pointers to modify its ISBN
number.
#include <stdio.h>
#include <string.h>

struct Book {
char title[100];
char author[100];
char isbn[20];
};

void modifyISBN(struct Book *book, const char *newISBN) {


strcpy(book->isbn, newISBN);
}

int main() {
struct Book myBook;

strcpy(myBook.title, "The Brief History of Time");


strcpy(myBook.author, "Stiphen Hawkings");
strcpy(myBook.isbn, "123456789");
printf("Original Book Details:\n");
printf("Title: %s\n", myBook.title);
printf("Author: %s\n", myBook.author);
printf("ISBN: %s\n", myBook.isbn);

modifyISBN(&myBook, "987654321");

printf("\nModified Book Details:\n");


printf("Title: %s\n", myBook.title);
printf("Author: %s\n", myBook.author);
printf("ISBN: %s\n", myBook.isbn);

return 0;
}
Output:
14. Implement a C program to swap two integers using
pointers.
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int num1, num2;

printf("Enter the first integer: ");


scanf("%d", &num1);

printf("Enter the second integer: ");


scanf("%d", &num2);

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;
}

Output-

You might also like