0% found this document useful (0 votes)
38 views55 pages

C Programs for Basic Functions

Uploaded by

roshanrawat2323
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)
38 views55 pages

C Programs for Basic Functions

Uploaded by

roshanrawat2323
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

1.

Write a program to calculate the factorial of a given


number using function.
#include <stdio.h>
int fact(int value);
int main()
{
int value;
printf("enter a number:");
scanf("%d",&value);
printf("The factorial of %d is %d", value, fact(value));
return 0;
}
int fact(int value)
{
int i,fact;
fact=1;
for(i=1;i<=value;i++)
{
fact=fact*i;
}
return fact;
}

Output:
enter a number:5
The factorial of 5 is 120
2. Write a program to calculate and print the average of
three numbers using function.
#include <stdio.h>
int average(int i,int j,int k);
int main()
{
int i,j,k;
printf("Enter any three numbers");
scanf("%d %d %d",&i,&j,&k);
printf("The average of three number
is:%d",average(i,j,k));
return 0;
}
int average(int i,int j,int k)
{
int sum,average;
sum=i+j+k;
average=sum/3;
return average;
}
Output:
Enter any three numbers 1 2 3
The average of three number is: 2
3. Write a program to find greatest among three numbers
using function.
#include <stdio.h>
int greatest(int i,int j,int k);
int main()
{
int i,j,k;
printf("Enter any three numbers:");
scanf("%d %d %d",&i,&j,&k);
printf("The greatest among these three numbers is:
%d",greatest(i,j,k));
return 0;
}
int greatest(int i,int j,int k)
{
if(i>j&&i>k)
{
return i;
}
else if (j>i&&j>k)
{
return j;
}
else
{
return k;
}
}

Output:
Enter any three numbers: 34 67 23
The greatest among these three numbers is: 67
4. Write a program to print the factorial of numbers from
1 to n using function.
#include <stdio.h>
void fact(int n);
void main( )
{
int n;
n=1;
printf("Enter the terms");
scanf("%d",&n);
fact(n);
}
void fact(int n)
{
int i,fact,value;
fact=1;
value=1;
for(i=1;i<=n;i++)
{
fact=fact*i;
printf("\n The factorial of number of %d is
%d",value,fact);
value++;
}
}
Output:

Enter the terms 5

The factorial of number of 1 is 1


The factorial of number of 2 is 2
The factorial of number of 3 is 6
The factorial of number of 4 is 24
The factorial of number of 5 is 120
5. Write a program in C to print the multiplication table of a
given number using function.
#include <stdio.h>
void mtable( int a);
void main()
{
int i,a,b;
i=1;
printf("Enter a number");
scanf("%d",&a);
mtable (a);
}
void mtable( int a)
{
int i,b;
i=1;
do
{
b=a*i;
printf("\n %d*%d=%d",a,i,b);
i++;
}
while(i<=10);
}
Output:
Enter a number 5

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
6. Write a program to calculate the power of a number using
function.
#include <math.h>
#include <stdio.h>
void power(int base,int exp);
void main()
{
int base, exp, result;
printf("Enter a base number: ");
scanf("%d", &base);
printf("Enter an exponent: ");
scanf("%d", &exp);
power(base,exp);
}
void power(int base,int exp)
{
int result;
result = pow(base, exp);
printf("%d^%d = %d", base, exp, result);
}

Output:
Enter a base number: 2
Enter an exponent: 3
2^3 = 8
7. Write a program to swap the values of two variables
without using a temporary variable.
#include <stdio.h>
void swap(int i,int j);
void main()
{
int i,j;
printf("Enter the value of i:");
scanf("%d",&i);
printf("Enter the value of j:");
scanf("%d",&j);
swap(i,j);
}
void swap(int i,int j)
{
i=i+j;
j=i-j;
i=i-j;
printf("After swaping: i=%d, j=%d",i,j);
}

Output:
Enter the value of i:3
Enter the value of j:4
After swaping: i=4, j=3
8. Write a program to calculate simple interest given principle,
rate, and time using function.

#include <stdio.h>
void interest(int p,int t,int r);
void main()
{
int p,t,r;
printf("Enter the value of principle:");
scanf("%d",&p);
printf("Enter the value of time:");
scanf("%d",&t);
printf("Enter the value of rate:");
scanf("%d",&r);
interest(p,t,r);
}
void interest(int p,int t,int r)
{
int i;
i=p*t*r;
i=i/100;
printf("simple interest=%d",i);
}

Output:
Enter the value of principle:200
Enter the value of time:2
Enter the value of rate:3
simple interest=12
9. Write a program to calculate the sum of all elements in an
array using fuction.

#include <stdio.h>
void sum(int n);
void main()

{
int n;
printf("Enter the number of elements to be stored in the
array :");
scanf("%d", &n);
printf("Input %d elements in the array :\n", n);
sum(n);
}
void sum(int n)
{
int i,sum = 0,a[100];
for (i = 1; i <=n; i++)
{
printf("element - %d : ", i);
scanf("%d", &a[i]);
}
for (i = 0; i <=n; i++)
{
sum += a[i];
}
printf("Sum of all elements stored in the array is : %d\n",
sum);
}

Output:
Enter the number of elements to be stored in the array :4
Input 4 elements in the array :
element - 1 : 2
element - 2 : 3
element - 3 : 2
element - 4 : 3
Sum of all elements stored in the array is : 10
10. Write a program to reverse the elements of an array using
function.
#include <stdio.h>
void reverse(int n);
void main()
{
int n;
printf("Enter the number of elements in array:");
scanf("%d", &n);
printf("Enter the array elements\t");
reverse(n);
}
void reverse(int n)
{
int i, j, a[100], b[100];
for (i = 0; i < n ; i++)
{
scanf("%d", &a[i]);
}

for (i= n - 1, j = 0; i >= 0; i--, j++)


{
b[j] = a[i];
}
for (i = 0; i < n; i++)
{
a[i] = b[i];
}
printf("Reverse array is\t");
for (i= 0; i < n; i++)
{
printf("%d\t", a[i]);
}
}

Output:
Enter the number of elements in array: 5
Enter the array elements 12345
Reverse array is 5 4 3 2 1
11. Write a program to count the number of characters in a
string using function.
#include <stdio.h>
#include <string.h>
void length(char str[100]);
void main()
{
char str[100];
printf("Enter a string: ");
scanf("%s", str);
length(str);
}
void length(char str[100])
{
int i, length;
length = 0;
for (i = 0; str[i] != '\0'; i++)
{
length++;
}
printf("The length of the string is: %d\n", length);
}

Output:
Enter a string: apple
The length of the string is: 5
12.Implement a program to find the sum of digits of a number
using function
#include <stdio.h>
void sum(int n);
void main()
{
int n;
printf("Enter the number ");
scanf("%d", &n);
printf("The number is = %d\n", n);
sum(n);
}
void sum(int n)
{
int sum=0;
while (n!= 0)
{
sum += n % 10;
n= n / 10;
}
printf("Sum: %d\n", sum);
}
Output:
Enter the number 122344
The number is = 122344
Sum: 16
13.Implement a program to find the Fibonacci
series.(1,1,2,3,5,…….up to 10 terms) using function.
#include <stdio.h>
void fibo();
void main()
{
fibo();
}
void fibo()
{
int a=1,b=1,c;
int count=1;
while(count<=10)
{
printf("%d\t",a);
c=a+b;
a=b;
b=c;
count++;
}

Output:
1 1 2 3 5 8 13 21 34 55
14.Write a program to check whether a number is prime or
not using function.
#include <stdio.h>
void prime(int n);
void main()
{
int n;
printf("Enter a number");
scanf("%d",&n);
prime(n);
}
void prime(int n)
{
int c=2;
for(c=2;c<=n-1;c++)
{
if(n%c==0)
{
printf("%d is not prime number\n",n);
break;
}
}
if (c==n)
{
printf("%d is prime number\n",n);
}
}

Output:
Enter a number23
23 is prime number
15.Write a program to check whether a given string is
palindrome or not using function.
#include <stdio.h>
void palindrome(int n);
void main()
{
int n;
printf("Enter an integer: ");
scanf("%d", &n);
palindrome(n);
}
void palindrome(int n)
{
int rev = 0, remainder, original;
original = n;
while (n != 0)
{
remainder = n % 10;
rev= rev* 10 + remainder;
n /= 10;
}
if (original == rev)
{
printf("%d is a palindrome.", original);
}
else
{
printf("%d is not a palindrome.", original);
}
}

Output:
Enter an integer: 34543
34543 is a palindrome.
16.Write a program to count the number of vowels and
consonants in a given string using function.
#include <stdio.h>
void count( char sentence[80]);
int main()
{
char sentence[80];
printf("Enter a sentence:");
scanf("%s",sentence);
count(sentence);
}
void count( char sentence[80])
{
int i, vowels = 0, consonants = 0;

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


{
if ((sentence[i] == 'a' || sentence[i] == 'e' || sentence[i]
== 'i' || sentence[i] == 'o' || sentence[i] == 'u') ||
(sentence[i] == 'A' || sentence[i] == 'E' || sentence[i] ==
'I' || sentence[i] == 'O' || sentence[i] == 'U'))
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
}
printf("No. of vowels in %s = %d\n", sentence, vowels);
printf("No. of consonants in %s = %d\n", sentence,
consonants);
}

Output:
Enter a sentence:computer
No. of vowels in computer = 3
No. of consonants in computer = 5
17.Write a program to sort an array of integers using bubble
sort and function.
#include <stdio.h>
void series(int n,int num[100]);
void main()
{
int i,n,num[100];
printf("Enter the size of an array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter array elements:");
scanf("%d",&num[i]);
}
series(n,num);
}
void series(int n,int num[100])
{
int j,i,temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf(" An array of integers sorted by using bubble sort
are:");
for(i=0;i<n;i++)
{
printf("\t %d",num[i]);
}
}

Output:
Enter the size of an array:5
Enter array elements:3
Enter array elements:5
Enter array elements:6
Enter array elements:1
Enter array elements:2
An array of integers sorted by using bubble sort are: 1
2 3 5 6
18.Write a program to transpose a matrix using function.
#include <stdio.h>
void transpose(int a[10][10], int r, int c, int b[10][10]);
int main()
{
int r,c,b[10][10],a[10][10];
printf("Enter the number of rows and column:");
scanf("%d %d",&r,&c);
printf("Enter the elements of the matrix:\n");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe elements in the matrix are: \n");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
transpose(a,r,c,b);
return 0;
}
void transpose(int a[10][10], int r, int c, int b[10][10])
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
b[j][i]=a[i][j];
}
}
printf("\nAfter transpose the elements are...\n");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
}

Output:
Enter the number of rows and column:2 2
Enter the elements of the matrix:
1
2
3
4

The elements in the matrix are:


12
34

After transpose the elements are...


13
24
19. Implement a program to multiply two matrices using
function.
#include <stdio.h>
void mul(int r1,int r2,int c1,int c2);
int main()
{
int r1,c1,r2,c2;
printf("Enter rows and columns of array 1:");
scanf("%d %d",&r1,&c1);
printf("Enter the rows and columns of array 2:");
scanf("%d %d",&r2,&c2);
mul(r1,r2,c1,c2);
return 0;
}
void mul(int r1,int r2,int c1,int c2)
{
if(c1!=r2)
{
printf("Matrix multiplication is not possible");
}
else
{
int a[r1][c1],b[r2][c2],i,j,k,product[r1][c2];
printf("Enter the details of an array 1:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("Enter value in %d %d index:",i,j);
scanf("%d",&a[i][j]);
}
}
printf("Enter the details of an array 2:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("Enter the value in %d %d index:",i,j);
scanf("%d",&b[i][j]);
}
}
//array multiplication
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
product[i][j]=0;
for(k=0;k<c1;k++)
{
product[i][j]+=a[i][k]*b[k][j];
}
printf("%d\t",product[i][j]);
}
printf("\n");
}
}
}

Output:
Enter rows and columns of array 1:2 2
Enter the rows and columns of array 2:2 2
Enter the details of an array 1:
Enter value in 0 0 index:1
Enter value in 0 1 index:2
Enter value in 1 0 index:3
Enter value in 1 1 index:4
Enter the details of an array 2:
Enter the value in 0 0 index:5
Enter the value in 0 1 index:1
Enter the value in 1 0 index:2
Enter the value in 1 1 index:3
9 7
23 15
20.Implement a program to convert a decimal number to
binary using function.
#include <stdio.h>
void decimaltobinary(int n);
int main()
{
int n;
printf("\nEnter a decimal number:");
scanf("%d",&n);
decimaltobinary(n);
return 0;
}
void decimaltobinary(int n)
{
int d,sum=0,i=1;
while(n!=0)
{
d=n%2;
sum=sum+d*i;
n=n/2;
i*=10;
}
printf("The binary number is %d",sum);
}
Output:
Enter a decimal number:123
The binary number is 1111011
21.Implement a program to convert a binary number to
decimal using function.
#include <stdio.h>
#include <math.h>
void binarytodecimal(int n);
int main()
{
int n;
printf("\nEnter a binary number:");
scanf("%d",&n);
binarytodecimal(n);
return 0;
}
void binarytodecimal(int n)
{
int n1,d,p=0,sum=0;
n1=n;
while(n!=0)
{
d=n%10;
sum=sum+pow(2,p)*d;
n=n/10;
p++;
}
printf("Decimal equivalent of %d=%d",n1,sum);
}
Output:
Enter a binary number:10011001
Decimal equivalent of 10011001=153
22.Write a C program to calculate the area and perimeter of
rectangle.
#include <stdio.h>
void areaandperimeter(int l,int b);
int main()
{
int l,b;
printf("Enter the length of the rectangle:");
scanf("%d",&l);
printf("Enter the breadth of the reactangle:");
scanf("%d",&b);
areaandperimeter(l,b);
return 0;
}
void areaandperimeter(int l,int b)
{
int a,p;
a=l*b;
printf("Area of rectangle is:%d",a);
p=2*(l+b);
printf("\nperimeter of rectangle is:%d",p);
}
Output:
Enter the length of the rectangle:12
Enter the breadth of the reactangle:6
Area of rectangle is:72
perimeter of rectangle is:36
23.Write a C program to find the LCM (Least Common
Multiple) of two numbers using function.
#include <stdio.h>
void LCM(int n1,int n2);
int main()
{
int n1,n2;
printf("Enter two positive numbers:");
scanf("%d %d",&n1,&n2);
LCM(n1,n2);
return 0;
}
void LCM(int n1,int n2)
{
int max;
max=(n1>n2) ? n1:n2;
while(1)
{
if ((max % n1==0)&&(max % n2 ==0))
{
printf("The LCM of %d and %d is %d",n1,n2,max);
break;
}
++max;
}
}
Output:
Enter two positive numbers: 2 4
The LCM of 2 and 4 is 4
24.Write a C program to check whether a given number is
Armstrong or not.
#include <stdio.h>
void armstrong(int num);
int main()
{
int num;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
armstrong(num);
return 0;
}
void armstrong(int num)
{
int originalNum, remainder, result = 0;
originalNum = num;
while (originalNum != 0)
{
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
{
printf("%d is an Armstrong number.", num);
}
else
{
printf("%d is not an Armstrong number.", num);
}
}

Output:
Enter a three-digit integer: 153
153 is an Armstrong number.
25.Implement a program to calculate the sum of the
series:1+1/2+1/3+…+1/n using function.
#include<stdio.h>
void series(int num);
void main()
{
int num;
printf("Input any number: ");
scanf("%d", &num);
series(num);
}
void series(int num)
{
int i, sum = 0;
printf("1 + ");
for(i = 2; i <= num - 1; i++)
{
printf(" 1/%d +", i);
}
for(i = 1; i <= num; i++)
{
sum = sum + i;
}
printf(" 1/%d", num);
printf("\nSum = 1/%d", sum + 1/num);
}
Output:
Input any number: 3
1 + 1/2 + 1/3
Sum = 1/6
26.Convert number of days into year , month , day , in C
programming.
#include<stdio.h>
void afterconverting(int day);
void main()
{
int day;
printf("Enter days:");
scanf("%d",&day);
afterconverting(day);
}
void afterconverting(int day)
{
int years,months,days,d1;
years=day/365;
d1=day%365;
months=d1/30;
days=d1%30;
printf("Years=%d Months=%d
Days=%d",years,months,days);
}

Output:
Enter days:439
Years=1 Months=2 Days=14
27.Check whether a year is leap year or not in C
programming.
#include <stdio.h>
void leapyear(int year);
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
leapyear(year);
}
void leapyear(int year)
{
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
else
{
printf("%d is not a leap year.", year);
}

Output:
Enter a year: 2008
2008 is a leap year.
28.Check whether a character is an alphabet or not using C
programming.
#include <stdio.h>
void alpha(char c);
void main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
alpha(c);
}
void alpha(char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
printf("%c is an alphabet.", c);
}
else
{
printf("%c is not an alphabet.", c);
}
}

Output:
Enter a character: Computer
C is an alphabet.
29.Write a program to input 100 students marks and arrange
them in ascending order and print them.
#include <stdio.h>
void marks(int n);
void main()
{
int n;
printf("Enter the number of the students:");
scanf("%d",&n);
marks(n);
}
void marks(int n)
{
int i,j,num[100],t;
for (i=0;i<n;i++)
{
printf("Enter the marks of the students:");
scanf("%d",&num[i]);
}
for(i=0;i<n-1;i++)
{
for (j=i+1;j<n;j++)
{
if (num[i]>num[j])
{
t=num[i];
num[i]=num[j];
num[j]=t;
}
}
}
printf("Entered marks of the students are in ascending
order:\n");
for(i=0;i<n;i++)
{
printf("%d\t",num[i]);
}
}

Output:
Enter the number of the students:10
Enter the marks of the students:78
Enter the marks of the students:98
Enter the marks of the students:77
Enter the marks of the students:88
Enter the marks of the students:87
Enter the marks of the students:78
Enter the marks of the students:89
Enter the marks of the students:86
Enter the marks of the students:79
Enter the marks of the students:97
Entered marks of the students are in ascending order:
77 78 78 79 86 87 88 89 97 98
30.WAP to input 100 student’s marks and display highest
marks.
#include <stdio.h>
void marks(int n);
void main()
{
int n;
printf("Enter the number of the students:");
scanf("%d",&n);
marks(n);
}
void marks(int n)
{
int i,num[100],max;
for (i=0;i<n;i++)
{
printf("Enter the marks of the students:");
scanf("%d",&num[i]);
}
max=num[0];
for(i=1;i<n;i++)
{
if(num[i]>max)
{
max=num[i];
}
}
printf("\nHighest mark among these students
is:%d",max);10
}

Output:
Enter the number of the students: 12
Enter the marks of the students: 67
Enter the marks of the students: 87
Enter the marks of the students: 77
Enter the marks of the students: 89
Enter the marks of the students: 87
Enter the marks of the students: 88
Enter the marks of the students: 96
Enter the marks of the students: 84
Enter the marks of the students: 79
Enter the marks of the students: 85
Enter the marks of the students: 78
Enter the marks of the students: 97

Highest mark among these students is: 97

**The End**

You might also like