➤ There are three types of functions in c language.
1) User defined functions.
2) Standard library functions.
3) System Calls
Q.1: Write a program to find the greatest common divisor(GCD) of two numbers and factorial of the greatest common divisor.
Ans. #include <stdio.h>
#include <conio.h>
int main()
{
int x=0, y=12, z=18, a=1, b=0;
clrscr();
while((x=y%z)!=0)
{
y=z;
z=x;
}
printf("These are the current valus bellow:>\n");
printf("value of x:> %d\n", x);
printf("value of z:> %d\n", z);
printf("value of a:> %d\n", a);
printf("value of b:> %d\n", b);
printf("value of y:> %d\n", y);
while(z>0)
{
a=a*z;
z--;
}
printf(" %d\n", a);
getch();
return 0;
}
// the final answer you get i.e 720 .
/* Behind the programe
1=1*6
6=6*5
30=30*4
120=120*3
360=360*2
720=720*1 */
Q.2: Introduction to user defined function .
Ans. #include <stdio.h>
#include <conio.h>
int main()
{
clrscr();
printf("Hello World\n");
user();
printf("I'm the root. \n");
getch();
return 0;
}
user()
{
printf("It is user defined function.\n");
}
Q.3: Write a program for sum of two integers by using user defined function.
Ans. #include <stdio.h>
#include <conio.h>
void main()
{
int a, b;
printf("Enter two nos: ");
scanf("%d %d ", &a, &b);
sum(a, b);
}
sum( a, b) // It is call by value.
{
int c;
c=a+b;
printf("sum of two numbers: %d\n", c);
getch();
}
#include <conio.h>
void main()
{
add();
getch();
}
add()
{
int a, b, c;
clrscr();
printf("Enter two nos:\n");
scanf("%d %d", &a, &b);
c=a+b;
printf("aditon of two noumbers: %d", c);
}
#include <conio.h>
void main()
{
int a, b;
printf("Enter two nos: ");
scanf("%d %d ", &a, &b);
sum(a, b);
}
sum( a, b) // It is call by value.
{
int c;
c=a+b;
printf("sum of two numbers: %d\n", c);
getch();
}
Q.4: Write a program for sum of two integers by using user defined function.
Ans. #include <stdio.h>#include <conio.h>
void main()
{
add();
getch();
}
add()
{
int a, b, c;
clrscr();
printf("Enter two nos:\n");
scanf("%d %d", &a, &b);
c=a+b;
printf("aditon of two noumbers: %d", c);
}
Q.5: Write a program to find the largest & the smallest element from a 2D array.
Ans.
#include <stdio.h>
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3], i, j, l, s;
int big(int [3][3]);
int small(int [3][3]);
printf("\nEnter nos to the array:>");
for(i=0; i<3; i++)
for(j=0; j<3; j++)
scanf("%d", &a[i][j]);
l=big(a);
s=small(a);
printf("\nLargest element is = %d ", l);
printf("\nSmallest element is = %d", s);
getch();
}
int big(int d[3][3])
{
int i, j, l;
l=d[0][0];
for(i=0; i<3; i++)
for(j=0; j<3; j++)
if(d[i][j]>l)
{
l=d[i][j];
}
return(l);
}
int small(int e[3][3])
{
int i, j, s;
s=e[0][0];
for(i=0; i<3; i++)
for(j=0; j<3; j++)
if(e[i][j]<s)
{
s=e[i][j];
}
return(s);
}
Q.6: Write a program to print the even numbers from random input.
Ans.
#include <stdio.h>
#include <conio.h>
void main()
{
int a[3][3], i, j;
void display(int a[3][3]);
clrscr();
printf("\nEnter the elements to the array:>");
for(i=0; i<3; i++)
for(j=0; j<3; j++)
scanf("%d", &a[i][j]);
display(a);
getch();
}
void display(int b[3][3])
{
int i, j;
for ( i = 0; i < 3; i++)
for ( j = 0; j < 3; j++)
if (b[i][j] % 2 == 0)
{
printf("\nEven no is : %d", b[i][j]);
}
}
Using of string manipulate function
- It is a Standard library function.
- Which is comes under #include<string.h>
Q.1: Using of strlen()
- It use for represent the length of string.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[20];
int n;
clrscr();
printf("Enter a string:>");
gets(a);
n=strlen(a);
printf("\nThe length of string is:> %d", n);
getch();
}
Q.2: Using of strcpy()
- It use for copy a string to a given location.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[20], b[20];
int n;
clrscr();
printf("Enter a string:>");
gets(a);
strcpy(b,a);
printf("\nThe string is in location b:> %s", b);
getch();
}
#include <conio.h>
#include <string.h>
void main()
{
char a[20], b[20];
int n;
clrscr();
printf("Enter a string:>");
gets(a);
strcpy(b,a);
printf("\nThe string is in location b:> %s", b);
getch();
}
Q.3: Using of strcmp()
- It is use for compare two strings.
- To compare that two strings are same or not.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[20], b[20];
int con;
clrscr();
printf("Enter the first string:>");
gets(a);
printf("Enter the second string:>");
gets(b);
con=strcmp(a,b);
if(con==0)
printf("string are equal\n");
else
printf("string are not equal\n");
getch();
}
Q.4: Using of strcat()
- It use for coordination of two string.
Program:
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char a[20], b[20];
int con;
clrscr();
printf("Enter the first string:>");
gets(a);
printf("Enter the second string:>");
gets(b);
strcat(a,b);
puts(a);
getch();
}
0 Comments