XAVIER INTERNATIONAL COLLEGE
Kalopul, Kathmandu
Lab Report of C-Programming Batch -2080
Submitted By : Submitted To:
Ishan Thapa Senate Shah
Class: XII -SM56 Computer Teacher
QN 1.Write a C program to calculate the factorial of a number using
function.
Code:
#include<stdio.h>
int fact(int);
int main()
int n,f;
printf("Enter any number:");
scanf("%d",&n);
f=fact(n);
printf("factorial is %d",f);
return 0;
int fact(int n)
if(n<=1)
return 1;
else
return(n*fact(n-1));
}
QN 2. Write a C program to print fibonacci series of 6,12,18,30… upto 10
terms using loop.
Code:
#include<stdio.h>
int main()
int a=6,b=12,c;
printf("Fibonacci series");
printf("%d,%d",a,b);
for(int i=3;i<=10;i++)
c = a+b;
printf(",%d", c);
a = b;
b= c;
return 0;
}
QN 3.Write a C program to find the nth Fibonacci number using recursion.
Code:
#include<stdio.h>
int fibo(int n);
int main()
int n,i;
printf("Enter the number of terms:");
scanf("%d",&n);
printf("Fibonacci series:");
for(i=0;i<n;i++){
printf("%d",fibo(i))
return 0;
int fibo(int n){
if(n==0)
return 0;
else if(n==1)
return 1;
else
return(fibo(n-1)+fibo(n-2));
}
QN 4 . Write a function to reverse a given string using recursion.
#include <stdio.h>
void reverseString();
int main() {
printf("Enter a string: ");
reverseString();
return 0;
void reverseString() {
char ch;
scanf("%c", &ch);
if (ch != '\n') {
reverseString();
printf("%c", ch);
}
QN 5. Write a program to find sum of given ‘n’ numbers in array using
function.
#include <stdio.h>
int sumArray(int arr[] , int n);
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int result = sumArray(arr, n);
printf("The sum of the elements is: %d\n", result);
return 0;
}
int sumArray(int arr[], int n) {
int sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
}
return sum;
}
QN 6. Write a C program to implement a simple calculator (add,
subtract, multiply, divide) using functions.
#include<stdio.h>
float add(float , float );
float subtract(float , float );
float multiply(float , float );
float divide(float , float );
int main() {
float n1, n2;
int choice;
float result;
printf("Simple Calculator\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
printf("Enter two numbers: ");
scanf("%f %f", &n1, &n2);
switch (choice) {
case 1:
result = add(n1, n2);
printf("Result: %.2f\n", result);
break;
case 2:
result = subtract(n1, n2);
printf("Result: %.2f\n", result);
break;
case 3:
result = multiply(n1, n2);
printf("Result: %.2f\n", result);
break;
case 4:
if (n2 != 0) {
result = divide(n1, n2);
printf("Result: %.2f\n", result);
}
else {
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
float add(float x, float y) {
return x + y;
}
float subtract(float x, float y) {
return x - y;
}
float multiply(float x, float y) {
return x * y;
}
float divide(float x, float y) {
return x / y;
}
QN 7.Write a C program to find the sum of digits of a number using
recursion.
#include<stdio.h>
int sum_of_digit(int n);
int main()
{
int num;
printf(“Enter the number:”);
scanf(“%d”, &num);
int result = sum_of_digit(num);
printf("Sum of digits in %d is %d\n", num, result);
return 0;
}
int sum_of_digit(int n)
{
if (n == 0)
return 0;
else
return (n % 10 + sum_of_digit(n / 10));
}
QN 8.Write a C program to find greatest number among different
numbers using array with function.
#include<stdio.h>
int main() {
int n;
int arr[10];
printf("Enter the number of elements (1 to 10): ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("Enter number%d: ", i + 1);
scanf("%d", &arr[i]);
for (int i = 1; i < n; i++) {
if (arr[0] < arr[i]) {
arr[0] = arr[i];
printf("Largest element = %d", arr[0]);
return 0;
}
QN 9.Write a C program to find whether a given number is Armstrong or
not using function.
#include<stdio.h>
#include<math.h>
int sumOfPowers(int num, int power);
int isArmstrong(int num);
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isArmstrong(num)) {
printf("%d is an Armstrong number.\n", num);
} else {
printf("%d is not an Armstrong number.\n", num);
}
return 0;
}
int isArmstrong(int num) {
int digits = 0, temp = num;
while (temp != 0) {
temp /= 10;
digits = digits + 1;
}
return (num == sumOfPowers(num, digits));
}
int sumOfPowers(int num, int power) {
int sum = 0;
while (num != 0) {
int digit = num % 10;
sum += pow(digit, power);
num /= 10;
}
return sum;
}
QN 10 .Write a function that returns the number of vowels in a given
string.
#include<stdio.h>
int count_vowels(char str[]);
int main() {
char str[20];
printf(“Enter the string:”);
scanf(“%s”, str);
int num = count_vowels(str);
printf("Number of vowels in '%s': %d\n", str, num);
return 0;}
int count_vowels(char str[]) {
int count =0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U') {
count++;
return count;
}
QN 11.Write a function to check if a given string is a palindrome.
#include<stdio.h>
#include<string.h>
int palindrome(const char str[]);
int main() {
char str[10];
printf(“Enter the string:”);
scanf(“%s”, str);
if (palindrome(str)==1) {
printf("The string is a palindrome.\n");
else {
printf("The string is not a palindrome.\n");
return 0;
int palindrome(char str[]) {
int firstlett = 0;
int lastlett = strlen(str) - 1;
while (firstlett <= lastlett) {
if (str[firstlett] != str[lastlett]) {
return 0;
}
firstlett++;
lastlett--;
} return 1;
QN 12.Write a C program to implement a function that takes a string and
returns its length.
#include <stdio.h>
int stringLength(char str[]);
int main() {
char str[20];
printf(“Enter a string:”);
scanf(“%s”, str);
int len = stringLength(str);
printf("Length of the string: %d\n", len);
return 0;
}
int stringLength(char str[]) {
int length = 0;
while (str[length] != '\0') {
length = length + 1;
}
return length;
}
QN 13. Write a C program to create a structure representing a student
(name, age, and roll number), input data for 5 students, and display
the details of each student.
#include<stdio.h>
struct student{
char name[30];
int age;
int roll;
};
int main(){
struct student s[5], temp;
int i, n;
for(i = 0; i < 5; i++)
{
printf(“Roll number:%d\n”, s[i].roll=i+1);
printf(“Enter name: ”);
scanf(“%s”,s[i].name);
printf(“Enter Age: ”);
scanf(“%d”,&s[i].age);
}
printf(“\n\nThe details of each students:\n”);
for(i = 0; i < 5; i++)
{
printf(“Roll number:%d\n”, s[i].roll);
printf(“Enter name:%s\n”,s[i].name);
printf(“Enter Age:%d\n”,s[i].age);
printf(“\n”);
}
return 0;
}
QN 14. Write a C program to store information of 5 books (title, author,
price) using an array of structures and display the books sorted by
price.
#include <stdio.h>
#include <string.h>
struct num_books {
char title[50];
char author[50];
float price;
};
int main() {
struct num_books books[5], temp;
int n;
printf(“Enter the number of books: “);
scanf(“%d”, &n);
for (int i = 0; i < 5; i++) {
printf("Enter details for book %d:\n", i + 1);
printf("Enter title: ");
scanf(" %[^\n]%*c", books[i].title);
printf("Enter author: ");
scanf(" %[^\n]%*c", books[i].author);
printf("Enter price: ");
scanf("%f", &books[i].price);
printf("\n");
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (books[i].price > books[j].price) {
temp = books[i];
books[i] = books[j];
books[j] = temp;
}
}
}
printf("Books sorted by price:\n");
for (int i = 0; i < 5; i++)
{
printf("\nBook: %d\n", i + 1);
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Price: $%.2f\n", books[i].price);
}
return 0;
}
QN 15. Write a C program using structure to input id, name, and salary of
10 staffs. Display id, name and salary of those staff whose salary range
from 30000 to 45000.
#include <stdio.h>
struct Staff {
int id;
char name[50];
float salary;
};
int main() {
struct Staff staff[100];
int i;
for (i = 0; i < 100; i++) {
printf("Enter details for staff %d:\n", i + 1);
printf("Enter ID: ");
scanf("%d", &staff[i].id);
printf("Enter name: ");
scanf(" %[^\n]%*c", staff[i].name);
printf("Enter salary: ");
scanf("%f", &staff[i].salary);
printf("\n");
printf("Staff members with salary between 30000 and 45000:\n");
for (i = 0; i < 100; i++)
if (staff[i].salary >= 30000 && staff[i].salary <= 45000)
printf("\nID: %d\n", staff[i].id);
printf("Name: %s\n", staff[i].name);
printf("Salary: %.2f\n", staff[i].salary);
return 0;
}
QN.16 Write a program which writes “Welcome to Xavier” in a file
#include <stdio.h>
int main() {
FILE *fptr;
fptr = fopen("welcome.txt", "w");
//char str1[100];
//printf(“Enter ’welcome to xavier’:”);
//scanf(“%s”, str1);
//fprintf(fptr, “%s”, str1);
fprintf(fptr, "Welcome to Xavier");
fclose(fptr);
return 0;
}
17. Write a program to open a new file and read roll, name, address and
phone_no of students until user says “no”, after reading the data, write it to
the file then display the content of the file.
#include <stdio.h>
#include <string.h>
int main() {
int roll, ph;
char name[30], adds[20];
FILE *fptr;
char choice[4];
fptr = fopen("students.txt", "w");
do {
printf("Enter roll number: ");
scanf("%d", &roll);
printf("Enter name: ");
scanf(" %[^\n]%*c", name);
printf("Enter address: ");
scanf(" %[^\n]%*c", adds);
printf("Enter phone number: ");
scanf(" %d", &ph);
fprintf(fptr, "%d%s %s%d\n\n" , roll, name, adds, ph);
printf("Do you want to enter another student (yes/no)? ");
scanf("%s", choice);
} while (strcmp(choice, "no") || strcmp(choice, “NO”) || strcmp(choice, “No”)
!= 0);
fclose(fptr);
fptr = fopen("students.txt", "r");
printf("\nContents of 'students.txt':\n\n");
while (fscanf(fscanf(fptr, “%d%s%s%d”, roll, name, adds, ph) != EOF)
printf(“%d%s%s%d”, roll, name, adds, ph);
fclose(fptr);
return 0;
}
QN 18 .Write a program to input the employee name, address and
salary of 5 employees and display the records in proper format using
structure.
#include <stdio.h>
struct employee {
char name[50], add[20];
float salary;
};
int main() {
struct employee s[100];
int i;
for (i = 0; i < 5; i++) {
printf("Enter details of student %d:\n", i + 1);
printf("Enter name: ");
scanf(" %[^\n]%*c", s[i].name);
printf(“Enter the address:”);
scanf(“%s”, s[i].add);
printf("Enter salary: ");
scanf("%f", &s[i].salary);
printf(“\n”);
}
printf(“\n\nThe details of five employees:\n”);
for (i = 0; i < 5; i++)
{
printf("Enter details of student %d:\n", i + 1);
printf("Enter name: %s\n", s[i].name );
printf(“Enter the address: %s\n”, s[i].add);
printf("Enter salary: %f\n ", s[i].salary);
}
return 0;
}
QN 19 .Write a C program to rename and delete a data file using rename
and remove command.
#include <stdio.h>
int main() {
if (rename("oldfile.txt", "newfile.txt") == 0) {
printf("File renamed successfully from 'oldfile.txt' to 'newfile.txt'.\n");
else {
printf("Error: Could not rename the file.\n");
if (remove("newfile.txt") == 0) {
printf("File 'newfile.txt' deleted successfully.\n");
else {
printf("Error: Could not delete the file.\n");
return 0;
}
QN 20.Write a C program to enterid, name, and post of the employee and
store them in a data file named “record.txt”. Display each record on the
screen in appropriate format.
#include <stdio.h>
#include <string.h>
struct employee{
int emp_id, i, num;
char emp_name[30], emp_post[20];
};s[100];
int main() {
int i, num;
FILE *fptr;
fptr = fopen("record.txt", "w");
printf(“Enter the no of records:”);
scanf(“%d”, &num);
for(i=0; i < num; i++){
printf("Enter id of employee: ");
scanf("%d", &s[i].emp_id);
printf("Enter name: ");
scanf(" %s", s[i].emp_name);
printf("Enter the post of employee: ");
scanf(" %s", s[i].emp_post);
fprintf(fptr, "%d%s %s%" , s[i].emp_id, s[i].emp_name, s[i].emp_post);
}
fclose(fptr);
fptr = fopen("record.txt", "r");
printf("\nContents of 'record.txt':\n\n");
while (fscanf(fptr, “%d%s%s”, &s[i].emp_id, s[i].emp_name,
s[i].emp_post) != EOF)
for(i=0; i < num; i++){
printf(“Employee ID:%d\n”, s[i].emp_id);
printf(“Name:%s\n”, s[i].emp_name);
printf(“Post of the employee:%s\n”, s[i].emp_post);
fclose(fptr);
return 0;