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

Write A Function Called Leap That Receives The Year As A Parameter and Returns 0 If It Is A Leap Year, Otherwise It Returns 1

The document contains 10 code snippets that define C functions to perform various tasks related to functions and arrays. These include functions to check for a leap year, calculate the sum of digits in a number, check if a number is prime, calculate factorials, powers, Fibonacci series, and sorting arrays in ascending and descending order.

Uploaded by

122AYUSH RANA
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)
47 views

Write A Function Called Leap That Receives The Year As A Parameter and Returns 0 If It Is A Leap Year, Otherwise It Returns 1

The document contains 10 code snippets that define C functions to perform various tasks related to functions and arrays. These include functions to check for a leap year, calculate the sum of digits in a number, check if a number is prime, calculate factorials, powers, Fibonacci series, and sorting arrays in ascending and descending order.

Uploaded by

122AYUSH RANA
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/ 9

1.

Write a function called leap() that receives the year as a parameter and returns 0 if it is a leap
year, otherwise it returns 1.

#include<stdio.h>
int leap(int); // function prototype
int main()
{
int year;

printf("Enter a year to find leap year or not\n");


scanf("%d", &year);

//function call leap(year);


if( leap(year) )
{
printf("%d is leap year\n", year);
}
else
{
printf("%d is not leap year\n", year);
}

return 0;
}

//function definition
int leap(int year)
{
if(year % 100 == 0)
{
if(year % 400 == 0)
return 1;
else
return 0;
}
else
{
if(year % 4 == 0)
return 1;
else
return 0;
}
}

2. Write a function to calculate sum of digits in the number N.

#include<stdio.h>
void sum(int);
int main()
{
int a;
printf("enter a number=");
scanf("%d",&a);
sum(a); //function call
return 0;
}
void sum(int n)
{
int sum=0;

while(n!=0)
{
int r=n%10;
sum=sum+r;
n=n/10;
}
printf("Sum of digits: %d",sum);
return;
}

3. Write a function to check whether a given number is prime number or not

#include <stdio.h>
int isprime(int);
int main()
{
int x;
printf("enter no.");
scanf("%d", &x);
if (isprime(x) == 0)
{
printf("prime");
}
else
{
printf("not prime");
}
return 0;
}
int isprime(int n)
{
int i, flag;
for (i = 2; i <= n / 2; i++)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
if(flag == 1)
{
return 1;
}
else
{
return 0;
}
}
4. Write a function to display return factorial of integer n.

#include <stdio.h>
int isarmstrong(int);
int main()
{
int x;
printf("enter value of x:");
scanf("%d",&x);
if(isarmstrong(x)==0)
{
printf("\n armstrong number");
}
else
{
printf("\n not armstrong number");
}
return 0;
}
int isarmstrong(int n)
{
int n1,sum=0,r,digit=0,temp=1,i;
n1=n;
while(n1!=0)
{
n1=n1/10;
digit=digit+1;
}
n1=n;
while(n1!=0)
{
r=n1%10;
temp=1;
for(i=0;i<digit;i++)
{
temp=temp*r;
}
sum=sum+temp;
n1=n1/10;
}
if(n==sum)
{
return 0;
}
else
{
return 1;
}
}

5. Write a function to display return factorial of integer n.


#include <stdio.h>
int factorial(int);
int main()
{
int num;
printf("enter number:");
scanf("%d",&num);
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}

6. Write a function to calculate a power of

#include <stdio.h>
void power(int);
int main()
{
int x;

power(x);
return 0;
}
void power(int x)
{
int i,m,b,R;
printf("Enter base number: ");
scanf("%d", &m);
printf("Enter power:");
scanf("%d",&b);
R=m;
for(i=1;i<b;i++)
{
R=R*m;
}
printf("Power is %d",R);
return;
}

7. Write a modular program that will perform the following tasks:


A) Read two integer arrays with unsorted elements

B) Sort them in ascending order

C) Merge the sorted arrays

D) Print the sorted list

#include <stdio.h>
void input(int[], int);
void print(int[], int);
void asort(int[], int);
int main()
{
printf("\n 1st array");
int m[10];
input(m, 10);
print(m, 10);
printf("\n 2nd array");
int n[10];
input(n, 10);
print(n, 10);
printf("sorting of 1st array");
asort(m, 10);
printf("\n Result after sorting \n");
print(m, 10);
printf("sorting of 1st array");
asort(n, 10);
printf("\n Result after sorting \n");
print(n, 10);

return 0;
}
void input(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("\n enter vaue fot element %d ", i);
scanf("%d", &a[i]);
}
return;
}
void print(int b[], int n)
{
int i;
for (i = 0; i < n; i++)
{
printf("\t %d ", b[i]);
}
return;
}
void asort(int a[], int n)
{
int i, j, temp;
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return ;
}

8. Write a function that return a power of a number


e.g pow(3,2)= 9

#include <stdio.h>
int power(int , int);
int main()
{
int m, n, R;
printf("Enter base number: ");
scanf("%d", &m);
printf("Enter power number(positive integer): ");
scanf("%d", &n);
R = power(m, n);
printf("%d^%d = %d", m, n, R);
return 0;
}

int power(int x, int y) {


if (y != 0)
return (x * power(x, y - 1));
else
return 1;
}

9. Write a function to generate Fibonacci series up to N.


The Fibonacci sequence is a sequence where the next term is the sum of the previous two terms. The
first two terms of the Fibonacci sequence are 0 followed by 1.

#include<stdio.h>
void Fib(int);
int main()
{
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
Fib(n);
return 0;
}
void Fib(int n)
{
int a=0,b=1,c;
c=a+b;

printf("Fibonacci Series: %d, %d, ",a,b);

while(c<=n)
{
printf("%d,",c);
a=b;
b=c;
c=a+b;
}
}

10. Write functions to perform sorting in ascending and descending order in 2 dimensional array.

#include<stdio.h>
void input(int m,int n,int x[m][n]);
void print(int m,int n,int x[m][n]);
void asc(int m,int n,int x[m][n]);
void des(int m,int n,int x[m][n]);

int main()
{
int a[3][3];
input(3,3,a);
print(3,3,a);
printf("\nRow wise\n");
asc(3,3,a);
printf("\nColumn wise\n");
des(3,3,a);
return 0;
}
void input(int m,int n,int x[m][n])
{
int i,j;
printf("Enter array:");
for ( i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d",&x[i][j]);
}
}
return;
}
void print(int m,int n,int x[m][n])
{
int i,j;
for ( i = 0; i < m; i++)
{
for ( j = 0; j < n; j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
return;
}
void asc(int m,int n,int x[m][n])
{
int i=0,j=0,k,temp[i][j];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
temp[i][j]=x[i][j];
for(k=j+1;k<n;k++)
{
if(x[i][j]>x[i][k])
{
temp[i][j]=x[i][j];
x[i][j]=x[i][k];
x[i][k]=temp[i][j];
}
}
}
}
for ( i = 0; i < m; i++)
{
for ( j = 0; j < n; j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
return;
}

void des(int m,int n,int x[m][n])


{
int i=0,j=0,k,temp[i][j];
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
temp[j][i]=x[j][i];
for(k=j+1;k<n;k++)
{
if(x[j][i]>x[k][i])
{
temp[j][i]=x[j][i];
x[j][i]=x[k][i];
x[k][i]=temp[j][i];
}
}
}
}
for ( i = 0; i < m; i++)
{
for ( j = 0; j < n; j++)
{
printf("%d\t",x[i][j]);
}
printf("\n");
}
return;
}

You might also like