Csf101 Unit 03
Csf101 Unit 03
Solving
Session: 2023-24
Declaratio
• Now we need to store rollno of 100 students.
n
int rollno101, rollno102, rollno103,
rollno104...;
1 // data[2][4]
2 can be initialized like this also
int data[2][4]={{1,2,3,4},{5,6,7,8}};
Read(Scan) 2D Array Elements
Program
1 void main(){
2 int data[3][3],i,j;
3 for(i=0;i<3;i++)
4 {
5 for(j=0;j<3;j++)
Outpu
6 { t
7 printf("Enter array element="); Enter array element=1
8 scanf("%d",&data[i][j]); Enter array element=2
9 } Enter array element=3
10 } Enter array element=4
11 for(i=0;i<3;i++) Enter array element=5
12 { Enter array element=6
13 for(j=0;j<3;j++) Enter array element=7
14 { Enter array element=8
15 printf("%d",data[i][j]); Enter array element=9
16 } 123
17 printf("\n"); 456
18 } 789
19 }
Develop a program to count number of positive, negative
and zero elements from 3 X 3 matrix
Program
1 void main(){ Outpu
2 int data[3][3],i,j,pos=0,neg=0,zero=0; t
3 for(i=0;i<3;i++) Enter array element=9
4 { Enter array element=5
5 for(j=0;j<3;j++) Enter array element=6
6 { Enter array element=-3
7 printf("Enter array element="); Enter array element=-7
8 scanf("%d",&data[i][j]); Enter array element=0
9 if(data[i][j]>0) Enter array element=11
10 pos=pos+1; Enter array element=13
11 else if(data[i][j]<0) Enter array element=8
12 neg=neg+1; positive=6,negative=2,zero
13 else =1
14 zero=zero+1;
15 }
16 }
17 printf("positive=%d,negative=%d,zero=
18 %d",pos,neg,zero);
}
String
(Character Array)
Definition: String
• A String is a one-dimensional array of characters terminated
by a null('\0').
[0] [1] [2] … [9]
char name[10]
;
Syntax
void main()
{
// body part
}
• Why function ?
• Avoids rewriting the same code over and over.
• Using functions it becomes easier to write programs and keep track of what they
doing.
Types of Function
Function
void main()
{
....
func1(); Function call
}
void func1()
{
....
Function
//function body definition
....
}
Function Prototype
A function Prototype also know as function declaration.
A function declaration tells the compiler about a function name and how
to call the function.
It defines the function before it is being used or called.
A function prototype needs to be written at the beginning of the
program.
Syntax Example
return-type function-name (arg-1, arg 2, void addition(int, int);
…);
Function Definition
A function definition defines the functions header and body.
A function header part should be identical to the function prototype.
Function return type
Function name
List of parameters
A function body part defines function logic.
Function statements
Syntax Example
return-type function-name (arg-1, arg 2, void addition(int x, int y)
…) {
{ printf("Addition is=%d“,
//... Function body (x+y)); }
}
WAP to add two number using add(int, int) Function
Program Outpu
t
1 #include <stdio.h> Addition is = 11
2 void add(int, int); // function declaration
3
4 void main()
5 {
6 int a = 5, b = 6;
7 add(a, b); // function call
8 }
9
10 void add(int x, int y) // function definition
11 {
12 printf("Addition is = %d", x + y);
13 }
Actual parameters and Formal parameters
• Values that are passed to the called function from the main
function are known as Actual parameters.
• The variables declared in the function prototype or definition
are known as Formal parameters.
• When a method is called, the formal parameter is temporarily
"bound" to the actual parameter.
Actual paramete Formal
rs parameters
void main() void add(int x, int y) // x and y are
{ formal parameters.
int a = 5, b = 6; {
add(a, b); // a and b are the printf("Addition is = %d", x + y);
actual parameters in this
call. }
}
Return Statement
• If function is returning a value to calling function, it needs to
use the keyword return.
• The called function can only return one value per call.
Syntax
return;
Or
return (expression);
WAP to find maximum number from two number
Program Outpu
t
1 #include <stdio.h> Max value is : 200
2 int max(int a, int b);
3 void main()
4 {
5 int a = 100;
6 int b = 200;
7 int maxvalue;
8 maxvalue = max(a, b);
9 printf("Max value is : %d\n",
10 maxvalue);
11 }
12 int max(int a, int b)
13 {
14 if (a > b)
15 return a; // return a
16 else
17 return b; // return b
18 }
WAP to calculate the Power of a Number
Program Outpu
t
1 #include <stdio.h> Enter any number : 5
2 int power(int, int); Enter power of number : 3
3 void main()
4 {
5 int num, pow, res;
5's power 3 = 125
6 printf("Enter any number : ");
7 scanf("%d", &num);
8 printf("Enter power of number : ");
9 scanf("%d", &pow);
10 res = power(num, pow);
11 printf("%d's power %d = %d", num, pow, res);
12 }
13 int power(int n, int p)
14 { int r = 1;
15 while (p >= 1)
16 {
17 r = r * n;
18 p--;
19 }
20 return r;}
WAP to find Factorial of a Number
Program Outpu
t
1 #include <stdio.h> Enter the number :
2 int fact(int); 5
3 int main() factorial = 120
4 {
5 int n, f;
6 printf("Enter the number :\n");
7 scanf("%d", &n);
8 f = fact(n);
9 printf("factorial = %d", f);
10 }
11 int fact(int n)
12 {
13 int i, fact = 1;
14 for (i = 1; i <= n; i++)
15 fact = fact * i;
16 return fact;
17 }
WAP to check Number is Prime or not
Program
Program
contd.
1 #include <stdio.h> 14 int checkPrime(int n1)
2 int checkPrime(int); 15 {
3 void main() 16 int i = 2;
4 { while (i <= n1 / 2)
17
5 int n1, prime; {
6 printf("Enter the number :");
18 if (n1 % i == 0)
7 scanf("%d", &n1); 19 return 0;
8 prime = checkPrime(n1); 20 else
9 if (prime == 1) 21 i++;
10 printf("The number %d is a prime 22 }
number.\n", n1); 23 return 1;
11 else 24 }
12 printf("The number %d is not a
prime number.\n", n1);
13 }
Outpu
t
Enter the number :7
The number 7 is a prime number.
Category of Function
(1) Function with no argument and but no return value
No
void main() Input void fun1()
{ {
..... .....
No return .....
fun1();
value .....
.....
} }
Storage Initial
Storage Scope Life Example
Specifier Value
Automatic int a;
Stack Garbage Within block End of block
{auto} auto int a;
Thank you