Ans of C Lab Work-3

Looping

Looping is the process of executing the same program statement or block of program statements repeatedly for a specified number of times or till the given condition is satisfied. Loop structure is used to carry out looping.

Mainly there are three types of loop:
  1. while loop
  2. do while loop
  3. for loop
while loop

It executes the program statements repeatedly until the given condition is true. It checks the condition at first; if it is found true then it executes the statements written in its body part otherwise it just gest out from the loop structure. It is also known as entry control or pre-test loop.

Syntax of while loop:
initialization;
while(condition)
{
statements;
increment/decrement;
}
where initialization means starting point, condition means stopping point and increment/decrement means counter.
Flowchart of while loop

do while loop

It also executes program statements repeatedly until the given condition is true. It executes the program statements once at first then only condition is checked. If a condition is found true then it executes the program statements again, otherwise it gets out from the loop structure. As it checks the condition at last it is also known as the post-test loop or exit control loop.

Syntax of do while Loop:
initialization;
do
{
statements;
increment/decrement;
}while (condition);
Flowchart of do while Loop:
do while loop flowchart
while loop do while loop
(i) In the while loop, condition is checked in the beginning. (i) In the do while loop, condition is checked at the end.
(ii) It is also known as a pre-test or entry control loop. (ii) It is also known as post-test or exit control loop.
(iii) It is not terminated with a semicolon. (iii) It is terminated with a semicolon.
(iv) In the while loop, statements are not executed if the condition is false. (iv) In the do while loop, statements are executed once even the condition is false.
(v) It uses the keyword ‘while’. (v) It uses two keywords ‘do’ and ‘while’.
(vi) The syntax of while loop is as follows: initialization; while(condition) { statements; increment/decrement; } (vi) The syntax of do-while loop is as follows: initialization; do { statements; increment/decrement; } while(condition);
(vii) The operation of while loop can be represented using flowchart as follows: (vii) The operation of do while loop can be represented using flowchart as follows: do while loop flowchart
(viii) Example of while loop: #include<stdio.h> main() { int i=1; while(i>=10) { printf("I love my country"); i++; } }
Output: This program displays nothing as output; as condition is evaluated false in the beginning.
(viii) Example of do while loop: #include<stdio.h> main() { int i=1; do { printf("I love my country"); i++; }while(i>=10); }
Output: This program displays "I love my country" as output at once.

for loop

It is the most common type of loop which is used to execute a program statement or block of program statements repeatedly for a specified number of times. It is a definite loop. Mainly it consists of three expressions: initialization, condition and increment / decrement. The initialization defines the loop starting point, condition defines the loop stopping points and counter helps to increment and decrement the value of counter variable.

Syntax of for loop:
for(initialization;condition;increment/decrement )
{
statements;
}
Flowchart of for loop
One program per page [ Write in Single Side ONLY]
1. Write a C program to calculate and display the following series: 1 3 5 .... to 10th terms

Algorithm to calculate and display : 1 3 5 .... to 10th terms

  1. Start
  2. Set i = 0 and a=1
  3. Check is i<10?
    3.1 If yes, Print ‘a’ and calculate a=a+2 and goto step 4
    3.2 If no, go to step 5.
  4. Calculate i=i+1 and go to step 3
  5. Stop
Flowchart to calculate and display : 1 3 5 .... to 10th terms

Code to calculate and display : 1 3 5 .... to 10th terms

#include <stdio.h> main() { int i=0,a=1; for(i=0;i<10;i++) { printf(" %d\t ",a); a=a+2; } }

Output

1 3 5 7 9 11 13 15 17 19
2. Write a C program to display a square series of first ‘n’ natural numbers.

Algorithm to display a square series of first ‘n’ natural numbers

  1. Start
  2. Input a number as n
  3. Set i=1
  4. Check is i<=n?
    4.1 If yes, Calculate sq=i*i ; Print “ sq ” and goto step 5
    4.2 If no, goto step 6
  5. Calculate i=i+1 and go to step 4
  6. Stop
Flowchart to display a square series of first ‘n’ natural numbers

C Code to display a square series of first ‘n’ natural numbers

#include <stdio.h> main() { int i,n,sq; printf("Enter the value of n : "); scanf("%d",&n); for(i=1;i<=n;i++) { sq=i*i; printf(" %d\t ",sq); } }

Output

Enter the value of n : 5 1 4 9 16 25
3. Write a C program to calculate the factorial of any number given by the user. [The product of a given positive integer multiplied by all lesser positive integers: The quantity five factorial (5!) =5x4x3x2x1=120.]

Algorithm to calculate the factorial of any number given by the user

  1. Start
  2. Input a number as n
  3. Set f=1,i=n
  4. Check is i >=1?
    4.1 If yes, Calculate f=f*i and goto step 5
    4.2 If no, goto step 6
  5. Calculate i=i-1 and go to step 4
  6. Print f
  7. Stop
Flowchart to calculate the factorial of any number given by the user

C Code to calculate the factorial of any number given by the user

#include<stdio.h> main() { int n,i,f=1; printf("Enter a number : "); scanf("%d",&n); for(i=n;i>=1;i--) { f=f*i; } printf("Factorial of %d = %d",n,f); }

Output

Enter a number : 5
Factorial of 5 = 120
4. Write a C program to calculate and display the multiplication table of a number.

Algorithm to calculate and display the multiplication table of a number

  1. Start
  2. Input a number as n
  3. Set i=1
  4. Check is i <=10?
    4.1 If yes, Calculate and display n*i and goto step 5
    4.2 If no, goto step 6
  5. Calculate i=i+1 and go to step 4
  6. Stop
Flowchart to multiplication table of a number

C Code to calculate and display the multiplication table of a number

#include <stdio.h> main() { int i,n; printf("Enter the value of n : "); scanf("%d",&n); for(i=1;i<=10;i++) { printf("%d x %d = %d \n", n,i,n*i); } }

Output

Enter the value of n : 2 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20
5. Write a C program to display Fibonacci series having ‘n’ terms (Fibonacci series means the sum of two preceding numbers will create a succeeding number. Example: 0,1,1,2,3,5,.....)

Algorithm to display Fibonacci series having ‘n’ terms

  1. Start
  2. Set a=0,b=1, i=0
  3. Ask the value of ‘n’
  4. Is i<n?
    4.1 If yes, Print "a" and calculate c=a+b . Also set a=b , b=c
    4.2 If no, go to step 6
  5. Calculate i=i+1 and go to step 4
  6. Stop
Flowchart to display Fibonacci series having 'n' terms

C Code to display Fibonacci series having ‘n’ terms

#include <stdio.h> main() { int a=0,b=1,c,n,i; printf(" Enter the value of n : "); scanf("%d",&n); for(i=0;i<n;i++) { printf(" %d ",a); c=a+b; a=b; b=c; } }

Output

Enter the value of n : 10 0 1 1 2 3 5 8 13 21 34
6. Write a C program to check if a given number is palindrome or not palindrome . (If a number gives the same number when it reversed is called palindrome number .eg 121, 101 etc)

Algorithm to check if a given number is palindrome or not palindrome

  1. Start
  2. Initialize int n,r,sum=0 and a
  3. Ask to enter any number as n
  4. Set a=n
  5. Is n !=0?
    5.1 If yes,
    calculate r=n%10 ,
    sum=sum*10+r and
    n=n/10
    5.2 If no, go to step 6
  6. Is sum = = a?
    6.1 If yes, display “ Palindrome number ”
    6.2 If no, display “ Not Palindrome number ”
  7. Stop
Flowchart to check if a given number is palindrome or not palindrome

C Code to check if a given number is palindrome or not palindrome

#include <stdio.h> main() { int n,r,sum=0,a; printf(" Enter the value of n : "); scanf("%d",&n); a=n; while(n!=0) { r=n%10; sum=sum*10+r; n=n/10; } if(sum==a) printf(" Palindrome number "); else printf(" Not Palindrome number "); }

Output

Enter the value of n : 101 Palindrome number ----------------------- Enter the value of n : 123 Not Palindrome number
7. Write a C program to calculate sum of the following series: sum=1+½+⅓+¼+...+1/n

Algorithm to calculate sum of given series

  1. Start
  2. Set sum=0
  3. Ask the value of n
  4. Set i=1
  5. Is i<=n?
    5.1 If yes, calculate sum=sum+1/i and go to step 6
    5.2 If no, go to step 7
  6. Calculate i=i+1 and go to step 5
  7. Print sum
  8. Stop
Flowchart to calculate sum of given series

C Code to calculate sum of given series

#include <stdio.h> main() { float n,i,sum=0; printf(" Enter the value of n: "); scanf("%f",&n); for(i=1;i<=n;i++) { sum=sum+1/i; } printf(" Sum = %0.2f ",sum); }

Output

Enter the value of n: 3 Sum = 1.83
8. Write a C program to display the following output:
1 2 3 4 5 2 4 6 8 10 3 6 9 12 15

Algorithm to display given number pattern:

  1. Start
  2. Set the outer loop(i) from 1 to 3
  3. Set the inner loop(j) from 1 to 5
  4. Calculate and print i*j
  5. Stop
Flowchart to display given number pattern

C Code to display given number pattern:

#include <stdio.h> main() { int i,j; for(i=1;i<=3;i++) { for(j=1;j<=5;j++) { printf("%5d",i*j); } printf("\n"); } }

Output

1 2 3 4 5 2 4 6 8 10 3 6 9 12 15
9. Write a C program to check whether the given number is prime or not.

Algorithm to check given number is prime or composite

  1. Start
  2. Initialize I=1, count=0
  3. Input any number as N
  4. Is I<=N?
    4.1: If yes, Calculate remainder(R) of num divided by I and check R=0? If yes, calculate count=count+1 and go to step 5
    4.2: If no, go to step 6
  5. Calculate I=I+1 and go to step 4
  6. Is count equal to two.
    6.1: If yes, Print "Prime"
    6.2: If no, Print "Composite"
  7. Stop
Flowchart to check given number is prime or composite

C Code to check given number is prime or composite

#include <stdio.h&gt main() { int n,count=0,i; printf("Enter any number : "); scanf("%d",&n); for(i=1;i<=n;i++) { if(n%i==0) count++; } if(count==2) printf("%d is prime number.",n); else printf("%d is composite number.",n); }

Output

Enter any number : 13 13 is prime number. ------------------- Enter any number : 4 4 is composite number.
10. Write a C program display the prime series:
2 3 5 7 11 upto n

Algorithm to display prime series upto n

  1. Start
  2. Input any number as n
  3. Set i=1
  4. Is i<=n?
    4.1 If yes, set j=1 , count=0
    (a) Is j<=i?
    (a1) If Yes, calculate remainder(R) of i divided by j
    (a2) check R=0? If yes, calculate count=count+1 and j=j+1
    If no, only calculate j=j+1 and go to step (a)
    (b) If no, goto step 5
    4.2: If no, go to step 7
  5. Is count equal to two.
    5.1: If yes, Print i and goto step 6
    5.2: If no, go to step 6
  6. Calculate i=i+1 and go to step 4
  7. Stop
Flowchart to display the prime series up to n

C Code to display the prime series up to n

#include <stdio.h> main() { int count,n,i,j; printf("Enter value of n: "); scanf("%d",&n); for(i=1;i<=n;i++) { count=0; for(j=1;j<=i;j++) { if(i%j==0) count++; } if(count==2) printf(" %d ",i); } }

Output

Enter value of n: 20 2 3 5 7 11 13 17 19