0% found this document useful (0 votes)
53 views10 pages

C Programs

The document contains several C programming code samples that demonstrate functions like checking for a leap year, calculating an average, matrix transpose, linear search, string copy, finding the maximum element in an array, reversing a string, dynamic memory allocation, and copying files.

Uploaded by

DurbaGhosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views10 pages

C Programs

The document contains several C programming code samples that demonstrate functions like checking for a leap year, calculating an average, matrix transpose, linear search, string copy, finding the maximum element in an array, reversing a string, dynamic memory allocation, and copying files.

Uploaded by

DurbaGhosh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C Programming Samples

#include<stdio.h>
int leap(int );
int main()
{
int year;
printf("Enter any year : ");
scanf("%d", &year);
if(leap(year))
printf("\n%d is leap year",year);
else
printf("\n%d is not leap year",year);
return 0;
}
int leap(int y)
{
if((y%100==0 && y%400==0)||(y%4==0))
return 1;
else
return 0;
}

-------------CHECKED-------------------
#include<stdio.h>

void main()

int n,i;

float a,sum=0;

printf("Enter a number\n");

scanf("%d",&n);

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

sum=sum+i;

a=sum/(n);

printf("Average is %f",a);

---------------------------------------------

-------------CHECKED------------------

#include <stdio.h>

void main()

int c, d, matrix[3][3], transpose[3][3];


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

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

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

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

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

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

transpose[d][c] = matrix[c][d];

printf("Transpose of entered matrix :-\n");

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

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

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

printf("\n");

---------------------------------------------

----------------CHECKED-----------------

#include<stdio.h>

void main()

int i,m,flag=0;

int arr[]={1,2,3,4,5,6,7,8};
printf("Enter the element you want to search \n");

scanf("%d", &m);

for (i=0; i<8; i++)

if(arr[i]==m)

flag=1;

break;

if(flag==0)

printf("Not present");

else

printf("Present");

--------------------------------------------

---------------------CHECKED----------------

#include <stdio.h>

int main()

char s1[100], s2[100], i;


printf("Enter string s1: ");

scanf("%s",s1);

for(i=0; s1[i]!='\0'; ++i)

s2[i]=s1[i];

s2[i]='\0';

printf("String s2: %s",s2);

return 0;

-----------------------------------------------------

----------------------CHECKED-------------------------

#include <stdio.h>

int main()

int array[100], maximum, size, c, location = 1;

printf("Enter the number of elements in array\n");

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

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

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

maximum = array[0];

for (c = 1; c < size; c++)

if (array[c] > maximum)

maximum = array[c];

location = c+1;

printf("Maximum element is present at location %d and it's value is %d.\n",


location, maximum);

return 0;

------------------------------------------------

--------------------CHECKED-----------------

#include<stdio.h>
void main()

int i,count=0;

char ch[20];

printf("Enter the string\n");

gets(ch);

for(i=0;ch[i]!='\0';i++)

count++;

printf("Reverse is:");

for(i=count-1;i>=0;i--)

printf("%c",ch[i]);

-------------------------------------------

-------------CHECKED-------------------

#include <stdio.h>

#include <conio.h>

#include <stdlib.h>
main()

int i, n;

int *arr;

clrscr();

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

scanf ("%d", &n);

arr= (int*)malloc(n * sizeof(int));

if(arr ==NULL)

printf("\n Memory Allocation Failed");

exit(0);

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

printf("\n Enter the value %d of the array: ",i );

scanf("%d", &arr[i]);

printf("\n The array contains \n");

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

printf(" \n \t%d \t", arr[i]);


/*another way is to write *(arr+i) */

return 0;

---------------------------------------------

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void main() {

FILE *fp1, *fp2;

char ch;

clrscr();

fp1 = fopen("Sample.txt", "r");

fp2 = fopen("Output.txt", "w");

while (1) {

ch = fgetc(fp1);

if (ch == EOF)

break;
else

putc(ch, fp2);

printf("File copied Successfully!");

fclose(fp1);

fclose(fp2);

You might also like