C PGMMNG Notes
C PGMMNG Notes
C PGMMNG Notes
2. Uses of C functions:
#include<stdio.h>
float square ( float x );
main( )
{
float m, n ;
printf ( "\nEnter some number for finding square ");
scanf ( "%f", &m ) ;
n = square ( m ) ;
printf ( "\nSquare of the given number %f is %f",m,n );
}
OUTPUT:
Enter some number for finding square
2
Square of the given number 2.0 is 4.0
1. Call by value
2. Call by reference
1. CALL BY VALUE:
In call by value method, the value of the variable is passed to the function
as parameter.
The value of the actual parameter cannot be modified by formal
parameter.
Different Memory is allocated for both actual and formal parameters.
Because, value of actual parameter is copied to formal parameter.
Note:
main()
{
int m = 22, n = 44;
2. CALL BY REFERENCE:
In call by reference method, the address of the variable is passed to the
function as parameter.
The value of the actual parameter can be modified by formal parameter.
Same memory is used for both actual and formal parameters since only
address is used by both parameters.
EXAMPLE PROGRAM FOR C FUNCTION (USING CALL BY
REFERENCE):
In this program, the address of the variables “m” and “n” are passed to
the function “swap”.
These values are not copied to formal parameters “a” and “b” in swap
function.
Because, they are just holding the address of those variables.
This address is used to access and change the values of the variables.
#include<stdio.h>
void swap(int *a, int *b);
main()
{ int m = 22, n = 44;
Operators in C
Arithmetic Operators:
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}
PROGRAMS
1.Program to display Hello World !
#include<stdio.h>
main()
{
printf(“Hello World!”);
getch()
return;
}
2.Write the program to input an integer and display the value
#include<stdio.h>
main()
{
int a;
printf(“Enter the number”);
scanf(“%d”,&a);
printf(“the number you entered is : %d \n”,a);
getch();
return;
}
3.Write a program to declare one integer variable,character and floating point number &
display all the values on the screen.
#include<stdio.h>
main()
{
int a=20;
char ch=’a’;
float f=19.7675;
printf(“The number you entered is %d \n”,a);
printf(“The character you entered is %c \n”,ch);
printf(“The floating point number is %f \n”,f);
getch();
return;
}
4.Write a program to do all arithmetic operations of two numbers.
#include <stdio.h>
#include<conio.h>
int main()
{
clrscr();
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf ("Addition of a, b is : %d\n", add);
printf ("Subtraction of a, b is : %d\n", sub);
printf ("Multiplication of a, b is : %d\n", mul);
printf ("Division of a, b is : %d\n", div);
printf ("Modulus of a, b is : %d\n", mod);
getch();
return;
}
5.Write a program to input two integer numbers and find the sum
#include <stdio.h>
#include<conio.h>
int main()
{
clrscr();
Int a,b,add;
printf(“enter the first number”);
scanf(“%d”,&a);
printf(“enter the second number”);
scanf(“%d”,&b);
add = a+b;
printf ("Addition of a, b is : %d\n", add);
getch();
return;
}
c = a+b;
return c;
}
8. write the program to find the square of a no using function
#include<stdio.h>
#include<conio.h>
main()
{
int a,s;
clrscr();
printf(“Enter the number”);
scanf(“%d”,&a);
s=sqr(a);
printf(“The square root is :%d”,s);
getch();
return;
}
Int sqr (int x)
{
int c=x*x;
return c;
}
9.write the program to find the product of two numbers
#include <stdio.h>
#include<conio.h>
int main()
{
int x,y,z;
clrscr();
}
Limitation of If
The statement(s) are executed if the condition is true; if the
condition is false nothing happens in other words we may say it
is not the effective one. Instead of it we use if-else statements
etc.
If-else statement
In if-else statement if the condition is true, then the true
statement(s), immediately following the if-statement are
executed otherwise the false statement(s) are executed. The use
of else basically allows an alternative set of statements to be
executed if the condition is false.
Syntax:
If (condition)
{
Statement(s);
}
else
{
statement(s);
}
Example:
#include<stdio.h>
#include<conio.h>
main ()
{
int y;
clrscr();
printf("Enter a year:");
scanf("%d",&y);
if (y % 4==0)
printf("%d is a leap year.",y);
else
printf("%d is not a leap year.",y);
getch();
return 0;
}
IF -else if statement
It can be used to choose one block of statements from many
blocks of statements. The condition which is true only its block
of statements is executed and remaining are skipped.
Syntax:
if (condition)
{
statement(s);
}
else if (condition)
{
statement(s);
}
else
{
(statement);
}
Example:
#include<stdio.h>
#include<conio.h>
main ()
{
int n;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
if(n>0)
printf("The number is positive.");
else if (n<0)
printf("The number is negetive.");
else
printf("The number is zero.");
getch();
return 0;
}
Nested IF
In nested-if statement if the first if condition is true the control
will enter inner if. If this is true the statement will execute
otherwise control will come out of the inner if and the else
statement will be executed.
Syntax:
If (condition)
if(condition)
{
statement(s);
}
else
{
statement(s);
}
else
{
statement(s);
}
Example:
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if(number1 == number2)
{
printf("Result: %d = %d",number1,number2);
}
else
{
printf("Result: %d < %d",number1, number2);
}
return 0;
Switch Statement
Switch statement is alternative of nested if-else.it is executed
when there are many choices and only one is to be executed.
Syntax:
switch(expression)
{
case 1:
statement;
break;
case 2:
statement;
break;
.
.
.
.
case N:
statement;
break;
default:
statement;
}
Example:
#include<stdio.h>
#include<conio.h>
main ()
{
char c;
clrscr();
printf("Enter an alphabet:");
scanf("%c",&c);
switch(c)
{
case'a':
case'A':
printf("You entered vowel.");
break;
case'e':
case'E':
printf("You You entered vowel.");
break;
case'i':
case'I':
printf("You entered vowel.");
break;
case'o':
case'O':
printf("You entered vowel.");
break;
case'u':
case:'U'
printf("You entered vowel.");
break;
default:
printf("You entered a consonant.");
}
getch();
Return;
}
LOOPS :
Loops are used in programming to repeat a specific block until
some end condition is met. There are three loops in C
programming:
1. for loop
2. while loop
3. do...while loop
for Loop
The syntax of for loop is:
for (initializationStatement; testExpression; updateStatement)
{
// codes
}
How for loop works?
The initialization statement is executed only once.
Then, the test expression is evaluated. If the test expression is
false (0), for loop is terminated. But if the test expression is true
(nonzero), codes inside the body of for loop is executed and
the update expression is updated.
This process repeats until the test expression is false.
The for loop is commonly used when the number of iterations is
known.
Example of For loop
#include <stdio.h>
#include<conio.h>
main()
{
int i;
clrscr();
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>
main()
{
int n, i, sum = 0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Sum = %d",sum);
getch();
return 0;
}
Example 3: Multiplication Table Up to 10
The program takes an integer input from the user and generates the
multiplication table up to 10.
#include<stdio.h>
#include<conio.h>
main()
{
int n, i;
clrscr();
printf("Enter an integer: ");
scanf("%d",&n);
While Loop
The most basic loop in C is the while loop and it is used is to
repeat a block of code. A while loop has one control expression
(a specific condition) and executes as long as the given
expression is true.
Syntax
while (condition)
{
statement(s);
}
clrscr();
printf("Enter an integer number: ");
scanf("%d",&num);
i=1;
while(i<=10)
{
printf("%d\n",(num*i));
i++;
}
getch();
return 0;
}
Do While Loop: