You are on page 1of 6

7.

Implement using functions to check whether the given number


isprime and display appropriate messages (no built in math function)

Algorithm:

Step 1: [Start] Begin


Step 2: [Input the integer number] Read num
Step 3: [Invoke function isprime(num)]
res = isprime(num)
Step 4: [Output]
If (res = 1)
Print(“Prime numbers between range a and b”)
Else
Print(“No prime numbers exists”)
Step 5: [Stop]

isprime(num)

For i = 2 through num / 2 in steps of 1


If ( ( num mod i) = 0) then Return 0
Return 1 End For
Flowchart:
Program:
#include<stdio.h>
int isPrime(int);

void main()
{
int res,num;
clrscr();
printf("Enter the number:");
scanf("%d",&num);
res=isPrime(num);
if(res==0)
printf("%d is not a Prime Number\n",num);
else
printf("%d is a Prime Number\n",num);
}

int isPrime(int num)


{
int i;
if(num==0 || num==1)
return 0;
for(i=2;i<=num/2;i++)
{
if(num %i == 0 )
{
return 0;
}
}
return 1;
}
11. Develop a program to sort the given set of N numbers using Bubble
sort.

Algorithm:
Step 1: [Start]
Begin
Step 2: [Input the number of elements in the array]
Read n
Step 3: [Input the n integer numbers]
For i = 1through n in steps of 1
Read a[ i ]
End for
Step 4: [Display the original array]
For i = 0 through n-1 in steps of 1
Print a[ i]
End for
Step 5: [Repeat step 5 for i varies from 0 thru n-1]
For j = 0 through n-1 in steps of 1
For i = 0 through n-j-1 in steps of 1
IF ( a[i] > a[i + 1] ) then
temp= a[i]
a[i] = a[i + 1]
a[i+1] = temp
End if
End for
End for
Step 6: [Output the sorted array]
For i = 0 thru n-1 in steps of 1
Print a[i]
End for
Flowchart:
Program:
#include<stdio.h>
void main()
{
int a[30],i,j,n,temp;
printf("Enter number of elements : ");
scanf("%d",&n);
printf("Enter %d elements : ",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Before sorting the elements :");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}

for(j=0;j<n-1;j++)
{
for(i=0;i<n-j-1;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("\nAfter sorting the elements :");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

You might also like