Showing posts with label C Lab Programs. Show all posts
Showing posts with label C Lab Programs. Show all posts

Friday, March 30, 2018

Write A Program Using File Handling To Copy The Contents Of One File To Another file.

/* Copy the content of one file to another file */

 

#include< stdio.h>

#include< stdlib.h>

#include< conio.h>

 int main()

 {

 FILE *fptr1;

 FILE *fptr2;

 char ch;

     // Open one file for reading

fptr1 = fopen("Quote.c", "w");

 

fputs("Knowledge helps you to reach destination provided you know what your       destination is ",fptr1);

 fclose(fptr1);

 

 /*Open another file for writing */

 fptr1 = fopen("Quote.c","w");

 fptr2=fopen("new.c","r");

 while(!feof(fptr1))

 {

  ch=getc(fptr1);

  putc(ch,fptr2);

  }

  while(ch!=EOF);

  fclose(fptr1);

  fclose(fptr2);

 getch();

 }

 

Output:-

Copy File

C Program for String manipulation.

/*C program to count upper case, lower case and special characters in a string.*/
#include<stdio.h>
int main()
{
char text[100];
int i;
int countL,countU,countS;
printf("Enter any string: ");
gets(text);
/* here, we are printing string using printf
//without using loop */
printf("Entered string is: %s\n",text);
/* count lower case, upper case and special characters
//assign 0 to counter variables */
countL=countU=countS=0;
for(i=0;text[i]!='\0';i++)
{
/* check for alphabet */
if((text[i]>='A' && text[i]<='Z') || (text[i]>='a' && text[i]<='z'))
{
if((text[i]>='A' && text[i]<='Z'))
{
/* it is upper case alphabet */
countU++;
}
else
{
/* it is lower case character */
countL++;
}
}
else
{
/* character is not an alphabet */
countS++; /*it is special character */
}
}
/*convert into upper case */
for(i=0;text[i]!='\0';i++)
{
/*check character is alphabet or not */
if((text[i]>='A' && text[i]<='Z')||(text[i]>='a' && text[i]<='z'))
{
/* check for upper case character */
if(text[i]>='A' && text[i]<='Z')
text[i]=text[i]+0x20;
else
text[i]=text[i]-0x20;
}
}
printf("String is: ");
for(i=0;text[i]!='\0';i++)
{
printf("%c",text[i]);
}
printf("\n");
printf("VOWEL Characters are: ");
for(i=0;text[i]!='\0';i++)
{
if(text[i]=='A' || text[i]=='a' || text[i]=='E' || text[i]=='e' || text[i]=='I' || text[i]=='i' || text[i]=='O' || text[i]=='o' || text[i]=='U' || text[i]=='u')
printf("%c",text[i]);
}
printf("\n");
printf("CONSONANT Characters are: ");
for(i=0;text[i]!='\0';i++)
{
if(!(text[i]=='A' || text[i]=='a' || text[i]=='E' || text[i]=='e' || text[i]=='I' || text[i]=='i' || text[i]=='O' || text[i]=='o' || text[i]=='U' || text[i]=='u'))
printf("%c",text[i]);
}
printf("String after toggle case: %s\n",text);
printf("Upper case characters: %d\n",countU);
printf("Lower case characters: %d\n",countL);
printf("Special characters: %d\n",countS);
return 0;
}

Output:-

String manipulation

Thursday, March 29, 2018

Program for Binary Search.

/*Program for Binary Search*/

#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+1);

break;

}

else

last = middle - 1;

middle = (first + last)/2;

}

if (first > last)

printf("Not found! %d is not present in the list.\n", search);

return 0;

}

Output:-

Binary Search

Tuesday, March 27, 2018

Program for Linear search c program.

/* Program for Linear search c program */

#include <stdio.h>

int main()

{

int array[100], search, c, n;

printf("Enter the 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 the number to search\n");

scanf("%d", &search);

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

{

if (array[c] == search)     /* if required element found */

{

printf("%d is present at location %d.\n", search, c+1);

break;

}

}

if (c == n)

printf("%d is not present in array.\n", search);

return 0;

}

Output:-

Linear Search

  C Program For Bubble Sort.

/* Program for Bubble sort */

#include <stdio.h>

int main()

{

int array[100], n, c, d, swap;

 

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 = 0 ; c < ( n - 1 ); c++)

{

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

{

if (array[d] > array[d+1]) /* For decreasing order use < */

{

swap       = array[d];

array[d]   = array[d+1];

array[d+1] = swap;

}

}

}

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

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

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

return 0;

}

Output:-

Bubble Sort



Monday, March 26, 2018

Program for insertion sort ascending order.

/* Program for insertion sort ascending order */

#include <stdio.h>

int main()

{

int n, array[1000], 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:-

sort ascending order

Friday, March 23, 2018

C Program To Matrix Multiplication.

/* Matrix Multiplication */

#include <stdio.h>

int main()

{

int m, n, p, q, c, d, k, sum = 0;

int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");

scanf("%d%d", &m, &n);

printf("Enter the elements of first matrix\n");

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

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

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

printf("Enter the number of rows and columns of second matrix\n");

scanf("%d%d", &p, &q);

if ( n != p )

printf("Matrices with entered orders can't be multiplied with each other.\n");

else

{

printf("Enter the elements of second matrix\n");

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

for ( d = 0 ; d < q ; d++ )

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

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

{

for ( d = 0 ; d < q ; d++ )

{

for ( k = 0 ; k < p ; k++ )

{

sum = sum + first[c][k]*second[k][d];

}

multiply[c][d] = sum;

sum = 0;

}

}

printf("Product of entered matrices:-\n");

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

{

for ( d = 0 ; d < q ; d++ )

printf("%d\t", multiply[c][d]);

printf("\n");

}

}

return 0;

}

Output:-

Matrix Multiplication

Life Articles And News:

Life Articles And News: