Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5f9a34935 | ||
|
|
01f553fe4c | ||
|
|
ba2e7c99f4 | ||
|
|
e580f118a1 |
34
atoi.c
Normal file
34
atoi.c
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
int myatoi(char *s, int l);
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
printf("Print your string number\n");
|
||||||
|
char s[100]; scanf("%s", s);
|
||||||
|
printf("Your converted number is %d\n", myatoi(s, strlen(s)));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int myatoi(char *s, int l)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
int is_pos = 1;
|
||||||
|
for(int i=0; i < l; i++)
|
||||||
|
{
|
||||||
|
if((int)s[i]==45)
|
||||||
|
{
|
||||||
|
is_pos = -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if((int)s[i] < 48 || (int)s[i] > 57)
|
||||||
|
{
|
||||||
|
printf("Not a number\n");
|
||||||
|
result = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
result = result * 10 + ((int)s[i] - 48);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result = result * is_pos;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
57
atoi_2.0.c
Normal file
57
atoi_2.0.c
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
int myatoi(char *s, int l, int base);
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
printf("Print your string number\n");
|
||||||
|
char s[100]; scanf("%s", s);
|
||||||
|
printf("Enter your string number base\n");
|
||||||
|
int base;
|
||||||
|
scanf("%d", &base);
|
||||||
|
printf("Your converted number is %d\n", myatoi(s, strlen(s), base));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int myatoi(char *s, int l, int base)
|
||||||
|
{
|
||||||
|
int result = 0;
|
||||||
|
int is_pos = 1;
|
||||||
|
int symb;
|
||||||
|
int d;
|
||||||
|
for(int i=0; i < l; i++)
|
||||||
|
{
|
||||||
|
symb = (int)s[i];
|
||||||
|
if((int)s[i]=='-')
|
||||||
|
{
|
||||||
|
is_pos = -1;
|
||||||
|
}
|
||||||
|
else if(base <= 10)
|
||||||
|
{
|
||||||
|
if((int)s[i] < '0' || (int)s[i] > '9')
|
||||||
|
{
|
||||||
|
printf("Not a number\n");
|
||||||
|
result = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
result = result * base + ((int)s[i] - '0');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (symb >= '0' && symb <= '9')
|
||||||
|
{
|
||||||
|
d = symb - '0';
|
||||||
|
}
|
||||||
|
else if (symb >= 'A' && symb <= 'Z')
|
||||||
|
{
|
||||||
|
d = symb - 'A' + 10;
|
||||||
|
}
|
||||||
|
else if (symb >= 'a' && symb <= 'z')
|
||||||
|
{
|
||||||
|
d = symb - 'a' + 10;
|
||||||
|
}
|
||||||
|
result = result * base + d;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
result = result * is_pos;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
18
fib.c
Normal file
18
fib.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
int fib(int n);
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
printf("Print number of fibanachi sum\n");
|
||||||
|
int n = 1; scanf("%d", &n);
|
||||||
|
printf("\n%dth number of fibanachi sum is: ", n);
|
||||||
|
printf("%d\n", fib(n));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int fib(int n)
|
||||||
|
{
|
||||||
|
int array[n];
|
||||||
|
array[0] = 0; array[1] = 1;
|
||||||
|
for(int i = 2; i <= n; i++)
|
||||||
|
{array[i] = array[i-1] + array[i-2];}
|
||||||
|
return array[n - 1];
|
||||||
|
}
|
||||||
20
sum_array.c
Normal file
20
sum_array.c
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
int sum(int array[], int size);
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
int array[] = {2,2,2,2,2};
|
||||||
|
int size = sizeof(array)/sizeof(array[0]);
|
||||||
|
printf("Array size is %d\n{", size);
|
||||||
|
for(int i = 0; i < size; i++)
|
||||||
|
{printf("%d,",array[i]);}
|
||||||
|
printf("}\nResult of summing the array:%d\n", sum(array, size));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int sum(int array[], int size)
|
||||||
|
{
|
||||||
|
int sum = 0;
|
||||||
|
for(int i = 0; i < size; i++)
|
||||||
|
{sum += array[i];}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user