computerishan
computerishan
Kalopul, Kathmandu
Code:
#include<stdio.h>
int fact(int);
int main()
int n,f;
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 main()
int n,i;
scanf("%d",&n);
printf("Fibonacci series:");
for(i=0;i<n;i++){
printf("%d",fibo(i))
return 0;
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() {
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>
return 0;
}
#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];
scanf("%d", &n);
scanf("%d", &arr[i]);
arr[0] = arr[i];
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 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;
}
#include<stdio.h>
int main() {
char str[20];
scanf(“%s”, str);
return 0;}
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 main() {
char str[10];
scanf(“%s”, str);
if (palindrome(str)==1) {
else {
return 0;
int firstlett = 0;
if (str[firstlett] != str[lastlett]) {
return 0;
}
firstlett++;
lastlett--;
} return 1;
#include <stdio.h>
int stringLength(char str[]);
int main() {
char str[20];
printf(“Enter a string:”);
scanf(“%s”, str);
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;
#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);
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() {
int i;
scanf("%d", &staff[i].id);
scanf("%f", &staff[i].salary);
printf("\n");
return 0;
}
QN.16 Write a program which writes “Welcome to Xavier” in a file
#include <stdio.h>
int main() {
FILE *fptr;
//char str1[100];
//scanf(“%s”, str1);
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() {
FILE *fptr;
char choice[4];
do {
scanf("%d", &roll);
scanf("%s", choice);
} while (strcmp(choice, "no") || strcmp(choice, “NO”) || strcmp(choice, “No”)
!= 0);
fclose(fptr);
printf("\nContents of 'students.txt':\n\n");
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;
#include <stdio.h>
int main() {
if (rename("oldfile.txt", "newfile.txt") == 0) {
else {
if (remove("newfile.txt") == 0) {
else {
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{
};s[100];
int main() {
int i, num;
FILE *fptr;
scanf(“%d”, &num);
scanf("%d", &s[i].emp_id);
fclose(fptr);
printf("\nContents of 'record.txt':\n\n");
printf(“Name:%s\n”, s[i].emp_name);
fclose(fptr);
return 0;