Skip to content

Commit a298692

Browse files
committed
Commit tmp.c
0 parents commit a298692

File tree

1 file changed

+342
-0
lines changed

1 file changed

+342
-0
lines changed

tmp.c

Lines changed: 342 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,342 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
#define VARSIZE(var) ((char*)(&var+1) - (char*)(&var)) /*MTA: & operator returns a pointer to the variable. +1 takes you beyond the end of the variable. char* typecasting helps you count.*/
5+
6+
/*Reverse a number*/
7+
8+
int reversedigit(int num)
9+
{
10+
int result = 0, count = 0;
11+
int tmp = num; //in case original num is needed
12+
while (num)
13+
{
14+
result = (result*10) + num%10;
15+
num /= 10;
16+
}
17+
return result;
18+
}
19+
20+
/*************************************************************/
21+
22+
/* Check a num is prime or not */
23+
24+
int isprime(int num)
25+
{
26+
if (num%2 == 1)
27+
{
28+
for(int i=2; i<num; i++)
29+
{
30+
if (num%i == 0)
31+
{
32+
return 0;
33+
}
34+
}
35+
return 1;
36+
}
37+
return 0;
38+
}
39+
40+
/****************************************************************/
41+
42+
/*Palindrome number*/
43+
44+
int isPalindrome(int num)
45+
{
46+
int rev = 0;
47+
int num2 = num; //in case origl num is needed later
48+
while (num2)
49+
{
50+
rev = (rev*10) + (num2%10);
51+
num2 /= 10;
52+
}
53+
if (rev == num)
54+
return 1;
55+
else
56+
return 0;
57+
}
58+
59+
/*****************************************************************/
60+
61+
/* reattempt: Palindrome number*/
62+
63+
int isnumberPal (int n)
64+
{
65+
int m = n;
66+
int rev = 0;
67+
while (n > 0)
68+
{
69+
rev = rev*10 + n%10;
70+
n/=10;
71+
}
72+
printf ("rev = %d", rev);
73+
if (rev == m)
74+
return 1;
75+
else
76+
return 0;
77+
}
78+
79+
/*Fibonacci recursion*/
80+
81+
int Fibonacci (int num)
82+
{
83+
if (num == 0)
84+
return 0;
85+
else if (num == 1)
86+
return 1;
87+
else
88+
return (Fibonacci(num-1) + Fibonacci(num-2));
89+
}
90+
91+
/*******************************************************************/
92+
93+
/* Fibonacci iteratively*/
94+
95+
int Fibon (int n)
96+
{
97+
int p = 0, q = 1, ans;
98+
if (n == 0)
99+
return 0;
100+
else if (n == 1)
101+
return 1;
102+
else
103+
{
104+
for(int i = 2; i<= n; i++)
105+
{
106+
ans = p+q;
107+
p = q;
108+
q = ans;
109+
}
110+
return ans;
111+
}
112+
}
113+
114+
/********************************************************************/
115+
116+
/*Factorial of a number RECURSIVELY*/
117+
118+
int Factorial (int num)
119+
{
120+
if(num == 1)
121+
return 1;
122+
else if (num ==0)
123+
return 1;
124+
else
125+
return (num*Factorial(num-1));
126+
}
127+
/********************************************************************/
128+
129+
/* Factorial ITERATIVELY*/
130+
131+
int Fact (int n)
132+
{
133+
int m = 1, ans;
134+
if (n == 0)
135+
return 1;
136+
else if (n == 1)
137+
return 1;
138+
else
139+
{
140+
for (int i=2; i<=n; i++)
141+
{
142+
ans = i*m;
143+
m = ans;
144+
}
145+
return ans;
146+
}
147+
148+
}
149+
150+
/*******************************************************************/
151+
152+
/*Reverse a string*/
153+
154+
char* strrev (char* s, int size)
155+
{
156+
char* ans = s;
157+
for (int i=0; i<size; i++)
158+
{
159+
ans[size-1-i] = s[i];
160+
}
161+
return ans;
162+
}
163+
/**********************************************************************/
164+
165+
/*Find first duplicate element in Array*/
166+
167+
int FirstDup (int* a, int l)
168+
{
169+
for (int i=0; i<l-1; i++)
170+
{
171+
for (int j=i+1; j<l; j++)
172+
{
173+
if (a[i] == a[j])
174+
{
175+
return a[i];
176+
}
177+
}
178+
}
179+
return -1;
180+
/*
181+
inside main:
182+
183+
printf("Enter size\n");
184+
scanf("%d", &size);
185+
printf("Enter elements\n");
186+
for (int i=0; i<size; i++)
187+
{
188+
scanf("%d", &ar[i]);
189+
}
190+
191+
*/
192+
}
193+
194+
/********************************************************************/
195+
196+
/* Remove consecutive characters of a string */
197+
198+
char* RmvChar (char* s, int l)
199+
{
200+
char* ans;
201+
for(int i=0; i<l; i=i+2)
202+
{
203+
ans[i/2] = s[i];
204+
}
205+
ans[l/2] = '\0';
206+
return ans;
207+
}
208+
209+
/*******************************************************************/
210+
211+
/*Bubble Sort*/
212+
213+
void bubsort (int* nums, int n)
214+
{
215+
int temp;
216+
for (int i=0; i<n-1; i++)
217+
{
218+
for (int j=0; j<n-1; j++)
219+
{
220+
if(nums[j] > nums[j+1])
221+
{
222+
nums[j] = nums[j]^nums[j+1];
223+
nums[j+1] = nums[j]^nums[j+1];
224+
nums[j] = nums[j]^nums[j+1];
225+
}
226+
}
227+
}
228+
}
229+
/*******************************************************************/
230+
231+
/* BitMnpl: check whether ith bit set or not */
232+
233+
int isBitSet (char c)
234+
{
235+
//checking if bit i is set
236+
//char mask
237+
char mask = 0x10;
238+
if(~(mask)^c)
239+
return 1;
240+
return 0;
241+
}
242+
/* *****************************************************************/
243+
244+
/* BitMnpl: Check if number is power of 4*/
245+
246+
// Check if number is power of 2 AND mod 3 == 1
247+
248+
int isPwrof4 (int num)
249+
{
250+
if ((num & (num-1)) == 0) //Power of 2
251+
{
252+
if (num%3 == 1)
253+
return 1;
254+
}
255+
else
256+
return 0; //False
257+
}
258+
259+
/*******************************************************************/
260+
261+
/* Check if given string is a Palindrome */
262+
263+
int isPal (char* s, int len)
264+
{
265+
char* ss = s;
266+
for (int i=0; i<(len/2); i++) //thought of reversing but there's no need
267+
{
268+
if(ss[i] != s[len-1-i])
269+
return 0;
270+
else
271+
return 1;
272+
}
273+
}
274+
275+
/*********************************************************************/
276+
277+
/* Remove odd numbers from array */
278+
279+
int* rmvodds (int* ar, int* size)
280+
{
281+
int count = 0;
282+
int sizeofar = *size;
283+
printf("sizeofar = %d\n", sizeofar);
284+
for (int i=0; i<*size; i++)
285+
{
286+
if (ar[i]%2 == 0)
287+
{
288+
count++;
289+
}
290+
}
291+
printf("count = %d\n", count);
292+
int* ans = (int*) malloc (count*sizeof(int));
293+
for (int i=0, j=0; i<*size; i++)
294+
{
295+
if (ar[i] %2 == 0)
296+
{
297+
ans[j] = ar[i];
298+
j++;
299+
}
300+
}
301+
*size = count;
302+
return ans;
303+
304+
/*
305+
int main()
306+
{
307+
int arr[] = {8,9,5,6,1,2,7,4,7};
308+
int size = sizeof(arr)/sizeof(int);
309+
//printf("size = %d\n", size); //works
310+
int* arrout = NULL;
311+
arrout = rmvodds(arr, &size);
312+
printf("size = %d\n", size);
313+
for (int i=0; i<size; i++)
314+
{
315+
printf("%d ", arrout[i]);
316+
}
317+
//free(what?) //find answer: what to free in this case of dynamic mem allocation
318+
return 0;
319+
}
320+
*/
321+
322+
/***************************************************************************/
323+
}
324+
325+
int main()
326+
{
327+
/*int n = 64;
328+
if (isPwrof4(n))
329+
printf("True");
330+
else
331+
printf("False");
332+
/*char* str = "String";
333+
n = strlen(str);
334+
char* result = strrev(str, n);
335+
printf("%s ", result);
336+
*/
337+
338+
int n;
339+
n = Fibon(4);
340+
printf("last term = %d", n);
341+
return 0;
342+
}

0 commit comments

Comments
 (0)