1.
#include <stdio.h>
int avalt(int);
int main()
{
int num,r;
printf("please enter your number\n:");
scanf("%d",&num);
r=avalt(num);
if(r == 1)
printf("this number is prime");
else
printf("this number is not prime");
return 0;
}
int avalt(int v)
{
int i=v-1,b;
for(i;i>1;i--)
{
b=v%i;
if(b == 0)
return 0;
}
return 1;
}
out(13,31,14):
please enter your number
:13
this number is prime⏎
---
please enter your number
:31
this number is prime⏎
---
please enter your number
:14
this number is not prime⏎
2.
#include <stdio.h>
#include <stdlib.h>
void reg(int* st,int stnum);
void out(int *n,int stnum);
void mor(int *st, int stnum);
int main()
{
int stnum;
printf("please enter the number of students\n:");
scanf("%d",&stnum);
int *st = malloc(stnum * sizeof(int));
reg(st,stnum);
mor(st,stnum);
out(st,stnum);
free(st);
}
void reg(int* st,int stnum)
{
int i;
for(i=0;i<stnum;i++)
{
printf("please enter the student number %d code\n:",i+1);
scanf("%d",&st[i]);
}
}
void mor(int *st, int stnum)
{
int temp, i, j, k;
for (j = 0; j < stnum; ++j)
{
for (k = j + 1; k < stnum; ++k)
{
if (st[j] < st[k])
{
temp = st[j];
st[j] = st[k];
st[k] = temp;
}
}
}
}
void out(int *n,int stnum)
{
for(int i=0;i<stnum;i++)
printf("\n%d\n",n[i]);
}
out(4,1234,4321,12345,54321):
please enter the number of students
:4
please enter the student number 1 code
:1234
please enter the student number 2 code
:4321
please enter the student number 3 code
:54321
please enter the student number 4 code
:654321
654321
54321
4321
1234
3.
#include <stdio.h>
void bubble(int arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
void pra(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
void read(int arr[],int size)
{
for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);
}
void merge(int arr1[], int n1, int arr2[], int n2, int ma[])
{
int i = 0, j = 0, k = 0;
while (i < n1 && j < n2)
{
if (arr1[i] < arr2[j])
ma[k++] = arr1[i++];
else
ma[k++] = arr2[j++];
}
while (i < n1)
ma[k++] = arr1[i++];
while (j < n2)
ma[k++] = arr2[j++];
}
int main()
{
int arr1[5], arr2[5],mergea[10];
int n1 = sizeof(arr1)/sizeof(arr1[0]);
int n2 = sizeof(arr2)/sizeof(arr2[0]);
printf("Enter 5 elements for the first array: ");
read(arr1,n1);
printf("Enter 5 elements for the second array: ");
read(arr2,n2);
bubble(arr1, n1);
bubble(arr2, n2);
printf("sorted first array\n:");
pra(arr1, n1);
printf("\nSorted second array\n:");
pra(arr2, n2);
merge(arr1, n1, arr2, n2, mergea);
printf("\nMerged array\n:");
pra(mergea, n1+n2);
return 0;
}
out:
Enter 5 elements for the first array: 12345
54321
11111
123457
687634
Enter 5 elements for the second array: 1297362
1287368
28282171
1283820
29862135
sorted first array
:11111 12345 54321 123457 687634
Sorted second array
:1283820 1287368 1297362 28282171 29862135
Merged array
:11111 12345 54321 123457 687634 1283820 1287368 1297362 28282171 29862135
4.
#include <stdio.h>
void del();
int size;
int main()
{
int i;
printf("please enter the ammount of chars you want\n:");
scanf("%d",&size);
char a[size];
for(i=0;i<size;i++)
scanf(" %c",&a[i]);
del(a);
printf (" \n duplicateds are deleted: ");
for ( i = 0; i < size; i++)
{
printf ("\n %c \n", a[i]);
}
return 0;
}
void del(char *arr)
{
int i,j,k;
for ( i = 0; i < size; i ++)
{
for ( j = i + 1; j < size; j++)
{
if ( arr[i] == arr[j])
{
for ( k = j; k < size - 1; k++)
{
arr[k] = arr [k + 1];
}
size--;
j--;
}
}
}
}
out:(6,aabb00):
please enter the ammount of chars you want
:6
aabb00
duplicateds are deleted:
a
b
0
5.
#include <stdio.h>
unsigned long long int plus();
unsigned long long int stn();
void read();
int main()
{
char str1[21],str2[21];
unsigned long long int num1=0,num2=0,sum=0;
printf("please enter your first number [20]\n:");
read(str1);
printf("please enter your second number [20]\n:");
read(str2);
num1=stn(str1);
num2=stn(str2);
sum = plus(num1,num2);
printf("\n%llu",sum);
return 0;
}
void read(char* *str)
{
gets(str);
}
unsigned long long int stn(char *str)
{
unsigned long long int num = 0;
for (int i = 0; str[i] != '\0'; i++)
{
num = num * 10 + (str[i] - '0');
}
return num;
}
unsigned long long int plus(unsigned long long int num1,unsigned long long int num2)
{
unsigned long long int sum;
sum = num1+num2;
return sum;
}
out(12345678901234567890,09876543210987654321)
please enter your first number [20]
:12345678901234567890
please enter your second number [20]
:09876543210987654321
3775478038512670595⏎
6.
#include <stdio.h>
int main()
{
int i,len;
printf("how many numers you got\n:");
scanf("%d",&len);
int n[len];
for(i=0;i<len;i++)
{
printf("enter numer %d\n:",i+1);
scanf("%d",&n[i]);
}
set(n,len);
for(i=0;i<len;i++)
{
printf("%d\n",n[i]);
}
}
void set(int *n, int len)
{
int temp, i, j, k;
for (j = 0; j < len; ++j)
{
for (k = j + 1; k < len; ++k)
{
if (n[j] > n[k])
{
temp = n[j];
n[j] = n[k];
n[k] = temp;
}
}
}
}
out(3,1234,4321,12345):
how many numers you got
:3
enter numer 1
:1234
enter numer 2
:4321
enter numer 3
:12345
1234
4321
12345
7.
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int count[256] = {0};
printf("Enter a string ending with '.': ");
scanf("%[^.]", str);
int len = strlen(str);
for (int i = 0; i < len; i++)
count[str[i]]++;
for (int i = 0; i < 256; i++)
{
if (count[i] > 0)
{
printf("%c occurs %d times\n", i, count[i]);
}
}
return 0;
}
out(hello my little world, my little rose.):
Enter a string ending with '.': hello my little world, my little rose.
occurs 6 times
, occurs 1 times
d occurs 1 times
e occurs 4 times
h occurs 1 times
i occurs 2 times
l occurs 7 times
m occurs 2 times
o occurs 3 times
r occurs 2 times
s occurs 1 times
t occurs 4 times
w occurs 1 times
y occurs 2 times
8.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void revstr(char* str,int len);
int main()
{
int len = 1;
char *sent = malloc(len * sizeof(char));
printf("enter the word you want\n:");
char c;
int i = 0;
while ((c = getchar()) != '\n' && c != EOF)
{
if (i == len - 1)
{
len *= 2;
sent = realloc(sent, len * sizeof(char));
}
sent[i++] = c;
}
sent[i] = '\0';
len = strlen(sent);
char *sentold = malloc(len * sizeof(char));
strncpy(sentold, sent, len);
revstr(sent,len);
if(strcmp(sent,sentold)==0)
printf("\nYES, you can say word %s backwards!\n",sentold);
else
printf("\nNO, you can not say word %s backwards :(\n",sentold);
puts(sent);
free(sent);
free(sentold);
return 0;
}
void revstr(char* str,int len)
{
int i,temp;
for (i = 0; i < len/2; i++)
{
temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
out(lol,bob,mario)
enter the word you want
:lol
YES, you can say word lol backwards!
lol
---
enter the word you want
:bob
YES, you can say word bob backwards!
bob
---
enter the word you want
:mario
NO, you can not say word mario backwards :(
oiram
9.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
getchar();
fgets(str2, 100, stdin);
str2[strcspn(str2, "\n")] = 0;
char *ptr = strstr(str2, str1);
if (ptr != NULL)
printf("The first string was found in the second string at position %ld\n", ptr - str2);
else
printf("The first string was not found in the second string\n");
return 0;
}
out(jamal,hi my name is jamal)
Enter the first string: jamal
Enter the second string: hi my name is jamal
The first string was found in the second string at position 14
10.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, 100, stdin);
str[strcspn(str, "\n")] = 0;
char *word = strtok(str, " ");
while (word != NULL)
{
if (strlen(word) == 4)
printf("love ");
else
printf("%s ", word);
word = strtok(NULL, " ");
}
return 0;
}
out(I hate you you made my life a nightmare):
Enter a string: I hate you you made my life a nightmare
I love you you love my love a nightmare ⏎
11.
#include <stdio.h>
void pr(int num[], int len)
{
for(int i=0;i<=len;i++)
printf("\n%d\n",num[i]);
}
int test(int num[],int len)
{
int i,j;
int const staticdev=num[(len+1)/2+i];
for(i=0;i<=(len+1)/2;i++)
{
if(num[(len+1)/2+i]!=0)
{
for(j=0;j<=len;j++)
num[j]=num[j]/staticdev;
return 1;
}
else
{
i*=-1;
if(num[(len+1)/2+i]!=0)
{
for(j=0;j<=len;j++)
num[j]=num[j]/staticdev;
return 1;
}
i*=-1;
}
}
return 0;
}
int main()
{
int len,i,j;
printf("how many numbers do you have?\n:");
scanf("%d",&len);
len=len-1;
int num[len];
for(i=0;i<=len;i++)
{
printf("please enter number %d:\n",i+1);
scanf(" %d",&num[i]);
}
j = test(num,len);
if(j==1)
pr(num,len);
else
printf("\ntheres no number, larger than 0");
}
out(5,4,4,2,4,4):
how many numbers do you have?
:5
please enter number 1:
4
please enter number 2:
4
please enter number 3:
2
please enter number 4:
4
please enter number 5:
4
2
2
1
2
2
am1nsab83@surfarch ~/D/p/HW (main)>
out(3,0,0,0):
how many numbers do you have?
:3
please enter number 1:
0
please enter number 2:
0
please enter number 3:
0
theres no number, larger than 0⏎
12.
#include <stdio.h>
#include <string.h>
char a[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char A[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char vowel[10]={'A','E','I','O','U','a','e','i','o','u'};
int checka(char str[],int len)
{
int count=0;
for(int i=0;i<len;i++)
{
for(int j=0;j<=26;j++)
{
if(str[i]==a[j])
count++;
else
continue;
}
}
return count;
}
int checkA(char str[],int len)
{
int count=0;
for(int i=0;i<len;i++)
{
for(int j=0;j<=26;j++)
{
if(str[i]==A[j])
count++;
else
continue;
}
}
return count;
}
int checknum(char str[],int len)
{
int count=0;
for (int i = 0; i < len; i++)
{
if (str[i] >= '0' && str[i] <= '9')
count++;
}
return count;
}
int checkvow(char str[],int len)
{
int count=0;
for(int i=0;i<len;i++)
{
for(int j=0;j<=10;j++)
{
if(str[i]==vowel[j])
count++;
else
continue;
}
}
return count;
}
int main()
{
char str[100];
printf("please enter your string\n:");
fgets(str,100,stdin);
int len = strlen(str);
int a=checka(str,len);
int A=checkA(str,len);
int num=checknum(str,len);
int vow=checkvow(str,len);
printf("the number of small chars:%d\nthe number of big chars:%d\nthe number of numbers:%d\nand at last the number of vowels:%d",a,A,num,vow);
return 0;
}
out(Hi My username is Am1nsab83):
please enter your string
:Hi My username is Am1nsab83
the number of small chars:17
the number of big chars:3
the number of numbers:3
and at last the number of vowels:8⏎
13.
#include <stdio.h>
#include <string.h>
int main(void) {
char str[100];
int sum = 0;
printf("Enter a string: ");
fgets(str, 100, stdin);
for (int i = 0; i < strlen(str); i++) {
if (str[i] >= '0' && str[i] <= '9') {
sum += str[i] - '0';
}
}
printf("Sum of digits: %d\n", sum);
return 0;
}
out(hi I have 3 apple and I want 5 more):
Enter a string: hi I have 3 apple and I want 5 more
Sum of digits: 8