-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab 2_Prog 1.c
107 lines (107 loc) · 1.85 KB
/
Lab 2_Prog 1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Q1 to Prime Check using 3 different algorithms
#include <stdio.h>
#include<math.h>
int flag=1;
int method1(int n)
{
int steps=0;
if(n==1)
{
steps++;
flag=0;
return steps;
}
else
{
steps++;
for(int i = 2; i<sqrt(n);i++)
{
steps++;
if(n%i==0)
{
steps++;
flag=0;
return steps;
}
steps++;
}
steps++;
}
return steps;
}
int method2(int n)
{
int steps=0;
if(n==1)
{
steps++;
return steps;
}
else
{
steps++;
for(int i=2;i*i<=n;i++)
{
steps++;
if(n%i==0)
{
steps++;
return steps;
}
}
}
return steps;
}
int method3(int n)
{
int steps=0;
if(n==1)
{
steps++;
flag=0;
return steps;
}
else
{
steps++;
for(int i = 2; i <=n/2;i++)
{
steps++;
if(n%i==0)
{
steps++;
flag=0;
return steps;
}
steps++;
}
steps++;
}
return steps;
}
int main()
{
int t=10,a[10];
printf("Enter the input elements:");
for(int i=0;i<t;i++)
scanf("%d",&a[i]);
printf("No\tInput\t\tALGO1\t\tALGO2\t\tALGO3 \tPrime or Not\n");
int j=0;
while(t--)
{
printf("%d\t%d\t",j+1,a[j]);
int count1=method1(a[j]);
printf("\t%d\t",count1);
int count2=method2(a[j]);
printf("\t%d\t",count2);
int count3=method3(a[j]);
printf("\t%d\t",count3);
if(flag==1)
printf("Prime\n");
else
printf("Not Prime\n");
flag=1;
j++;
}
return 0;
}