Download as pdf or txt
Download as pdf or txt
You are on page 1of 75

Jatiya Kabi Kazi Nazrul Islam University

Trishal, Mymensingh

Lab Report.

Course name: Computer Fundamentals and Programming Basic Lab

Course code:CSE-102

Department of Computer Science and Engineering

Submitted to:

Rubya Shahrian
Assistant Professor

Department of Computer Science and Engineering.

Submitted By:

Ariful Islam

Roll:19102039

Session:2018-19

Reg no:7759

Department of Computer Science and Engineering.


Problem no: 1

Problem Name: Find the greatest among three numbers.

Algorithm:

Step 1:Start.

Step 2:Input 3 numbers a,b and c.

Step 3:Check if a>b

Step 4:If true,check a>c

1:If true,a is the greatest

2:If false,c is the greatest

Step 5:If false,check b>c

1:If true,b is the greatest

2:If false,c is the greatest

Step 6:Stop.
Flow chart:

Start

Read a,b ,c

No Yes
Is
a>b
Yes No No Yes
Is a>c
Is
b>c

b c a
greatest greatest greatest

Stop

Code:

#include <stdio.h>

int main()

int a,b,c;

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

if(a>b)

if(a>c) printf(“%d is the greatest”,a);


else printf(“%d is the greatest”,c);

else

If(b>c) printf(“%d is the greatest”,b);

else printf(%d is the greatest”,c);

Return 0;

Result:

9 is the largest

Discussion:

In this experiment I have experienced the use of nested if..else.This make me understood how to
use logical nested if…else operation which helped me to make short my code.
Problem no: 2

Problem name: A program to display Triangle.

Algorithm:

step1 : Start
step2 : Read number num
step3 : [initialize]
x=1
step4 : Repeat step 4 through 10 until num>=x
step5 : [initialize]
y=1
step6 : Repeat step 6 through 8 until y<=n
step7 : print *

step8 : x=x+1

step9 : go to next line

step10: y=y+1

step11 : stop
Flowchart:

#include<stdio.h>

int main() {

int x,y,n;

scanf("%d", &n);

for(x = 0; x < n; x++) {

for(y = 0; y <= x; ++y) {

printf("* ");

printf("\n");

return 0;

}
Result:

**

* **

Discussion:

This experiment has experienced me the use of while loop with the logic of printing triangle
shape.This learning will help me to understand the mechanism of printing different shapes.

Problem No: 3

Problame Name: A program to display Pyramid

Algorithm:

step1: Start
step2: Read number num
step3: [initialize]
r=1
step4: Repeat step 4 through 10 until num>=r
step5: [initialize]
c=1
step6: Repeat step 6 through 8 until c<=r
step7: print */#
step8: c=c+1
[end of loop step6]
step9: go to next line
step10: r=r+1
[end of loop step4]
step11: stop

Flowchart:

Code:

#include <stdio.h>

int main() {

int x,y,n;

printf("Enter the number of rows: ");

scanf("%d", &n);

for (x = n; x >= 1; --x) {

for (y = 1; y <= x; ++y) {


printf("* ");

printf("\n");

return 0; }

Result:

* *

* * *

Discussion:

This experiment has experienced me the use of while loop with the logic of printing triangle
shape.This learning will help me to understand the mechanism of printing different shapes.

Problem No: 4

Problem Name: Find the roots of ax2+bx+c

Algorithm:

Step 1. Start

Step 2. Read the coefficients of the equation, a, b and c from the user.

Step 3. Calculate discriminant = (b * b) – (4 * a * c)

Step 4. If discriminant > 0:

4.1: Calculate root1 = ( -b + sqrt(discriminant)) / (2 * a)

4.2: Calculate root2 = ( -b - sqrt(discriminant)) / (2 * a)

4.3: Display “Roots are real and different”


4.4: Display root1 and root2

Step 5: Else if discriminant = 0:

5.1: Calculate root1 = -b / (2 *a)

5.2: root2 = root1

5.3: Display “Root are real and equal”

5.4: Display root1 and root2

Step 6. Else:

6.1: Calculate real = -b / (2 * a)

6.2:Calculate imaginary = sqrt(-discriminant) / (2 * a)

6.3: Display “Roots are imaginary”

6.4: Display real, “±” , imaginary, “i”

Step 7:Stop

Flowchart:
Code:

#include <math.h>

#include <stdio.h>

int main() {

double a, b, c, discriminant, root1, root2, realPart, imagPart;

printf("Enter coefficients a, b and c: ");

scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;
if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("root1 = %.2lf and root2 = %.2lf", root1, root2);

else if (discriminant == 0) {

root1 = root2 = -b / (2 * a);

printf("root1 = root2 = %.2lf;", root1);

else {

realPart = -b / (2 * a);

imagPart = sqrt(-discriminant) / (2 * a);

printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart, realPart, imagPart);

return 0;

Result:

Enter coefficients of a,b and c:

2.3

4
5.6

root1 = -0.87+1.30i and root2 = -0.87-1.30i

Discussion:

This experiment has experienced me to solving the math equation with computer programming.
In this program, the sqrt() library function is used to find the square root of a number.

Problem No: 5

Problem Name:

Algorithm:

Step 1: Start

Step 2: You've been given the variable celsius, which represents a temperature in degrees Celsius

Step 3: Assign the Fahrenheit ( ) to the already specified variable


Fahrenheit.

Step 4: A temperature that is the same as the temperature in Celsius.

Step 5: To convert the Celsius temperature to Fahrenheit, use the above-mentioned procedure.

Step 6: Stop

Flowchart:
Code:

#include<stdio.h>

void main()

float celsius,fahrenheit;

// Reads temperature in fahrenheit

printf("\nEnter temperature in Fahrenheit:");

scanf("%f",&fahrenheit);

// Fahrenheit to celsius conversion formula

celsius=(fahrenheit - 32)*5/9;

// Print the result

printf("\nCelsius = %.3f",celsius); //.3f means correct to 3 decimal places

Result:

Enter temperature in Farenhite:


98.0

Celcious:37.00

Discussion:

This experiment experienced me to play with number by c programming. By this program I have
learnt to convert temperature wich will help me to apply this procedure in any sector.

Problem No: 6

Problem Name: Find grade using if…else


Algorithm:

Step 1 : start

Step 2 : read marks or Percentage

Step 3 : if marks >= 80 then grade =A, go to step 7

aAStep 4 : if marks >= 60 and marks <=80 then grade = B, go to step 7

Step 5 : if marks >=40 and marks <=60 then grade = C go to step 7

Step 6 : display failed

Step 7 : display grade.

Step 8 : stop.

Flowchart:

Start

Stop
Code:

#include <stdio.h>

int main(void)

int num;

printf("Enter your mark ");

scanf("%d",&num);

printf(" You entered %d Marks n", num); // printing outputs

if(num >= 80){

printf(" You got A grade n"); // printing outputs

else if ( num >=60){ // Note the space between else & if

printf(" You got B grade n");

else if ( num >=40){

printf(" You got C grade n");

else if ( num < 40){

printf(" You Failed in this exam n");

printf(" Better Luck Next Time n");

return 0;

Result:
Enter your mark:

60

You got B grade.

Discussion:

This problem experienced me find the grade which help me to find out definite result from multiple
choice.I have used if else statement to complete this procedure.

Problem No: 7

Problem Name: Find grade using switch

Algorithm:

Step 1 : start

Step 2 : read marks or Percentage

Step 3 : if marks >= 80 then grade =A, go to step 7

Step 4 : if marks >= 60 and marks <=80 then grade = B, go to step 7

Step 5 : if marks >=40 and marks <=60 then grade = C go to step 7

Step 6 : display failed

Step 7 : display grade.

Step 8 : stop.

Flowchart:
Star
t

Stop
Code:

#include<stdio.h>
int main()
{
int score;

printf("Enter score( 0-100 ): ");


scanf("%d", &score);

switch( score / 10 )
{

case 10:
case 9:
printf("Grade: A");
break;

case 8:
printf("Grade: B");
break;

case 7:
printf("Grade: C");

break;

case 6:
printf("Grade: D");
break;

case 5:
printf("Grade: E");
break;

default:
printf("Grade: F");
break;

return 0;
}
Result:

Enter Score:

90

Discussion:

This problem experienced me find the grade which help me to find out definite result from multiple
choice.I have used if else statement to complete this procedure.

Problem No: 8

Problem Name: Use of break statement

Algorithm:

Step1:Start

Step2:Start loop

1.if true,break

2.if false,step 2

Step3:Stop

Flowchart:

Loop body start

False True
Condi
tion break
to
break
Code:

#include <stdio.h>

int main() {

int i;

double number, sum = 0.0;

for (i = 1; i <= 10; ++i) {

printf("Enter n%d: ", i);

scanf("%lf", &number);

if (number < 0.0) {

break;

sum += number;

printf("Sum = %.2lf", sum);

return 0;

Result:

3
4

Sum=15

Discussion:

This experiment has experienced me the use of break statement.Break statement is a logical statement
which stop the execution of program with given logic by programmer.

Problem No: 9

Problem Name: Use of continue statement

Algorithm:

Step1:start

Step2:Start loop

1.if false,exit loop

2.if true,continue

Step3:stop

Flowchart:
Code:

#include <stdio.h>

int main() {

int i;

double number, sum = 0.0;

for (i = 1; i <= 10; ++i) {

printf("Enter a n%d: ", i);

scanf("%lf", &number);

if (number < 0.0) {

continue;

sum += number;

printf("Sum = %.2lf", sum);

return 0;

Result:

2.4 6 7 8 9 0 6 4 3 6 8.8 8

29.7
Discussion:

This experiment has experienced me the use of continue statement.Continue statement is a logical
statement which the execute the program with given logic by programmer.

Problem No: 10

Problem Name: use of goto statement

Algorithm:

Step1: start

Step2: 1.Level1

2.Level2

3.Level3

Step3:goto Level3

Step4:execution of statement 3

Step5:Stop.

Flowchart:

Code:

#include <stdio.h>

int main()

{
int sum=0;

for(int i = 0; i<=10; i++){

sum = sum+i;

if(i==5){

goto addition;

addition:

printf("%d", sum);

return 0;

Result:

15

Discusssion:

This experiment ecperienced me on about goto statement. GoTo (goto, GOTO, GO TO or other case
combinations, depending on the programming language) is a statement found in many computer
programming languages. It performs a one-way transfer of control to another line of code; in contrast a
function call normally returns control.

Problem No: 11

Problem Name: Find the range of series of values.

Algorithm:

Step1:start
Step2:Input the start and end numbers.

Step3:Initialize sum = 0.

Step4:Repeat from i = start to and continue until i = end.

Step5:sum = sum + i

Step6:Print "sum"

Step7:stop

Flowchart:

Code:

#include <stdio.h>

Int main()

Int I,s=0,n;

scanf(“%d”,&n);

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

s=s+I;
}

Printf(“%d”,s);

Return 0;

Result:

15

Discussion:

This experiment has experienced me on how to find range of the series of the value by using for loop.

Problem no: 12

Problem Name: Calculate Interest

Algorithm:

Step1:Start

Step2:Read the value of P,r and n

Step3:calculate I=prn

Step4:show the interest I

Step5:stop
Flowchart:

Start

Read P,r and n

Calculate I=Prn

Print I

Stop

Code:

#include <stdio.h>

Int main()

float P,I,r,n;

scanf(“%f %f %f”,&P,&r,&n);

P=Irn;

Printf(“%f”,I);

return 0;

}
Result:

1000

0.1

100

Discussion:

This experiment has experienced me on how to calculate interest using simple interest equation I=Prn.

Problem No: 13

Problem Name: A program to evaluate the series 1+x+x2+x3+…….

Algorithm:

Step1:start

Step2:read the value x and n;

Step3:for i=,i<n,calculate a=pow(x,i) and s=s+i

Step4:print the value s

Step5:stop
Flowchart:

start

Read x,n

Yes No For(i=
0;i<n)

a=pow(x,i)

s=s+a

Print s

stop

Code:

#include <stdio.h>

Int main()

Int x,n,a,i,s=0;

Scanf(%d %d”,&x,&n);
for(i=0;i<n;i++)

a=pow(x,i);

s+=a;

printf(“%d”,s);

return 0;

Result:

2 3

Discussion:

This experiment has experienced me on how to calculate the multiple power series value by using for
loop.This will help me to proper control on for loop.

Problem No: 14

Problem Name: Write a program to process loan application and to sensation loan.

Algorithm:

Step1:Start

Step2:read loan1 and loan2

Step3:if loan1 and loan2 bcemoe true

1.can not proceed

Step4:if false

1.proceed
Step5:stop.

Flowchart:

start

Read loan1 and loan2

If(loan
&&loa
n2)

Can apply for loan Can not apply for


loan
Max loan=deposite-loan1

Print max loan

start
Code:

#include <stdio.h>

Int main()

Int loan1,loan2,max loan;

Scanf(“%d %d”,&loan1,&loan2);

If(loan1&&loan2)

Printf(“cant apply for loan”);

Else

Printf(“cant apply for loan”);

Return 0;

Result:

Apply for loan

Discussion:

This experiment has experienced me on binary operation with if else logical statement which will help to
make down any logic
Problem No: 15

Problem Name: Write a program to print ODD numbers from 1 to N using while loop.

Algorithm:

Step 1: Start .

Step 2: Read two integer value

Step 3: Input n

Step 4: set x=1

Step 5 :Repeat step 5.1 to 5.3 while (x<=n)

Step 5.1: If(x%2!=0)

Step 5.2: Then print x

Step 5.3: x=x+1

Step 6:End

Flowchart:

Start
x=1

Input n

No
x<=n End

Yes

No
x=x+1 x%2!=0

Yes

Program code:
Print x
#include<stdio.h>

int main()

{
int x=1,n;

printf("Enter a number:");

scanf("%d",&n);

while(x<=n){

if(x%2 != 0)

printf("%d\n",x);

x++;

return 0;

Results :

Input: 7

Output : 1 3 5 7

Discussion : We have successfully run a program which have displayed odd numbers

Problem No: 16

Problem Name: Write a program to print EVEN numbers from 1 to N using while loop.

Algorithm:

Step 1: Start .
Step 2: Read two integer value

Step 3: Input n

Step 4: set x=1

Step 5 :Repeat step 5.1 to 5.3 while (x<=n)

Step 5.1: If(x%2==0)

Step 5.2: Then print x

Step 5.3: x=x+1

Step 6:End

Flowchart:

Start

x=1

Input n
Program code:

#include<stdio.h>

int main()

int x=1,n;

printf("Enter a number:");

scanf("%d",&n);

while(x<=n){

if(x%2 == 0)

printf("%d\n",x);
x++;

return 0;

Results :

Input: 10

Output : 2 4 6 8 10

Discussion : We have successfully run a program which have displayed even numbers

Problem No: 17

Problem Name: Write aprogram to print all uppercase alphabets using while loop

Algorithm:

Step 1: Start .

Step 2: Read one character type data

Step 3: set alphabets= ‘A’

Step 4: Print alphabets while(alphabets= ‘Z’)

Step 5: alphabets=alphabets+1

Step 6:End
Flowchart:

Start

alphabets= ‘A’

No
Alphabets<= End
‘Z’

Yes
alphabets=alphab
Print alphabets
ets+1

Program code:

#include<stdio.h>

int main()

char alphabets='A';

printf("all uppercase alphabets:");

while(alphabets<='Z')

printf("%c\n",alphabets);

alphabets++;

Results :

Output : A B C D E F G H J K L M N O P Q R S T U V W X Y Z

Discussion : We have successfully run a program which have displayed uppercase alphabets
Problem No: 18

Problem Name: Write aprogram to print all lowercase alphabets using while loop

Algorithm:

Step 1: Start .

Step 2: Read one character type data

Step 3: set alphabets= ‘a’

Step 4: Print alphabets while(alphabets= ‘z’)

Step 5: alphabets=alphabets+1

Step 6:End
Flowchart:

Start

alphabets= ‘z’

No
Alphabets<= End
‘z’

Yes

alphabets=alphab
Print alphabets
ets+1
Program code:

#include<stdio.h>

int main()

char alphabets='a';

printf("all uppercase alphabets:");

while(alphabets<='z')

printf("%c\n",alphabets);

alphabets++;

return 0;

Results :

Output : a b c d e f g h I j k l m n o p q r s t u v w x y z

Discussion : We have successfully run a program which have displayed lowercase alphabetes
Problem No: 19

Problem Name: Write a program to print numbers from 1 to N using for loop.

Algorithm:

Step 1: Start
Step 2: Read two integer type value
Step 3: Input n
Step 4: Set =1
Step 5: Repeat step 6&7 until x<=n reach
Step 6: Print x
Step 7: Compute x=x+1
Step 8: End
Flowchart

Start

Input n

x=1

False
x<=n End

True

Print x

x=x+1

Program code:
#include<stdio.h>

int main()

int x,n;

printf("Enter a number off n :");

scanf("%d",&n);

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

printf("%d\n",x);

return 0;

Results :

Input: 10

Output : 1 2 3 4 5 6 7 8 9 10

Discussion : We have successfully run a program which have displayed numbers

Problem No: 20

Problem Name: Write aprogram to print numbers using while loop


Algorithm:

Step 1: Start .

Step 2: Read one integer type value

Step 3: set x=1

Step 4: Print numbers while(x<=10)

Step 5: x=x+1

Step 6:End

Flowchart:

Start

x=1
No
End
x<=10

Yes

x=x+1
Print x

Program code:

#include<stdio.h>

int main()

int x=1;

printf("Enter number :");

while( x<=10)
{

printf("%d\n",x);

x++;

return 0;

Results :

Input: 10

Output : 1 2 3 4 5 6 7 8 9 10

Discussion : We have successfully run a program which have displayed numbers from 1 to n

Problem No: 21

Problem Name: write program to print square, cube and square root of all numbers from 1 to n

Algorithm:

Step 1: Start .

Step 2: Read four integer type value and one float type value

Step 3: set c=1,s=1 and x=1

Step 4: Repeat step 4.1 to 4.5 Until x<=n

Step 4.1: s=x*x


Step 4.2: c=x*x*x

Step 4.3: r=sqrt((double) x)

Step 4.4:Print s,c,r

Step 4.5: x=x+1

Step 5:End

Flowchart :
Start

Input n

x=1

Fasle
x<=n End
True

s=x*x

c=x*x*x

r=sqrt((double) x)

x=x+1 Print s,c,r

Program code:

#include<stdio.h>

#include<math.h>

int main()

int x,n,s=1,c=1;

float r;

printf("Enter a number of n:");

scanf("%d",&n);

printf("Square Cube Scuareroot");

for(x=1;x<=n;x++)
{

s=x*x;

c=x*x*x;

r=sqrt((double) x);

printf("%d %d %.2f\n",s,c,r);

return 0;

Results :

Input: 3

Output : Square Cube Scuareroot

1 1 1.00

4 8 1.41

9 27 1.73

Discussion : We have successfully run a program which have displayed square ,cube and
squareroot from 1 to n

Problem No: 22

Problem Name: Write a program to read age of 15 person and count total Baby age, School age and Adult
age
Algorithm:

Step 1: Start .

Step 2: Read five integer type value

Step 3: set count=0

Step 4: Print numbers while(x<15)

Step 5: x=x+1

Step 6:End

Flowchart :
Start

count =0

Print total number


of baby scl and adul No
count< count++
person
15

Yes

Input age
End

Yes
If age>=0and Baby++
age<=5

No
yes
If
scl++
age>=0and
age<=5

No

Program code: adult ++

#include<stdio.h>

int main()

int age;

int cnt_baby=0,cnt_school=0,cnt_adult=0;

int count=0;

while(count<15)

printf("Enter age of person[%d]:",count+1);

scanf("%d",&age);

if(age>=0 && age<=5)

cnt_baby++;

else if(age>=6 && age<=17)

cnt_school++;

else

cnt_adult++;
count++;

printf("Baby age: %d\n",cnt_baby);

printf("School age: %d\n",cnt_school);

printf("Adult age: %d\n",cnt_adult);

return 0;

}
Result :

Please input age of 15 person

4 5 3 7 8 44 34 3 6 9 13 12 15 2 6

Total baby = 7

Total school age person = 6

Total adult person = 2

Discussion : This program successfully takes 15 inputs from user and print the number of
baby age ,school age and adult people among the inputs .
Problem No: 23

Problem Name: Write a Program to find factorial of a number.

Algorithm:

Step 1: Start .

Step 2: Read three integer type value

Step 3: set x=1,f=1

Step 4: Input n

Step 5: Repeat 5.1 to 5.2 until x<=n

Step 5.1: f=f*x

Step 5.2: x=x+1

Step 6: Print f

Step 7:End
Flowchart:

Start

Input n

x=1,f=1

False
x<=n Print f

True

End
x=x+1 f=f*x
Program code:

#include<stdio.h>

int main()

int x,n,f=1;

printf("Enter any number of n:");

scanf("%d",&n);

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

f=f*x

printf("Factorial is:%d\n",f);

return 0;

Results :

Input: 4

Output : Factorial is 24

Discussion : We have successfully run a program which have displayed factorial from 1 to n.

Problem No: 24
Problem Name: Write a Program to find sum of first N natural number, N must be taken by user

Algorithm:

Algorithm :

Step 1: Start.

Step 2: Read the value of n.

Step 3: Assign sum =0,x=1.

Step 4: Calculate sum = sum+i.

Step 5: Increase sum by 1.

Step 6: Repeat Step 4 and 5 until i is less than or equal to n.

Step 7: End.

Flowchart:
Start

Input n
x=1,sum=0

False
x<=n Print sum

True

End
x=x+1 sum=sum+x

Program code:

#include<stdio.h>

int main()

{
int x,n;

int sum=0;

printf("Enter a number off n :");

scanf("%d",&n);

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

sum += x;

printf("%d\n",sum);

return 0;

Result :

Please input a number to see sum of 1 to

number: 10 sum of 1 to 10 is = 55

Discussion : This program successfully takes a input from the user and print the total
summation from 1 to n .
Problem No. : 25

Problem Name : Write a C program to print all leap years from 1 to N.

Algorithm:

Step 1: Start .

Step 2: Take a input n.

Step 3: initialize a for loop which will print all the leap years from 1 to n.

Step 4: End.
Flowchart:

Program code:
#include <stdio.h>

int main()

int n,x;

printf("Enter the value of N: \n");

scanf("%d",&n);

printf("Leap years from 1 to %d:\n",n);


for(i=1;i<=n;i++){
if( (i % 400==0)||(i%4==0 && i%100!=0) ){

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

return 0;

Result :

Enter the value of N: 10

Leap years from 1 to 10:

Discussion : This program successfully takes input from the user and print leap
years from 1 to n.

Problem No. : 26
Problem Name : Write a c program to print all the Even and Odd numer from 1 to N.

Algorithm :

Step 1: Start.

Step 2: Take a input n.

Step 3: print a statement.

Step 4: Initialize a for loop which will print all the odd numbers from 1 to n.

Step 5: Print a statement.

Step 6: Initialize a for loop which will print all the even numbers from 1 to n.

Step 7: End.
47
Flowchart:
Program code:

#include<stdio.h>

int main()

int n,i;

printf("Enter any number of n:");

scanf("%d",&n);

printf("Even numbers are:\n");

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

if(i%2==0){

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

printf("Odd numbers are:\n");

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

if(i%2!=0){

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

return 0;

}
Result :

Please input any number

All ODD number between 1 and 8 are written below:

1 357
All EVEN number between 1 and 8 are written below:

2 468
Discussion: The program takes a number as a input from the user and prints all the even
and odd numbers from 1 to the input number successfully.

Problem No: 27

Problem name: Write a C program to print all prime numbers 1 to N.


Algorithm:

Step 1: Start.
Step 2: Read n value.
Step 3: Initialize count = 0
Step 4:for i = 2 to n
Step 4.1: for j = 1 to i
Step 4.2 : if i % j = 0
Step 4.3 : then increment count
Step 4.4 : if count is equal to 2
Step 4.5 : then print i value
Step 5: End.

Start
Flowchart:

Count=0

Read n
No
for(i=1;i<=n;i++

Yes

No
for(j=2;j<=i\2;j++

Yes

no
No
if(i%j== if(count=
0 =0

yes yes

Print I value
count++

End

Program code:

#include<stdio.h>
int main(){
int i, num, n, count;
printf("Enter the range: \n");
scanf("%d", &n);
printf("The prime numbers in between the range 1 to %d:",n);
for(i = 1;i<=n;i++){
count = 0;
for(j=2;j<=i/2;i++){
if(i%j==0){
count++;
break;
}
}
if(count==0 && i!= 1)
printf("%d ",i);
}
return 0;
}

Result:
Enter the range: 20
The prime numbers in between the range 1 to 20: 2 3 5 7 11 13 17 19

Discussion : We have successfully run a program which have displayed the


the prime numbers in 1 to N.

You might also like