Sunteți pe pagina 1din 10

C Programming (New)

1. Write a function to check leap year which takes the year as an argument and checks
whether the year is leap year or not and displays an appropriate message on the screen.

#include<stdio.h>
int leap(int );
int main()
{
int year;
printf("Enter 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;
}

OUTPUT
Enter year: 2016
2016 is leap year
2. Write a function that accepts a number n as input and returns the average of the sum of
numbers from 1 to n.

int avg(int n)
{
for(i=1;i<=n;i++)
{
sum=sum+i;
}
a=sum/(n);
return (a);
}

3. Write a program to transpose a 3x3 matrix.

#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");
}
}

OUTPUT
Enter the elements of matrix
1

Transpose of entered matrix:1

4. Write a program to check whether 3 is present in arr[]={1,2,3,4,5,6,7,8}

#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");
}

OUTPUT
Enter the element you want to search

3
Present

5. Write a program to copy one string to another string without using strcpy() function?

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

OUTPUT
Enter string s1: Hello
String s2: Hello

6. Write a program to find the largest of n numbers using array and display its position?

#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 its value is %d.\n", location,
maximum);

return 0;
}

OUTPUT
Enter the number of elements in an array
5
Enter 5 integers
4

Maximum element is present at location 2 and its value is 8.

7. Write a program to reverse a string.

#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]);
}
}

OUTPUT
Enter the string
world
Reverse is: dlrow

8. Write a program to read and display an integer array and allocate space dynamically for
the array.

#include <stdio.h>
#include <stdlib.h>
main()
{
int i, n;
int *arr;
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]);
}
}

OUTPUT
Enter the number of elements 3
Enter the value 0 of the array: 4
Enter the value 1 of the array: 6
Enter the value 2 of the array: 7
The array contains
4
6

9. Write a program to copy one file to another one character at a time.

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

S-ar putea să vă placă și