Unit 3 (Softcopy)
Unit 3 (Softcopy)
Unit 3 (Softcopy)
Looping: It is a powerful programming technique through which a group of statements are executed
repeatedly until certain condition is satisfied.
i) Looping involves repeating some portion of the program either a specified number of times or until
particular condition is being satisfied.
iii) C provides three types of loop control structure that are as follows:
1. For Loop:
ii) This statement is used when programmer knows how many times a set of statement are to be
executed.
Statements;
}
2. While Loop
iii) This is useful when programmer does not know in advance how many times the loop will be
executed.
Syntax:
Initialization;
while(condition)
Program Statements;
Iteration;
}
3. Do-While Loop: do-while loop is similar but the test occur after the loop body is executed. This ensurs
that loop body is run at least once.
Whenever one is certain about a test condition,do-while can be used it enter into the loop atleast once
then check the whether the condition is true or false.
As long as test condition is true the loop statement will be repeated again and again.
Syntax:
Initialization;
do
Program Statements;
Iteration;
} while(condition);
WAP to print the table of a given number using all 3 loops.
#include <stdio.h>
#include <conio.h>
void main()
int n , I , T;
scanf(“%d”,&n);
for(i=1;i<=10;i++)
T=n*i;
printf(“%d*%d=%d\n”,n,I,T); OR printf(“%d”,T);
getch();
#include <stdio.h>
#include <conio.h>
void main()
int n , I , T;
scanf(“%d”,&n);
while(i<=10)
{
T=n*i;
printf(“%d*%d=%d\n”,n,I,T); OR printf(“%d”,T);
i++;
getch();
#include <stdio.h>
#include <conio.h>
void main()
int n , I , T;
scanf(“%d”,&n);
do
T=n*i;
printf(“%d*%d=%d\n”,n,I,T); OR printf(“%d”,T);
i++;
} while(i<=10);
getch();
#include <stdio.h>
#include <conio.h>
void main()
int i;
for(i=1;i<=500;i++)
getch();
#include <stdio.h>
#include <conio.h>
void main()
Int i;
for(i=100;i>=1;i--)
printf(“%d”,i);
getch();
#include <stdio.h>
#include <conio.h>
void main()
Int i;
for(i=100;i<=1000;i=i+2)
printf(“%d”,i);
getch();
#include <stdio.h>
#include <conio.h>
void main()
Int i;
for(i=99;i>=1;i=i-2)
printf(“%d\n”,i);
getch();
#include <stdio.h>
#include <conio.h>
void main()
{
Int i;
for(i=1;i<=50;i=i+2)
printf(“%d\n”,i);
getch();
#include <stdio.h>
#include <conio.h>
void main()
Int i=1;
do
if((i%2)!=0)
printf(“%d\n”,i);
i++;
}while(i<50);
getch();
#include <stdio.h>
#include <conio.h>
void main()
Int i,a=0;
for(int i=1;i<=50;i++)
a=a+i;
printf(“%d”,a);
getch();
#include <stdio.h>
#include <conio.h>
void main()
int a=0,i=1,n;
scanf(“%d”,&n);
while(i<=n)
a=a+i;
i++;
printf(“%d”,a);
getch();
}
WAP to find the sum and average of 10 different numbers using for loop.
#include <stdio.h>
#include <conio.h>
void main()
int i,a=0,n,avg;
for(i=1;i<=10;i++)
scanf(“%d”,&n);
a=a+n;
avg=a/10;
printf(“%d%d”,a,avg);
getch();
WAP to find the sum of even and odd number between 1 to 100
#include <stdio.h>
#include <conio.h>
void main()
int i,sum1=0,sum2=0;
for(i=1;i<=100;i++)
{
if((i%2)==0)
sum1=sum1+i;
else
sum2=sum2+i;
printf(“%d”,sum1);
printf(“%d”,sum2);
getch();
#include <stdio.h>
#include <conio.h>
void main()
int f=1,n;
scanf(“%d”,&n);
for(i=1;i<=n;i++)
f=f*i;
getch();
}
#include <stdio.h>
#include <conio.h>
void main()
int n,d,a,s=0,f;
scanf(“%d”,&n);
n=f;
while(n>0)
d=n%10;
a=d*d*d;
s=s+a;
n=n/10;
if(s==f)
printf(“number is Armstrong”);
else
getch();
}
WAP to print the reverse of a given number
#include <stdio.h>
#include <conio.h>
void main()
int n,i,r,s=0;
scanf(“%d”,&n);
for(i=n;i>0;i=i/10)
r=i%10;
s=s*10+r;
printf(“%d”,s);
getch();
#include <stdio.h>
#include <conio.h>
void main()
int n,i,r,s=0;
scanf(“%d”,&n);
for(i=n;i>0;i=i/10)
{
r=i%10;
s=s*10+r;
if(s==n)
printf(“number is palindrome”);
else
getch();
#include <stdio.h>
#include <conio.h>
void main()
int n,c=0;
scanf(“%d”,&n);
for(i=2;i<=n;i++)
if((n%i==0)
c++;
if(c==2)
printf(“Number is prime”);
else
getch();
#include <stdio.h>
#include <conio.h>
void main()
int a=0,b=1,c=0,n,I;
printf(“%d%d”,a,b);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
c=a+b;
a=b;
b=c;
printf(“%d”,c);
getch();
Nested Loop:
Syntax:
Statements;
****
****
****
****
#include <stdio.h>
#include <conio.h>
void main()
int i,j;
for(i=1;i<=4;i++)
for(j=1;j<=4;j++)
printf(“*”);
printf(“\n”);
getch();
}
ii) WAP to draw a pattern.
**
***
****
*****
#include <stdio.h>
#include <conio.h>
void main()
int i,j;
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
printf(“*”);
printf(“\n”);
getch();
*****
****
***
**
*
#include <stdio.h>
#include <conio.h>
void main()
int i,j;
for(i=5;i>=1;i--)
for(j=1;j<=i;j++)
printf(“*”);
printf(“\n”);
getch();
**
***
****
*****
#include <stdio.h>
#include <conio.h>
void main()
int i,j,k;
for(i=1;i<=5;i++)
{
for(j=4;j>=i;j--)
printf(“ ”);
For(k=1;k<=i;k++)
printf(“*”);
printf(“\n”);
getch();
12
123
1234
12345
#include <stdio.h>
#include <conio.h>
void main()
int i,j;
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
{
printf(“%d”,j);
printf(“\n”);
getch();
12345
1234
123
12
#include <stdio.h>
#include <conio.h>
void main()
int i,j;
for(i=5;i>=1;i--)
for(j=1;j<=i;j++)
printf(“%d”,j);
printf(“\n”);
getch();
}
12
123
1234
12345
#include <stdio.h>
#include <conio.h>
void main()
int i,j,k;
for(i=1;i<=5;i++)
for(j=4;j>=i;j--)
printf(“ ”);
for(k=1;k<=i;k++)
printf(“%d”,k);
printf(“\n”);
getch();
}
viii) WAP to draw a pattern.
22
333
4444
55555
#include <stdio.h>
#include <conio.h>
void main()
int i,j;
for(i=1;i<=5;i++)
for(j=1;j<=i;j++)
printf(“%d”,i);
printf(“\n”);
getch();
i) Actual arguments: Actual parameter are defined in the main program. These variables are used in
place of formal arguments when any function is called.
ii) Formal arguments: formal arguments are the parameters declared in the bracket in the function
definition after the function name. These are called dummy arguments.
The data type of formal parameters should be same as of actual parameters and they should also be in
the same manner as in the function declaration.
Call by value and call by reference:
i) Call by value: When we called a function and pass the value of the variable to the called function then
such function are called call by value.
Call by value in which value of the variable are passed the calling function to the called function.
#include <stdio.h>
#include <conio.h>
void add(int,int);
void main()
int a,b;
scanf(“%d%d”,&a,&b);
add(a,b);
getch();
int z;
z=x+y;
printf(“%d”,z);
ii) Call by reference: when we called a function and pass the address of the original argument(variable)
to the called function, such function are called call by reference.
Call by reference in which address of the variable are passed by calling function to the called function.
#include <stdio.h>
#include <conio.h>
void add(int * ,int * );
void main()
int a,b;
scanf(“%d%d”,&a,&b);
add(&a,&b);
getch();
int z;
z=*x + *y;
printf(“%d”,z);
WAP to swap the value of two variables using call by value and call by reference.
#include <stdio.h>
#include <conio.h>
void swap(int,int);
void main()
int a,b;
scanf(“%d%d”,&a,&b);
swap(a,b);
getch();
int z;
z=x;
x=y;
y=z;
printf(“%d%d”,x,y);
#include <stdio.h>
#include <conio.h>
void main()
int a,b;
scanf(“%d%d”,&a,&b);
swap(&a,&b);
getch();
int *z;
*z=*x;
*x=*y;
*y=*z;
printf(“%d%d”,*x,*y);
Answer: return() keyword is used to return any value from the function called. This statements
terminate the function and return the value to its call.
Recursion:
i) It is function which call itself either directly or indirectly through another function.
ii) Recursion function include a keyword ‘return’ which is used to pass the result to the caller function
possibly the main function.
iii) Recursion is more elegant and requires few variables which make program clean.
iv) Recursion can be used to replace complex nesting code by dividing the problem into same problem of
its sub-type.
#include <stdio.h>
#include <conio.h>
int rec(int);
void main()
Int num,fact;
scanf(“%d”,&num);
fact=rec(num);
printf(“%d”,fact);
getch();
}
Int rec(int n)
int f;
if((n==1)||( n==0))
return 1;
else
f=n*rec(n-1);
return f;
#include<stdio.h>
int Fibonacci(int);
int main()
int n, i = 0, c;
scanf("%d",&n);
printf("Fibonacci series\n");
printf("%d\n", Fibonacci(i));
i++;
return 0;
}
int Fibonacci(int n)
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
Storage Class: Every variable and function in C programming has two properties: type and storage class.
Type refers to the data type of variable whether it is character or integer or floating-point value etc.
Storage class is one which provides four important information about the variable.
i) Scope of the variable: It is the area or the part of the program in which it is recognized or it is
accessible. Scope can be local or global.
iii) Storage: One inside the RAM and other in the internal register of the CPU.
iv) Default or initial value: which can either be zero or garbage value.
1. automatic
2. external
3. static
4. register
Variables declared inside the function body are automatic by default. These variables are also known as
local variables as they are local to the function and doesn't have meaning outside that function.
Since, variable inside a function is automatic by default, keyword auto are rarely used.
External variable can be accessed by any function. They are also known as global variables. Variables
declared outside every function are external variables.
In case of large program, containing more than one file, if the global variable is declared in file 1 and that
variable is used in file 2 then, compiler will show error. To solve this problem, keyword extern is used in
file 2 to indicate that, the variable specified is global variable and declared in another file.
#include<stdio.h>
void Check();
int a=5;
int main()
a+=4;
Check();
return 0;
void Check()
++a;
/* Variable a is not declared in this function but, works in any function as they are global variable- */
printf("a=%d\n",a);
Output a=10
Register variables are similar to automatic variable and exists inside that particular function only.
If the compiler encounters register variable, it tries to store variable in microprocessor's register rather
than memory. Value stored in register is much faster than that of memory.
In case of larger program, variables that are used in loops and function parameters are declared register
variables.
Since, there are limited number of register in processor and if it couldn't store the variable in register, it
will automatically store it in memory.
The value of static variable persists until the end of the program.
#include
void Check();
int main()
Check();
Check();
Check();
void Check()
printf("%d\t",c);
c+=5;
Output
0 5 10
During first function call, it will display 0. Then, during second function call, variable c will not be
initialized to 0 again, as it is static variable. So, 5 is displayed in second function call and 10 in third call.
If variable c had been automatic variable, the output would have been:
000