You are on page 1of 7

Programming in C language – B.A.

1st Year (Computer Application)


Unit III Function with no argument and no return value
value
Library Function Function with argument and no return value
Function returnhhhhvaluevaluevaluereturnvareturnvas
User Defined Function Function with argument and return value

Categories of User Defined Function iii)Function with Arguments and Return Values

i)Function with No Arguments and No Return Example3-


Values #include<stdio.h>
Example1 int largest(int x,int y)
#include<stdio.h> {
void main() int large;
{ if(a>b)
hello(); // call hello() function {
} large=x;
}
// function definition of hello() else
hello() {
{ large=y;
printf(“Hello to the world of functions”); }
} return (large);
}
Output- Hello to the World of functions void main()
+++++++++++++++++++++++++++++++ {
ii)Function with Arguments and No Return int a,b,result;
Values printf("Enter two values:");
scanf("%d%d",&a,&b);
Example2- result=largest(a,b);
#include<stdio.h> printf("Largest Value is:%d",result);
void largest(int a,int b) }
{
if(a>b) Input- Enter two values: 10 30
{ Output- Largest Value is: 30
printf("A is larger"); ++++++++++++++++++++++++++++++++++
} Example4- Program to calculate sum using
else function
{
printf("B is larger"); #include<stdio.h>
} void main()
} {
void main() int sum(int,int);//Function Declaration
{ int a=10, b=20, c;
int p, q; c=sum(a,b); //Function Calling
printf("Enter two values:"); printf("%d",c);
scanf("%d%d",&p,&q); }
largest(p,q); int sum(int x, int y) //Function Definition
} {
return(x+y);
Input- Enter two values:10 30 }
Output- B is larger Output-30

Prof. Gaurav Gupta


Programming in C language – B.A. 1st Year (Computer Application)
Example5- Program to calculate square using Example6- Program to swap (exchange) number
function using function

#include<stdio.h> #include<stdio.h>
void main() #include<conio.h>
{ void main()
int square(int); // Function Declaration {
int num,result; void swap(int a,int b); // function declaration
printf("Enter a number"); int a,b;
scanf("%d",&num); printf("enter two values\n");
result=square(num); //Function Calling scanf("%d%d",&a,&b);
printf("Square is:%d",result); printf("entered values are a=%d and b=%d",a,b);
} swap(a,b); // function calling
int square(int x) //Function Definition getch();
{ }
int j; void swap(int x, int y) // function definition
j=x*x; {
return j; int z;
} z=x,
x=y;
Input- Enter a number: 5 y=z;
Output- square is: 25 printf("\n exchanged values are a=%d b=
++++++++++++++++++++++++++++++ %d",x,y);
Recursive Function-
Que1 A recursive function for calculating the }
factorial of an integer.
#include<stdio.h> Input- Enter two values: 10
int fact(int); 30
void main() Output- entered values are a=10 and b=30
{ exchanged values are a=30 b=10
int x,y; ++++++++++++++++++++++++++++++
printf("Enter number:");
scanf("%d",&x);
y=fact(x);
printf("%d",y);
}
int fact(int n)
{
if(n==0)
return (1);
else
return(n*fact(n-1));
}

Input- Enter number:5


Output- Factorial is:120

Prof. Gaurav Gupta


Programming in C language – B.A. 1st Year (Computer Application)

Array

One Dimensional Array Two Dimensional Array Multi Dimensional Array

One Dimensional Array Example2-


#include<stdio.h>
Example1- एक एरे मे किस प्रकार मान कि घोषणा void main()
{
तथा मान को इनपुट किया जाता है इसे प्रदर्शित
int i;
करने के लिये प्रोग्राम। int arr[3] = {10, 20, 30}; //declaring and initializing
one-dimensional array in C
#include<stdio.h> for ( i = 0; i < 3; i++)
void main() {
{ // accessing elements of array
int marks[5]; //one dimensional array declaration printf(" Value of array: %d\n", arr[i]);
int i; }
printf("Enter five marks"); }
for(i=0; i<5; i++)
{ Output-Value of array: 10
Value of array: 20
scanf("%d",&marks[i]); Value of array: 30
+++++++++++++++++++++++++++++++++
} Example3-C program to find largest element in
for(i=0; i<5; i++) array
{ #include<stdio.h>
printf("\n Marks are: %d",marks[i]); void main()
} {
} // declaring and initializing one-dimensional array
int arr[5] = {25, 11, 7, 75, 56};
Input- Enter five marks-
90 //Initialize max with first element of array
75 int max = arr[0];
55
85 //Loop through the array
45 for (int i = 0; i<5; i++)
{
Output- Marks are: //Compare elements of array with max
90 if(arr[i] > max)
75 max = arr[i];
55 }
85 printf("Largest element present in given array:
45 %d\n", max);
}

Output-Largest element present in given array:75

Prof. Gaurav Gupta


Programming in C language – B.A. 1st Year (Computer Application)
Example4- C program to calculate the average of
array element

#include<stdio.h>
#include<conio.h>
void main()
{
int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int i,sum;
float avg;
sum = 0;

//calculation process
for(i=0;i<10;i++)
{
sum=sum+array[i];
}
avg=(float)sum/10;

//to display result


printf("Average of array values is: %f", avg);
getch();
}

Output- Average of array values is: 4.500000


+++++++++++++++++++++++++++++++++++

Prof. Gaurav Gupta


Programming in C language – B.A. 1st Year (Computer Application)
Two Dimensional Array
Example1- Addition of two matrix of 2by3 size
#include <stdio.h>
#include <conio.h>
void main()
{
int a[2][3],b[2][3],c[2][3],i,j;
printf("\n ENTER VALUES FOR MATRIX A:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n ENTER VALUES FOR MATRIX B:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nTHE VALUES OF MATRIX C ARE:\n");
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
printf("%d",c[i][j]);
printf("\n");
}
}
getch();
}

Output-

Prof. Gaurav Gupta


Programming in C language – B.A. 1st Year (Computer Application)
String
String- Characters का array string कहलाता है । Example3- Program using puts() function
Syntax- datatype string_name[size]; #include<stdio.h>
void main()
Example- char name[30];
{
char name[30];
Initialization of array of Char type- printf("Enter Your Name:");
char name[7]={‘g’,’a’,’u’,’r’,’a’,’v’,’\0’}; gets(name);
puts(name);
}
string हमेशा Null Character से समाप्त होती है , जिसे
‘\0’ से दिखाते है । Input- Enter Your Name: Raja
Output-Raja
or char name[7]={“gaurav”}; ++++++++++++++++++++++++++++++
or char name[]={“COMPUTER”};
or char name[]={‘C’,’O’,’M’,’P’,’U’,’T’,’E’,’R’} String handling function-
or char name[10]={‘R’,’A’,’J’,’A’}; 1 strcat()
+++++++++++++++++++++++++++ 2 strcmp()
gets()- gets() function का प्रयोग स्ट्रिंग को 3 strcpy()
4 strlen()
इनपुट करवाने के लिए किया जाता है ।
5 strrev()
syntax- gets(string);
6 strlwr()
7 strupr()
puts()- puts() function प्रयोग स्ट्रिंग को इनपुट
print करवाने के लिए किया जाता है । यह सभी फं क्शन “string.h” header file मे स्टोर
syntax- puts(string); होते है , इसलिए इन फं क्शन का उपयोग करने के
+++++++++++++++++++++++++++++ लिए “string.h” header file को अपने प्रोग्राम मे
Example1 Program for print a string
लिंक करना आवश्यक होता है ।
#include<stdio.h>
void main()
{ Example1- strlen() function
char name[]={"gaurav"};
printf("%s",name); #include<stdio.h>
} #include<string.h>
Output- gaurav void main()
++++++++++++++++++++++++++++++++++++++ {
Example2 program using get() function char name[30]={"gaurav gupta"};
#include<stdio.h> int result;
void main() result=strlen(name);
{ printf("String length is: %d",result);
char name[30]; }
printf("Enter Your Name:");
gets(name); Output- String length is:12
printf("Your Name is: %s",name);
}
Input-Enter Your Name: gaurav
Output- Your Name is: gaurav

Prof. Gaurav Gupta


Programming in C language – B.A. 1st Year (Computer Application)
Example2- strrev() function Example5- strcmp() function
#include<stdio.h>
#include<stdio.h> #include <string.h>
#include<string.h> void main()
void main() {
{ char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
char name[30]; int result;
printf("Enter a string:");
gets(name); // comparing strings str1 and str2
strrev(name); result = strcmp(str1, str2);
puts(name); printf("Comparison between string1 and String2 =
} %d\n", result);

Input- Enter a string: india // comparing strings str1 and str3


Output: aidni result = strcmp(str1, str3);
++++++++++++++++++++++++++++++++ printf("Comparison between string1 and String3 =
Example3- strlwr() function %d\n", result);
#include<stdio.h> }
#include<string.h>
void main() Output- Comparison between string1 and String2=1
{ Comparison between string1 and String3=0
char name[30];
printf("Enter a string:"); 1 means string1 and string2 are not equal
gets(name); 0 means string1 and string3 are equal
strlwr(name); +++++++++++++++++++++++++++++++++++++
puts(name); Example6-strcat() function
} #include<stdio.h>
#include<string.h>
Input- Enter a string: INDIA void main()
Output-india {
+++++++++++++++++++++++++++++++++ char str1[]="India is";
Example4- strupr() function char str2[]="big country";
#include<stdio.h> strcat(str1,str2);
#include<string.h> puts(str1);
void main()
{ Output- India is big country
char name[30]; +++++++++++++++++++++++++++++++++++
printf("Enter a string:"); Example7 strcpy() function
gets(name); #include <stdio.h>
strupr(name); #include <string.h>
puts(name); void main()
} {
char str1[20] = "C programming";
Input-Enter a string: india char str2[20];
Output-INDIA
// copying str1 to str2
strcpy(str2, str1);
puts(str2); // C programming
}
Output-C programming

Prof. Gaurav Gupta

You might also like