16 26 Programs
16 26 Programs
16 26 Programs
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
FILE *fptr;
char filename[100], c;
printf("Enter the filename to open \n");
scanf("%s", filename);
// Open file
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("Cannot open file \n");
exit(0);
}
// Read contents from file
c = fgetc(fptr);
while (c != EOF)
{
printf ("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
getch();
return 0;
}
Output:
Program 17: write a c program to copy one file to another file and while doing replace all lowercase
character to their equivalent upper case character.
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main()
char ch;
fp1=fopen("sourcefile.c","r");
if(fp1 ==NULL)
exit(1);
fp2=fopen("targetfile.c","w");
if(fp2 ==NULL)
fclose(fp1);
exit(1);
while((ch=fgetc(fp1))!=EOF)
ch=toupper(ch);
fputc(ch,fp2);
return 0;
}
Output:
Program 18: write a c program to determine if the given string is a palindrome or not.
#include <stdio.h>
#include<conio.h>
#include <string.h>
int main()
{
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++)
{
if(string1[i] != string1[length-i-1])
{
flag = 1;
break;
}
}
if (flag)
{
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
getch();
return 0;
}
Output:
Program 19: write a c program to calculate the power using recursive function.
#include <stdio.h>
if(a != 0)
else
return 1;
int main() {
scanf("%d", &base);
scanf("%d", &a);
return 0;
}
Output:
Program 20: Write C programs to find the factorial of a given integer. Use both recursive and non
Recursive functions
#include <stdio.h>
#include <conio.h>
int recfactorial(int x)
int f;
if(x == 0)
return(1);
else
f = x * recfactorial(x - 1);
return(f);
int nonrecfactorial(int x)
int i, f = 1;
f = f * i;
return(f);
int main()
int n, a, b;
clrscr();
printf("Enter any number\n");
scanf("%d", &n);
a = recfactorial(n);
b = nonrecfactorial(n);
getch();
return 0;
Output:
Program 21: Write C programs to find the GCD of a given integer. Use both recursive and non
Recursive functions.
#include<stdio.h>
#include<conio.h>
If (y == 0)
return(x);
else
return(recgcd(y, x % y));
int z;
while(x % y != 0)
z = x % y;
x = y;
y= z;
return (y);
int main()
inta, b, c, d;
printf("Enter two numbers a, b\n");
c = recgcd(a, b);
d = nonrecgcd(a, b);
getch();
return 0;
Output:
Searching and sorting:-
Program 22: write a program that uses non recursive function for a key value in a given list of integers
using linear search method.
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d integer(s) \n",n);
for (c = 0; c< n; c++)
scanf("%d", &array[c]);
printf("Enter a number to search\n");
scanf("%d", &search);
for (c = 0; c<n; c++)
{
if (array[c] == search) /* If required element is found */
{
printf("%d is present at location %d \n", search, c);
break;
}
}
if (c== n)
printf("%d isn't present in the array \n", search);
return 0;
}
Output:
Program 23: write a c program to sorted list of integers using binary search method.
#include <stdio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
first = 0;
last = n - 1;
middle = (first+last)/2;
return 0;
}
Output:
Program 24: write a c program that implement the bubble sort method to sort a given list of integers in
ascending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[25],n,i,j,k,temp;
clrscr();
printf("Enter the number of elements in array:");
scanf("%d",&n);
printf("Enter the elements into array:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=j+1; j<n; j++)
{
if(a[i]>a[j])
{
temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("Elements after sorting:");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
Output:
Program 25: write a c program that implements the selection sort method to sort a given list of
integers indescending order.
#include <stdio.h>
int main()
{
int Array[50], i, j, temp, Size;
}
}
printf("\n **** Array of Elemenst in Descending Order are : ****\n");
for (i = 0; i < Size; i++)
{
printf("%d\t", Array[i]);
}
return 0;
}
Output:
Program 26: write a c program that implements the insertion sort method to sort a given list of integers in
ascending order.
#include <stdio.h>
int main()
{
int n, array[100], c, d, t;
return 0;
}
Output: