Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Programming Lab Assignment N4

This document summarizes a programming lab assignment submitted by Raju Mahato. The assignment includes 4 programs: 1) a program to calculate a series excluding prime numbers, 2) a program to calculate the sum of even numbers between two input numbers, 3) a program to find the GCD and LCM of two numbers, and 4) a program to display the Fibonacci series up to 100. For each program, the document includes the objective, theory, algorithm, code, output, and conclusion.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Programming Lab Assignment N4

This document summarizes a programming lab assignment submitted by Raju Mahato. The assignment includes 4 programs: 1) a program to calculate a series excluding prime numbers, 2) a program to calculate the sum of even numbers between two input numbers, 3) a program to find the GCD and LCM of two numbers, and 4) a program to display the Fibonacci series up to 100. For each program, the document includes the objective, theory, algorithm, code, output, and conclusion.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Programming Lab Assignment: #4

Kathmandu University
COMP102: Computer programming
Submitted by:
Name: Raju Mahato
Roll number: 044
Program: Electrical and Electronics Engineering
Lab Date: 2024/01/22 Submission Date: 2024/01/27

Objective(s)
• To understand the programming using loop & nested loop
statements.
• To be familiar more about working on loops in C(for, while, do-whille)
Theory
Some program needs to be executed multiple times for desired output. For
this we use a loop to repeat some block of codes to execute. This process is called
iteration or looping. For this we use for, while or do while loop. Some large problem
required more looping inside specific block of codes, to encounter this we use more
loops inside root loop to execute specific codes which is called nested loop. This nested
loop executes only previous loops make conditions true. Hence previous loops act as
root of the nested loops.
Syntax:
Outer loop{
Inner loop{
// inner loop statement;
}
Outer loop statement;
}
❖ Lab exercise:
1. Write a program to find sum as Y of the following series excluding
prime numbers in the series.
1 22 33 42 102
Y=1+ + + + +⋯
1! 2! 3! 4! 10!
a) Program analysis
This program is for finding sum using the loop under c language.
This problem required the block of code to be repeated multiple of times and
find the sum. For this we need different variables with different data types.
This series include squares in numerator and factorial in denominator. So for
this we need to calculate factorial and substitute it to main sum.
Input Output Processing Header file
Int i,j; Float sum Y=sum+num/denom Stdio.h
Float sum

b) Algorithm
1. Start
2. Find factorial of number upto 10 with excluding prime
number.
3. Define the sum by keeping numerator as square and
denominator as factorial of number.
4. Find sum and display it.
5. End.
Flowchart

Start

Declare I,j,num,denom,sum,prime

Num=i*I;
Denom*=j

for ( j = 2; j <= i/2; j++)

No
if (i % j == 0)

yes
Prime=0;

no
If(prime==0)

yes

Sum += num/denom;

Display sum

End
c) Code
#include<stdio.h>
int main() {
int n,num;
float denom, sum=1;
printf("Enter a number:"); scanf("%d", &n);
printf("The series is:\nY=1+");
for (int i = 1; i <= n; i++) {
denom = 1;
for (int j = 1; j <= i; j++) {
denom *= j;
}
num = i * i;
int prime= 0;
int j;
if (num > 1) {
prime = 1;
for ( j = 2; j <= i/2; j++) {
if (i % j == 0) {
prime = 0;
break;
}
}
}
if (prime == 0) {
sum += num/denom;
printf("%d/%.2f + ", num, denom);
}
}
printf("\n-------------------------------------------\n");
printf("Y=%.2f\n", sum);
return 0;
}
d) Output

e) Conclusion
This program displays the series of the numerator and denominator such
that the numerator is square of number and denominator is its factorial less than
10. This program helps in understanding of the concepts of loops and branching
too. The program also includes only composite number sum.

2. Write a program to input two integer numbers and display the sum of even
numbers between these two input numbers.

a) program analysis

This program includes the basic use of the loop and comparision of
increments between numbers. For this we need int data types and simple block of
loop.

Input Output Processing Header file


Int a,b,I; Int sum Sum+=I; Stdio.h

b) Algorithm
(1) Start
(2) Input two numbers.
(3) Find even between numbers and add them.
(4) Display sum
(5) End
Flowchart
start

Input two numbers

For(i=a;i<b;i++)

no
If(i%2==0)

yes
Sum+=I;

Display sum

End

c) Code
#include<stdio.h>
int main(){
int a,b,i,sum=0;
printf("Enter number :"); scanf("%d%d",&a,&b);
for(i=a+1;i<b;i++){
if(i%2==0){
sum+=i;
}
} printf("sum of even number between %d and %d =%d",a,b,sum);
}
d) Output

e) Conclusion
This program is about the use of the loop and the condition
structure. We can use simple loop with the condition structure to check the
number as it is odd or even. This program helps to understand the basic of loops
and printing the sum.

3) write a program to find the GCD (Greatest common divisor) and LCM
(Lowest Common Multiple) of two numbers.
a. Problem Analysis
This is the problem of finding greatest common divisor and Lowest
common multiple of the two-input number. In this we need two variables to dtore
the numbers and do operation on it. And the loop will be used to check the
condition for useful output.
Input Output Processing Header file
Int a,b; Int GCD,LCM; Int rem; Stdio.h
Num=a or b;
Denom=aor b;
While(rem!=0){
LCM=a*b/GCD;
}

b. Algorithm
1. Start
2. Input two integer and store in a and b.
3. Compare two number, store greater in num and smaller in
denom.
4. Run while to get rem as 0 and swap the numerator and
denominator with rem.
5. Assign the denom as GCD and LCM as a*b/GCD.
6. Display GCD and LCM.
7. End.
Flowchart

Start

Input two integer in a and b

If (a>b) no

yes

Num=b;denom=a;
Num=a;denom=b;
Rem=num%denom;
Rem=num%denom;
Swap num, denom and rem;
Swap num, denom and rem;
GCD=denom;
GCD=denom;
LCM=a*b/GCD;
LCM=a*b/GCD;

Display GCD and LCM

c. Code
#include<stdio.h>
int main(){
int a,b,num,denom,GCD,LCM, rem;
printf("Enter two numbers:"); scanf("%d%d",&a,&b);
if(a>b){
num=b;
denom=a;
} // num=(a>b)?b:a;
else{
num=a;
denom=b;
} //denom=(b>a)?b:a;
rem=num%denom;
while(rem!=0){
num=denom;
denom=rem;
rem=num%denom;
}
GCD=denom;
LCM=a*b/GCD;
printf("input numbers are: %d and %d\nGCD = %d\n LCM=%d",a,b,GCD,LCM);
return 0;
}
d. Output

e. Conclusion
This program is about finding HCF and LCM of two input numbers.
This program enhances the use of loop with conditional bunch of code.

4) Write a program to display Fibonacci series of last up to 100.


a. Program analysis
this is the problem related to display the Fibonacci series. The
Fibonacci series is the series in which third term is sum of first two terms and so on.
Here we need two fix number to start the series as 0 and 1.
Input Output Processing Header file
Int n=100 0,1,1,2,3,5,8,…… For(i=3;i<n;i++){ Stdio.h
Printf(“%d”,c);
a=b;
b=c;
c=a+b;
}
2. Algorithm
1. Start
2. Declare variables a,b,n,I, sum.
3. Input number to be printed.
4. Calculate Fibonacci series.
5. Display the series.
6. End
Flowchart
Start

Input a, b,I and n=100

no
For(I=3;i<=n;i++)

yes

a=b;
b=c;
c=a+b

Display c

Stop
c. Code
#include<stdio.h>
int main(){
int n=100,i;
int a=0,b=1;
int c=a+b;
printf("Fibonacci series is:\n %d, %d,",a,b);
for(i=0;i<=n;i++){
if(c<=100){
printf("%d, ",c);
a=b;
b=c;
c=a+b;
}
}
return 0;
}

d. result

e. Conclusion
This is the program to display the fibonacci series upto 100 using loop. This
program enhances our knowledge regarding the use of loop in c language.

5) Write a program t display flag of Nepal using the symbol/HEX character in c.


a. Program analysis
this problem is related to display the sequential objects using c. to
encounter this, we can use loop and nested loop with appropriate output print
function.

Input Output Processing Header file


Int n; Symbol of flag of For loop and Stdio.h
Nepal nested for loop
b. Algorithm
1. Start
2. Use the loop to display the flag.
3. Print “*”.
4. End.
Flowchart

Start

Input the values of row as n.

For(i=1;i<=n;i++){
For(j=1;j<=I;j++){
Printf(“*”);
}
}
{

Display “*”

End

c. Code
#include<stdio.h>
int main(){
int n;
printf("Enter the value of row:"); scanf("%d",&n);
for(int i=1;i<=2;i++){
for(int i=1;i<=n;i++){
for(int j=1;j<=i;++j){
printf("*");
}
printf("\n");
}
}
for(int i=1;i<=2;++i){
printf("*\n");
}
return 0;
}
d. Output

e. Conclusion
This is the program for displaying the flag of Nepal using symbol star ‘*’. This
problem was solved using for loop with nested loop.
6.

You might also like