-
Notifications
You must be signed in to change notification settings - Fork 97
/
NumberAnalysis.c
304 lines (252 loc) · 6.9 KB
/
NumberAnalysis.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <stdio.h>
//1
int prime(long int n);
//11-----------------------
int getReverse(int num);
int getSumOfDigit(int num);
void main()
{
//1-----------------------------------------------
int n,res=0,i;
printf("\nENTER A NUMBER: ");
scanf("%d",&n);
res=prime(n);
if(res==0)
printf("\n%d is prime.\n",n);
else
printf("\n%d is not prime.\n",n);
//2-----------------------------------------------
int num=n;
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.\n", num);
else
printf("%d is odd.\n", num);
//3----------------------------------------------
int reversed = 0, remainder, original;
original = n;
// reversed integer is stored in reversed variable
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
// palindrome if orignal and reversed are equal
if (original == reversed)
printf("%d is a palindrome.\n", original);
else
printf("%d is not a palindrome.\n", original);
//4-------------------------------------------------
int number=original,flag=0;
for(i = 0; i <= number/2; i++)
{
if(number == i*i)
{
printf("%d is a perfect square\n", number);
flag=1;
break;
}
}
if(flag== 0)
printf("%d is not a perfect square\n", number);
//5------------------------------------------------
int originalNum, result = 0;
originalNum = number;
while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;
result += remainder * remainder * remainder;
// removing last digit from the orignal number
originalNum /= 10;
}
if (result == number)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
printf("\n");
//6-----------------------------------------------------
n=number;
i=0;
flag = 0;
printf("The divisors of %d are: 1",n);
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
printf(",%d",i);
flag = 1;
}
// flag is 0 for prime numbers
}
printf("\n");
if (flag == 0)
printf("%d is a prime number.\n", n);
//7----------------------------------------------------------
long int decimalNumber=n;
printf("Equivalent hexadecimal number is: %ld",decimalNumber);
printf("\n");
//8-----------------------------------------------------------
long int quotient=0;
decimalNumber=n;
remainder=0;
int binaryNumber[100],j;
i=1;
quotient = decimalNumber;
while(quotient!=0) {
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
printf("Equivalent binary value of decimal number %ld: ",decimalNumber);
for (j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
printf("\n");
//9--------------------------------------------------------------------
printf("Equivalent octal number is: %ld",decimalNumber);
printf("\n");
//10-----------------------------------------------------------------------
i=0, flag=0;
printf(" ");
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// print 3rd to nth terms
for (i = 3; i <= n; ++i)
{
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
if(n==nextTerm)
flag=1;
if(n<nextTerm)
break;
}
if (flag==1)
printf("%d The no is fibonacci ",n);
else
printf("%d The no is not fibonacci",n);
printf("\n");
//11---------------------------------------------------------------------------
num=n;
int digitSum=0, reverse=0;
/* get sum of digits of input number */
digitSum = getSumOfDigit(num);
/* reverse the digits of digitSum*/
reverse = getReverse(digitSum);
if ((digitSum * reverse) == num) {
printf("%d is a Magic Number\n", num);
} else {
printf("%d is not a Magic Number\n", num);
}
//12--------------------------------------------------------------------
n=n;
i=0;
int sum = 0;
for(i=1;i<=n;i++)
{
sum = sum + i;
}
printf("The sum of first %d numbers is %d\n",n,sum);
//13-------------------------------------------------------------------------
int rres=0;
i=0;
int pi=0;
int disp=0;
printf("Enter 1 if you wish to see the list of primes till %d:",n);
scanf("%d",&disp);
if(disp==1)
{
if(n>2)
{
printf("2");
pi=1;
}
if(n>3)
{
printf(",3");
pi=2;
}
if(n>5)
{
printf(",5");
pi=3;
}
}
for(i=2;i<=n;i++)
{
if((i%2!=0)&&(i%3!=0)&&(i%5!=0))
{
//printf("[RES pre= %d ]",rres);
rres= prime(i);
//printf("| %d, res=%d |\n", i,rres);
if(rres == 0)
{
pi=pi+1;
if(disp==1)
printf(",%d",i);
}
rres=0;
}
}
printf(".\n");
printf("Total no primes =%d \n", (pi));
//14---------------------------------------------
int m;
n=n;
sum=0;
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
n=number;
printf("Sum of the digits of %d is = %d \n",n,sum);
//15---------------------------------------------------------------------------
number=n, i=0, sum=0;
for(i=0;i < number; i++)
{
sum = sum+i;
if(sum == number)
{
printf("%d is TRIANGULAR NUMBER.\n", number);
break;
}
}
if(number == i)
{
printf("%d is NOT TRIANGULAR NUMBER.\n", number);
}
}
int prime(long int n)
{
long int i=0;
for(i=5;i<n/2;i++)
{
if(n%i==0)
return 1;
else
continue;
}
return 0;
}
/* Function to reverse an integer */
int getReverse(int num)
{
int r = 0;
while (num > 0) {
r = (r * 10) + (num % 10);
num = num / 10;
}
return r;
}
/* Function to calculate sum of digits of a number*/
int getSumOfDigit(int num){
int sum = 0;
while(num != 0){
/* num%10 gives least significant digit of num */
sum = sum + num%10;
num = num/10;
}
return sum;
}