Professional Documents
Culture Documents
Green University of Bangladesh Department of Computer Science and Engineering (CSE)
Green University of Bangladesh Department of Computer Science and Engineering (CSE)
Student Details
Name ID
1.
[For Teachers use only: Don’t Write Anything inside this box]
OBJECTIVES/AIM
• To attain knowledge on for loop.
• To identify and solve problems using for loop.
• To implement programs using for loop concept.
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;
scanf("%d",&n);
m=n/2;
if(n%i==0)
flag=1;
break;
if(flag==0)
printf("Number is prime");
return 0;
}
Output:
Procedure:
Algorithm:
Step 1: Input the n value until which the Fibonacci series has to be generated.
Step 4: sum = a + b
If (sum > n)
Else
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.
Implementation/code:
#include <stdio.h>
printf("%6lld", term);
}
printf("\n");
}
return 0;
}
Output: