Programming Assignment
Programming Assignment
(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-
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-
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-
bubbleSort(arr, size);
return 0;
}
Output-
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;
wordCount++;
}
}
wordCount++;
return 0;
}
Output-
int main() {
int rows, cols;
int matrix[MAX_ROWS][MAX_COLS];
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;
}
fclose(file);
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>
int main() {
FILE *sourceFile;
FILE *destinationFile;
char buffer[MAX_LENGTH];
size_t bytesRead;
if (sourceFile == NULL) {
printf("Error opening the source file. Make sure the file 'astrophysics.txt'
exists and is accessible.\n");
return 1;
}
if (destinationFile == NULL) {
printf("Error opening the blackhole file.\n");
fclose(sourceFile);
return 1;
}
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;
if (file == NULL) {
printf("Error opening the file.\n");
return 1;
}
fclose(file);
return 0;
}
Output-
12. Write a C program to define a structure representing a student'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;
return 0;
}
Output-
return age;
}
int main() {
struct Date birthdate, currentdate;
int 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];
};
int main() {
struct Book myBook;
modifyISBN(&myBook, "987654321");
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;
swap(&num1, &num2);
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Output-