0% found this document useful (0 votes)
56 views

C Programming New

The document contains 9 coding problems and their solutions in C programming. The problems include: 1) checking if a year is a leap year, 2) calculating the average of numbers from 1 to n, 3) transposing a 3x3 matrix, 4) checking if an element is present in an array, 5) copying one string to another without strcpy(), 6) finding the largest number in an array, 7) reversing a string, 8) dynamically allocating space for an integer array, and 9) copying one file to another character by character. For each problem, the code provided solves the problem and includes sample input/output.

Uploaded by

Debasish Ghosh
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)
56 views

C Programming New

The document contains 9 coding problems and their solutions in C programming. The problems include: 1) checking if a year is a leap year, 2) calculating the average of numbers from 1 to n, 3) transposing a 3x3 matrix, 4) checking if an element is present in an array, 5) copying one string to another without strcpy(), 6) finding the largest number in an array, 7) reversing a string, 8) dynamically allocating space for an integer array, and 9) copying one file to another character by character. For each problem, the code provided solves the problem and includes sample input/output.

Uploaded by

Debasish Ghosh
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 (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);

You might also like