# C: Strings - Applications resolved

1) Calculate the n! iterative variant.
```#include<stdio.h>
long int factorial (int n)
{
long int f=1;
for (int i=1; i<=n;i++)
f=f*i;
return f;
}
void main()
{
int n;
printf("Enter n= ");
scanf("%d", &n);

if(!n)
printf("0!=1\n");
else
printf("%d!=%ld\n",n,factorial(n));

getchar();
int var;
scanf("%d", var);
}```
2) Calculate the n! recursive variant.
```// factorial(3)=3*factorial(2)=3*2*factorial(1)=3*2*1
#include<stdio.h>
long int factorial (int n)
{
if (n==1) return 1;
else return n*factorial(n-1);
}
void main()
{
int n;
printf("Enter n= ");
scanf("%d", &n);

if(!n)
printf("0!=1\n");
else
printf("%d!=%ld\n",n,factorial(n));

getchar();
int var;
scanf("%d", var);
}```
3) Calculate the sum of the elements of an string recursively.
```#include<stdio.h>
int sum(int n)
{
if (n==0) return 0;
else return (n + sum(n-1));
}
void main()
{
int n;
printf("Enter n: ");
scanf("%d", &n);
printf("Sum of the elements is %d\n",sum(n));

getchar();
int var;
scanf("%d", var);
}```
4) Write an own function that performs the recursive calculation of the sum of the elements of a vector of n <= 10 , real numbers. Write the main function that reads data from the keyboard, calculates the amount, previously defined using recursive function and displays the value obtained.
```#include <stdio.h>
#include <conio.h>

int a[10], n;
int Sum (int n, int a[10])
{
if(n==0) return 0;
else return(a[n]+Sum(n-1,a));
}
void main()
{
printf("Enter the number of elements: ");
scanf("%d", &n);
for (int i=1; i<=n; i++)
{
printf("Element [%d] = ", i);
scanf("%d", &a[i]);
}
// Displaying results
printf("Sum = %d", Sum(n,a));

getch();
}```
5) Write a C program to solve gcd's between two unsigned integers (to determine gcd's we use Euclid algritmul by decreases). Iterative version.
```#include <stdio.h>
#include <conio.h>

unsigned int gcd(unsigned int a, unsigned int b)
{
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
}
void main()
{
unsigned int x,y;
printf("Enter x: ");
scanf("%u",&x);
printf("Enter y: ");
scanf("%u",&y);

if(!x || !y)		//if x=0 or y=0
printf("gcd(%u,%u) = 1\n",x,y);
else
printf("gcd(%u,%u) = %u\n",x,y,gcd(x,y));

getch();
}```
6) Write a C program to solve gcd's between two unsigned integers (to determine gcd's we use Euclid algritmul by decreases). Recursive version.
```#include <stdio.h>
#include <conio.h>

unsigned int gcd(unsigned int a, unsigned int b)
{
if(a==b) return a;
else
if(a>b) return gcd(a-b,b);
else return gcd(a,b-a);
}
void main()
{
unsigned int x,y;
printf("Enter x: ");
scanf("%u",&x);
printf("Enter y: ");
scanf("%u",&y);

if(!x || !y)		//if x=0 or y=0
printf("gcd(%u,%u) = 1\n",x,y);
else
printf("gcd(%u,%u) = %u\n",x,y,gcd(x,y));

getch();
}```
7) Write a C program to solve gcd's between two unsigned integers (to determine gcd's we use Euclid algritmul by decreases). Recursive version.
```#include <stdio.h>
#include <conio.h>

unsigned int gcd_2(unsigned int a, unsigned int b)
{
if(a==b) return a;
if(a>b) return gcd_2(a-b,b);
else
return gcd_2(a,b-a);
}
unsigned int gcd_n(unsigned int x[], int n)
{
if (n==2) return gcd_2(x[0],x[1]);
else	return gcd_2(gcd_n(x,n-1),x[n-1]);
}
void main()
{
unsigned int x[20];
int n;
printf("Enter n: ");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("element %d= ",i+1);
scanf("%u",&x[i]);
}
if (n==1)
printf("\ngcd number: %u",x[0]);
else
printf("\ngcd number: %u",gcd_n(x,n));

getch();
}```
8) Consider these declarations and conventions: typedef int array [20], x - is an array (number of elements) n - the length (n> = 1) Required to write recursive functions to determine, for a vector x of length n, the following:
b) display of items in the sequence
c) sum of its parts
d) product components
e) number of negative components
f) positive component product
g) the arithmetic mean of the elements
```#include<stdio.h>
#include<conio.h>
/* a type defined to store integer arrays of elements with a
maximum size of 20 components */
typedef int vector[20];
void read(vector x,int n) //n is the real size of the string
{
//read the last element from the string
printf("\telement %d: ",n);
scanf("%d",&x[n-1]);
if(n>=2)
read(x,n-1); //recursive call of the function
}
//display function
void display(vector x,int n) //n is the real size (number of elem. in string)
{
//display the last element
printf("%d ",x[n-1]);
if(n>=2)
display(x,n-1); //recursive call of the function
}
int sum(vector x,int n) //n in this case we consider the index element in the string
{
if(n==-1) return 0; //situation that are not elements in string, n = -1 position is not in string
else return x[n]+sum(x,n-1);
}
//product components
int product(vector x,int n)
{
if(n==-1) return 1;
else return x[n]*product(x,n-1);
}
//number of negative components
int number_negative(vector x,int n)
{
//position ourselves on the the first element of string and check if it is negative
if(n==0) return (x[n]<0); //conditional expression returns 1 in case of truth and 0 in case of false
else return (x[n]<0)+number_negative(x,n-1);
}
//positive component product
int product_positive(vector x,int n)
{
if(n==0) return (x[n]>0?x[n]:1); /* I used operator conditioning,
that, if the expression evaluates to true will be taken into account x [n] otherwise
value 1*/
else return (x[n]>0?x[n]:1)*product_positive(x,n-1);
}
//average of string parts
float average(vector x, int m, int n) //with "m" noted items index and the real size of the sequence with n
{
return (float)x[m]/n + ((m!=0)?average(x,m-1,n):0);
/*- I used the expression (float) for explicit conversion result
for a real type
- through x [m] / n understand an item (in the first phase, which is the last
element in the string) divided by the total number of components*/
}
//main function to run
void main()
{
vector x; //string of elements
int n; //size (number of components reads)

printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the string elements:\n");

//Performing display string
printf("String of elements is: ");
display(x,n);

//summation elements
printf("\nSum of the elements: %d",sum(x,n-1)); /* I called with n-1,
as I said above that this parameter is the index of the last element in the string*/
//product elements
printf("\nProduct elements: %d",product(x,n-1));
//number of negative elements in the string
printf("\nThe number of negative elements: %d",number_negative(x,n-1));
//positive component product
printf("\nProduct positive elements: %d",product_positive(x,n-1));
//average components string
printf("\nAverage components string: %.2f",average(x,n-1,n)); /* first parameter - string,
the second parameter - the index of the last element in the string,
the third parameter - the real size of the string (number of items read)*/
getch();
}
```
9) Write a recursive function to determine sum of integer numbers. Hint: Isolate last digit and assigns to n whole of the old value quotient and 10.
```#include<stdio.h>
#include<conio.h>

int sum(int n)
{
if(!n) return 0;			//!n=if there is no n
else return n%10+sum(n/10);
}
void main()
{
int n;
printf("Enter the number: ");
scanf("%d", &n);

printf("Sum of the digits of the number is: %d", sum(n));

getch();
}```
10) Write a recursive function to convert an integer "n" from the basis 10 in the basis "k" (1 <k < = 10). Note: The number is divided by 'k', withheld rest quotient is divided by k, withheld the rest ... until the quotient is less than the divisor. The result is obtained by writing the remainders in reverse order obtained. Printing is done after autoapel rest.
```#include<stdio.h>
#include<conio.h>

void transform(int n,int b)
{
int rest=n%b;
if (n>=b) transform(n/b,b);
printf("%d",rest);
}
void main()
{
int n,b;
printf("n="); scanf("%d",&n);
printf("base="); scanf("%d",&b);
transform(n,b);
getch();
}```
11) It reads x from Z. Required for calculating subprogram-Pnuelli Manna function:
```	x-1, x>=12
F(x)=
F(F(x+2)), x<12
```
```#include<stdio.h>
#include<conio.h>

int F(int x)
{
if (x>=12) return x-1;
return F(F(x+2));
}
void main()
{
int x;
printf("x="); scanf("%d",&x);
printf("Function value is: %d",F(x));

getch();
}```
12) It is considered the Fibonacci sequence (UN) defined as follows:
```	 0, n=0
Un=    	 1, n=1
Un-1+Un-2, altfel
```
It reads "n" natural numbers. Calculate UN, the iterative version.
```#include<stdio.h>
#include<conio.h>

void main()
{
int n,U0=0,U1=1,U2;
printf("n="); scanf("%d",&n);
if(!n) printf("%d",U0);
else
if (n==1) printf("%d",U1);
else
{
for (int i=2;i<=n;i++)
{
U2=U0+U1;
U0=U1;
U1=U2;
}
printf("%d",U2);
}
/*
for n=3
i=2: U2=U0+U1
U0=U1
U1=U2
i=3: U2=U1+U2
*/
getch();
}```
13) It is considered the Fibonacci sequence (UN) defined as follows:
```	 0, n=0
Un=    	 1, n=1
Un-1+Un-2, altfel
```
It reads "n" natural numbers. Calculate UN, the recursive version.
```#include<stdio.h>
#include<conio.h>

int U (int n)
{
if (!n) return 0;
else	if (n==1) return 1;
else	return U(n-1)+U(n-2);
}

void main()
{
int n;
printf("Enter n=");
scanf("%d",&n);

printf("String value n is: %d",U(n));

getch();

}```
Cookies help us deliver our services. By using our services, you agree to our use of cookies.