Himanshu
Himanshu
Submitted To:
Submitted By:
Mr.Saurabh Dhanik
Himanshu Nath
Assistant Professor BCA
School of Computing Section:
c
Graphic Era Hill University Roll
No.________ Bhimtal Campus
1 .Write a C program to check if a given number is an Armstrong number.
#include <stdio.h>
int main() {
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
if (result == num)
else
return 0;
}
1. Write a C program to generate the Fibonacci series up to a given number of terms.
Answer :
#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
scanf("%d", &n);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
return 0;
}
3. Define a structure for a student with attributes (name, roll number, marks) and display the student
details.
#include <stdio.h>
struct Student {
};
int main() {
fgets(student.name, sizeof(student.name), stdin); // Using fgets to input name (to allow spaces)
scanf("%d", &student.rollNumber);
scanf("%f", &student.marks);
printf("Name: %s", student.name); // %s to print string, fgets includes the newline at the end
return 0;
}
4 Define a union for employee details (name, ID, salary) and show how memory is shared.
#include <stdio.h>
#include <string.h>
union Employee {
};
int main() {
fgets(emp.name, sizeof(emp.name), stdin); // Input name using fgets (to allow spaces)
scanf("%d", &emp.ID);
scanf("%f", &emp.salary);
printf("Name: %s", emp.name); // Will show the name, but ID and Salary may be overwritten
printf("ID: %d\n", emp.ID); // Will show the ID, but Salary may be overwritten
printf("Salary: %.2f\n", emp.salary); // Will show the salary, but Name and ID may be overwritten
return 0;
}
5. Write a C program to dynamically allocate memory for an array using malloc, accept user input,
and print the values.
#include <stdio.h>
int main() {
int n, i;
scanf("%d", &n);
if (arr == NULL) {
scanf("%d", &arr[i]);
free(arr);
return 0;
}
6. Write a C program to dynamically allocate memory for an array using calloc and demonstrate the
difference from malloc.
#include <stdio.h>
int main() {
int n, i;
scanf("%d", &n);
if (arr_malloc == NULL) {
return 1;
if (arr_calloc == NULL) {
return 1;
scanf("%d", &arr_malloc[i]);
free(arr_malloc);
free(arr_calloc);
return 0;
}
7. Write a C program to dynamically resize an allocated memory block using realloc.
#include <stdio.h>
int main() {
int *arr;
scanf("%d", &initial_size);
if (arr == NULL) {
scanf("%d", &arr[i]);
scanf("%d", &new_size);
if (arr == NULL) {
scanf("%d", &arr[i]);
free(arr);
return 0;
}
8. Write a C program to create a singly linked list and display the elements.
#include <stdio.h>
#include <stdlib.h>
struct Node {
};
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Allocate memory for new
node
if (head == NULL) {
return;
printf("%d -> ", temp->data); // Print the data of the current node
int main() {
int n, data;
scanf("%d", &n);
scanf("%d", &data);
if (head == NULL) {
head = newNode;
} else {
temp = head;
displayList(head);
return 0;
}
9. Write a C program to find the largest element in an array.
#include <stdio.h>
int main() {
int n, i, largest;
scanf("%d", &n);
scanf("%d", &arr[i]);
largest = arr[0];
return 0;
}
10. Write a C program to check if a number is even or odd using bitwise operators.
#include <stdio.h>
int main() {
int num;
scanf("%d", &num);
if (num & 1) {
} else {
return 0;
11. Write a C program to read n elements in an array and perform the following operations: - Insert
an element at a given position. - Delete an element from a given position. - Display the array
elements.
#include <stdio.h>
if (n == 0) {
printf("Array is empty.\n");
return;
printf("\n");
printf("Invalid position.\n");
return;
// Shift elements to the right to make space for the new element
(*n)++;
printf("Invalid position.\n");
return;
(*n)--;
// Main function
int main() {
scanf("%d", &n);
scanf("%d", &arr[i]);
do {
printf("\nMenu:\n");
printf("4. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
scanf("%d", &element);
scanf("%d", &position);
break;
case 2:
scanf("%d", &position);
deleteElement(arr, &n, position);
break;
case 3:
displayArray(arr, n);
break;
case 4:
break;
default:
return 0;
}
12. Write a C program to read an m × n matrix and find its transpose
#include <stdio.h>
int main() {
int m, n;
scanf("%d", &m);
scanf("%d", &n);
scanf("%d", &matrix[i][j]);
transpose[j][i] = matrix[i][j];
}
// Display the original matrix
printf("\n");
printf("\n");
return 0;