0% found this document useful (0 votes)
23 views14 pages

16 26 Programs

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 14

Program 16: Write a C program to print contents of file.

#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()

FILE *fp1, *fp2;

char ch;

fp1=fopen("sourcefile.c","r");

if(fp1 ==NULL)

puts("file does not exist");

exit(1);

fp2=fopen("targetfile.c","w");

if(fp2 ==NULL)

puts("file does not exist");

fclose(fp1);

exit(1);

while((ch=fgetc(fp1))!=EOF)

ch=toupper(ch);

fputc(ch,fp2);

printf("\n Filesuccessfully copied…");

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>

int power(int n1, int n2);

int power(int base, int a)

if(a != 0)

return(base* power(base, a- 1));

else

return 1;

int main() {

int base, a, result;

printf("Enter base number: ");

scanf("%d", &base);

printf("Enter power number(positive integer): ");

scanf("%d", &a);

result = power(base, a);

printf("%d^%d = %d", base, a, result);

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;

for(i = 1;i <= x; i++)

f = f * i;

return(f);

int main()

int n, a, b;
clrscr();
printf("Enter any number\n");
scanf("%d", &n);

a = recfactorial(n);

printf("The factorial of a given number using recursion is %d\n", a);

b = nonrecfactorial(n);

printf("The factorial of a given number using nonrecursion is %d ", b);

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>

int recgcd(int x, int y)

If (y == 0)

return(x);

else

return(recgcd(y, x % y));

int nonrecgcd(int x, int 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");

scanf("%d%d", &a, &b);

c = recgcd(a, b);

printf("The gcd of two numbers using recursion is %d\n", c);

d = nonrecgcd(a, b);

printf("The gcd of two numbers using nonrecursion is %d", d);

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];

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++)


scanf("%d", &array[c]);

printf("Enter value to find\n");


scanf("%d", &search);

first = 0;
last = n - 1;
middle = (first+last)/2;

while (first <= last)


{
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search)
{
printf("%d found at location %d \n", search, middle);
break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Not found! %d isn't present in the list.\n", search);

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 Please Enter the Number of elements in an array : ");


scanf("%d", &Size);

printf("\nPlease Enter %d elements of an Array \n", Size);


for (i = 0; i < Size; i++)
{
scanf("%d", &Array[i]);
}
for (i = 0; i < Size; i++)
{
for (j = i + 1; j < Size; j++)
{
if(Array[i] < Array[j])
{
temp = Array[i];
Array[i] = Array[j];
Array[j] =temp;
}

}
}
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;

printf("Enter number of elements\n");


scanf("%d", &n);

printf("Enter %d integers\n", n);

for (c = 0; c < n; c++) {


scanf("%d", &array[c]);
}

for (c = 1 ; c <= n - 1; c++)


{
d = c;

while ( d > 0 && array[d] < array[d-1]) {


t= array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {


printf("%d\n", array[c]);
}

return 0;
}

Output:

You might also like