Nothing Special   »   [go: up one dir, main page]

Unit 3 (Softcopy)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

Unit-03(Loops in C)

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.

ii) Looping is also called REPETATIVE or an ITERATIVE CONTROL MECHANISM.

iii) C provides three types of loop control structure that are as follows:

1. For Loop 2. While Loop 3. Do-While Loop

1. For Loop:

i) Most commonly and simplest used loop in C.

ii) This statement is used when programmer knows how many times a set of statement are to be
executed.

Syntax: for(initialization; test condition; iteration)

Statements;

}
2. While Loop

i) It is known as entry controlled loop (pre-test loop).

ii) It keeps repeating an action until an associated test returns false.

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.

It is also know as exit control loop or post test loop.

Syntax:

Initialization;

do

Program Statements;

Iteration;

} while(condition);
WAP to print the table of a given number using all 3 loops.

Using For Loop

#include <stdio.h>

#include <conio.h>

void main()

int n , I , T;

printf(“enter the number”);

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();

Using While Loop

#include <stdio.h>

#include <conio.h>

void main()

int n , I , T;

printf(“enter the number”);

scanf(“%d”,&n);

while(i<=10)
{

T=n*i;

printf(“%d*%d=%d\n”,n,I,T); OR printf(“%d”,T);

i++;

getch();

Using Do-While Loop

#include <stdio.h>

#include <conio.h>

void main()

int n , I , T;

printf(“enter the number”);

scanf(“%d”,&n);

do

T=n*i;

printf(“%d*%d=%d\n”,n,I,T); OR printf(“%d”,T);

i++;

} while(i<=10);

getch();

WAP to print your name 500 times

#include <stdio.h>
#include <conio.h>

void main()

int i;

for(i=1;i<=500;i++)

printf(“My name is Santosh”);

getch();

WAP to print the counting from 100 to 1

#include <stdio.h>

#include <conio.h>

void main()

Int i;

for(i=100;i>=1;i--)

printf(“%d”,i);

getch();

WAP to print the even number from 100 to 1000

#include <stdio.h>

#include <conio.h>
void main()

Int i;

for(i=100;i<=1000;i=i+2)

printf(“%d”,i);

getch();

WAP to print odd number 100 to 1

#include <stdio.h>

#include <conio.h>

void main()

Int i;

for(i=99;i>=1;i=i-2)

printf(“%d\n”,i);

getch();

WAP to print from 1 to 50 odd number

#include <stdio.h>

#include <conio.h>

void main()
{

Int i;

for(i=1;i<=50;i=i+2)

printf(“%d\n”,i);

getch();

WAP to print from 1 to 50 odd number using do-while loop

#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();

WAP to find the sum of 1st 50 natural number.

#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();

WAP to find the sum of 1st n integer by while loop.

#include <stdio.h>

#include <conio.h>

void main()

int a=0,i=1,n;

printf(“enter the limit of the number”);

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++)

printf(“enter the number”);

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();

WAP to find the factorial of a given number.

#include <stdio.h>

#include <conio.h>

void main()

int f=1,n;

printf(“enter the number”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

f=f*i;

printf(“the factorial is=%d”,f);

getch();
}

WAP to check given number is Armstrong or not

#include <stdio.h>

#include <conio.h>

void main()

int n,d,a,s=0,f;

printf(“enter the number”);

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

printf(“number is not Armstrong”);

getch();

}
WAP to print the reverse of a given number

#include <stdio.h>

#include <conio.h>

void main()

int n,i,r,s=0;

printf(“enter the number”);

scanf(“%d”,&n);

for(i=n;i>0;i=i/10)

r=i%10;

s=s*10+r;

printf(“%d”,s);

getch();

WAP to check the given number is palindrome or not

#include <stdio.h>

#include <conio.h>

void main()

int n,i,r,s=0;

printf(“enter the number”);

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

printf(“number is not palindrome”);

getch();

WAP to check the given number is prime or not

#include <stdio.h>

#include <conio.h>

void main()

int n,c=0;

printf(“enter the number”);

scanf(“%d”,&n);

for(i=2;i<=n;i++)

if((n%i==0)

c++;

if(c==2)

printf(“Number is prime”);
else

printf(“Number is not prime”);

getch();

WAP to print fibonacci sreies

#include <stdio.h>

#include <conio.h>

void main()

int a=0,b=1,c=0,n,I;

printf(“%d%d”,a,b);

printf(“enter the number”);

scanf(“%d”,&n);

for(i=1;i<=n;i++)

c=a+b;

a=b;

b=c;

printf(“%d”,c);

getch();

Nested Loop:

Syntax:

for(initialization; test condition; iteration)


{

for(initialization; test condition; iteration)

Statements;

i) WAP to draw a pattern.

****

****

****

****

#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();

iii) WAP to draw a pattern.

*****

****

***

**

*
#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();

iv) WAP to draw a pattern.

**

***

****

*****

#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();

v) WAP to draw a pattern.

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();

vi) WAP to draw a pattern.

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();
}

vii) WAP to draw a pattern.

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();

Differentiate between actual and formal arguments

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;

printf(“enter two numbers”);

scanf(“%d%d”,&a,&b);

add(a,b);

getch();

void add(int x,int y)

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;

printf(“enter two numbers”);

scanf(“%d%d”,&a,&b);

add(&a,&b);

getch();

void add(int * x,int * y)

int z;

z=*x + *y;

printf(“%d”,z);

WAP to swap the value of two variables using call by value and call by reference.

Using call by value:

#include <stdio.h>

#include <conio.h>

void swap(int,int);

void main()

int a,b;

printf(“enter two numbers”);

scanf(“%d%d”,&a,&b);

swap(a,b);
getch();

void swap(int x,int y)

int z;

z=x;

x=y;

y=z;

printf(“%d%d”,x,y);

Using call by reference:

#include <stdio.h>

#include <conio.h>

void swap(int *,int *);

void main()

int a,b;

printf(“enter two numbers”);

scanf(“%d%d”,&a,&b);

swap(&a,&b);

getch();

void swap(int * x,int * y)

int *z;

*z=*x;
*x=*y;

*y=*z;

printf(“%d%d”,*x,*y);

Q. What do you mean by return() keyword?

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.

WAP to find the factorial of a given number using recursion:

#include <stdio.h>

#include <conio.h>

int rec(int);

void main()

Int num,fact;

printf(“enter the number”);

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;

WAP to print fibonacci series upto a given number using recursion:

#include<stdio.h>

int Fibonacci(int);

int main()

int n, i = 0, c;

scanf("%d",&n);

printf("Fibonacci series\n");

for ( c = 1 ; c <= n ; c++ )

printf("%d\n", Fibonacci(i));

i++;

return 0;

}
int Fibonacci(int n)

if ( n == 0 )

return 0;

else if ( n == 1 )

return 1;

else

return ( Fibonacci(n-1) + Fibonacci(n-2) );

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.

ii) Life Time: what time period a variables is accessible.

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.

There are 4 types of storage class:

1. automatic

2. external

3. static

4. register

Table of Storage Class

Storage Keyword Scope Life time Storage Initial or Example


Class Default
Value
automatic auto local With-in a Main Garbage auto int i;
block memory or value
RAM
register register local With-in a register Garbage register int i;
block value
static static local Between Main zero static int i;
different memory or
function RAM
class
external extern global Through-out Main zero extern int i;
the program memory or
execution RAM

i) Automatic storage class:

Keyword for automatic variable “auto”.

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.

ii) External storage class:

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.

Example to demonstrate working of external variable

#include<stdio.h>

void Check();

int a=5;

/* a is global variable because it is outside every function */

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

iii) Register Storage Class:

Keyword to declare register variable “register”.

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.

iv) Static Storage Class:

The value of static variable persists until the end of the program.

A variable can be declared static using keyword: “static”.

For example: static int i;

Here, i is a static variable.

Example to demonstrate the static variable:

#include
void Check();

int main()

Check();

Check();

Check();

void Check()

static int c=0;

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

You might also like