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

Green University of Bangladesh

Department of Computer Science and Engineering (CSE)


Faculty of Sciences and Engineering
Semester: (Fall, Year: 2021), B.Sc. in CSE (Day)

LAB REPORT NO #03


Course Title: Structured Programming Lab
Course Code: CSE 104 Section: 212DC

Lab Experiment Name: Problem solving using for Loop

Student Details

Name ID
1.

Lab Date : 12th November, 2021


Submission Date : 28th November, 2021
Course Teacher’s Name :

[For Teachers use only: Don’t Write Anything inside this box]

Lab Report Status


Marks: ………………………………… Signature:.....................
Comments:.............................................. Date:..............................

TITLE OF THE LAB EXPERIMENT


Problem solving using for Loop

1. Write a C program to find whether a given number is a prime number or not


2. Print Fibonacci series until a given number. For instance, if a user wants to print
Fibonacci series until
1000, print all the Fibonacci number below 1000.
3. Display Pascal’s Triangle until a given row. For instance, if a user selects row =
6, the pascal triangle for the choice would be something like below:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

OBJECTIVES/AIM
• To attain knowledge on for loop.
• To identify and solve problems using for loop.
• To implement programs using for loop concept.

Answer to the question number 1:

Procedure:
Algorithm:
Step 1: Start
Step 2: Initialize variables num,flag=1, j=2
Step 3: Read num from user
Step 4: If num<=1 // Any number less than 1 is not a prime number
Display "num is not a prime number"
Goto step 7
Step 5: Repeat the steps until j<[(n/2)+1]
5.1 If remainder of number divide j equals to 0,
Set flag=0
Goto step 6
5.2 j=j+1
Step 6: If flag==0,
Display num+" is not prime number"
Else
Display num+" n is prime number"
Step 7: Stop
Im
#include<stdio.h>

int main()

int n,i,m=0,flag=0;

printf("Enter the number to check prime:");

scanf("%d",&n);

m=n/2;

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

if(n%i==0)

printf("Number is not prime");

flag=1;

break;

if(flag==0)

printf("Number is prime");

return 0;
}
Output:

Answer to the question number 2:

Procedure:
Algorithm:

Step 1: Input the n value until which the Fibonacci series has to be generated.

Step 2: Initialize sum = 0, a = 0 and b = 1

Step 3: Print the first two terms of the series, a and b.

Step 4: sum = a + b

Step 5: If(sum < n)

Step 6: print (sum)


Step 7: swap a and b and swap b and sum.

If (sum > n)

Step 8: End the algorithm.

Else

Repeat from steps 4 to 7.

Implementation/Code:

#include<stdio.h>
int main()
{
int sum = 0, n;
int a = 0;
int b = 1;
printf("Enter the nth value: ");
scanf("%d", &n);
printf("Fibonacci series: ");
while(sum <= n)
{
printf("%d ", sum);
a = b;
b = sum;
sum = a + b;
}
return 0;
}

OUTPUT
Answer to the question number 3:

Procedure:
Algorithm:
Step 1: Input number of rows to print from user. Store it in a variable say num.
Step 2: To iterate through rows, run a loop from 0 to num, increment 1 in each
iteration. The loop structure should look like for(n=0; n<num; n++).
Step 3: Inside the outer loop run another loop to print terms of a row. Initialize
the loop from 0 that goes to n, increment 1 in each iteration.
Step 4: Inside the inner loop use formula term = fact(n) / (fact(k) * fact(n-
k)); to print current term of pascal triangle.

Here, fact() is a function defined to find factorial of a number.

Step 5: End the nprogram.

Implementation/code:

#include <stdio.h>

long long fact(int n);


int main()
{
int n, k, num, i;
long long term;

printf("Enter number of rows : ");


scanf("%d", &num);

for(n=0; n<num; n++)


{
for(i=n; i<=num; i++)
printf("%3c", ' ');

for(k=0; k<=n; k++)


{
term = fact(n) / (fact(k) * fact(n-k));

printf("%6lld", term);
}

printf("\n");
}

return 0;
}

long long fact(int n)


{
long long factorial = 1ll;
while(n>=1)
{
factorial *= n;
n--;
}
return factorial;
}

Output:

ANALYSIS AND DISCUSSION

Based on the focused objectives to understand about different types of loops in C


program, the additional lab exercise made me more confident towards the
fulfilment of the objectives.

You might also like