C: Instructions - Applications resolved


1) Write a program to perform arithmetical operations desired (+,-,/,*).
#include<stdio.h>
void main()
{	
int a,b;
	printf("Enter a: ");
	scanf("%d", &a);
	printf("Enter b: ");
	scanf("%d", &b);
	char x;
	getchar();
	printf("Enter the desired operation (+ - * /): ");
    	scanf("%c", &x);
 	switch(x)
	{
		case '+': printf("a+b= %d", a+b); break;
		case '-': printf("a-b= %d", a-b); break;
		case '/': printf("a/b=%.2f",a/(float)b); break;
		case '*': printf("a*b= %d", a*b); break;
		default: printf("Operation unknown");
	}
	getchar();
	int k;
	scanf("%d", k);
}
2) Write a program that calculates the sum of the first n natural numbers with for.
#include<stdio.h>
void main()
{
 	int n,s=0;
 	printf("Enter n:");
 	scanf("%d",&n);
 	for(int i=1;i<=n;i++)
 	s+=i;   //s=s+i;
	printf("Sum of the first %d integers is: %d\n",n,s);
 
 	getchar();
 	int k;
 	scanf("%d", k);
}
3) Write a program that calculates the sum of the first n natural numbers with while.
#include<stdio.h>
void main()
{
 int n, s=0, i=1;
 printf("Enter n:");
 scanf("%d",&n);
 
 while(i<=n)
 {
	s+=i;   //s=s+i;
	i++;
 }
 printf("Sum of the first %d integers is: %d\n",n,s);
 
	getchar();
	int k;
	scanf("%d", k);
}
4) Write a program that calculates the sum of the first n natural numbers with do while.
#include<stdio.h>
void main()
{
 int n, s=0, i=1;
 printf("Enter n:");
 scanf("%d",&n);
 
do
 {
	s+=i;   //s=s+i;
	i++;
 }
  while(i<=n);
 
 printf("Sum of the first %d integers is: %d\n",n,s);
 
	getchar();
	int k;
	scanf("%d", k);
}
5) Write a program to solve the equation of degree II.
#include <stdio.h>
#include <math.h>
void main (void)
{
	int a,b,c;
	float x1,x2,d;
	printf("Enter the equation coefficients.\n");
	printf("\ta="); scanf("%d",&a);
	printf("\tb="); scanf("%d",&b);
	printf("\tc="); scanf("%d",&c);
	if(!a) //a==0
		if(!b)
			if(!c)
				printf("Equation has an infinity of solutions.");
			else
				printf("Equation has no solution.");
		else
			{
			x1=-c/(float)b;
			printf("Solution of the equation is: %.2f.",x1);
			}
	else
		x1=-c/(float)b;
		printf("Solution of the equation is: %.2f.",x1);
 
		{
			d=b*b-4*a*c;
			if(d<0)
				printf("Equation has no real solutions.");
			else
				{
				x1=(-b-sqrt(d))/2*a;
				printf("Solutions are:\n\tx1 is: %.2f\n",x1);
				x2=(-b+sqrt(d))/2*a;
				printf("\tx2 is: %.2f\n",x2);
				}
		}
 
	getchar();
	int k;
	scanf("%d", k);
}
6) Write a program that calculates the value of the expression: S=1-2+3-4+....+, using eigenfunctions.
#include <stdio.h>
int sum1(int n)			//with the instruction FOR
{
	int s=0, i=1, sign=1;
	for(; i<=n; i++)
	{
		s+=sign*i;			//s=s+sign*i
		sign=-sign;
	}
	return s;
}
 
int sum2(int n)			//with the instruction WHILE
{
	int s=0, i=1, sign=1;
	while (i<=n)
		{
			s+=sign*i;		//s=s+sign*i
			sign=-sign;
			i++;
		}
	return s;
}
 
int sum3(int n)			//with the instruction DO WHILE
{
	int s=0, i=1, sign=1;
	do 
		{
			s+=sign*i;		//s=s+sign*i
			sign=-sign;
			i++;
		}
	while (i<=n);
	return s;
}
 
void main()
{
	int n;
	printf("Enter n:");
	scanf("%d", &n);
 
	printf("\n\tSum calculated with FOR has value: %d" , sum1(n));
	printf("\n\tSum calculated with WHILE has value: %d" , sum2(n));
	printf("\n\tSum calculated with DO WHILE has value: %d" , sum3(n));
 
	getchar();
	int k;
	scanf("%d", k);
}
7) Write a program that calculates the value of the expression: P=1*2*3*4*...*n.
#include<stdio.h>
int product1(int n)			//with the instruction FOR
{
	int P=1;
	for(int i=1; i<=n; i++)
		P=P*i;
	return P;
}
int product2(int n)			//with the instruction WHILE

{
	int P=1; int i=1;
	while(i<=n)
	{
		P=P*i;
		i++;
	}
	return P;
}
int product3(int n)			//with the instruction DO WHILE
{
	int P=1; int i=1;
	do{
		P=P*i;
		i++;
	   }
	while(i<=n);
	return P;
}
void main()
{
	int n; 
	printf("Enter n: ");
	scanf("%d", &n);
	printf("\n\tproduct calculated with FOR has value: %d.\n" , product1(n));
	printf("\n\tproduct calculated with WHILE has value: %d.\n" , product2(n));
	printf("\n\tproduct calculated with DO WHILE has value: %d.\n" , product3(n));
 
	getchar();
	int k;
	scanf("%d", k);
}
8) Write a program that calculates the value of the expression: P=1*(1/2)*3*(1/4)*5*(1/6)*...n .
#include<stdio.h>
#include<math.h>
float product1(int n)		//with the instruction FOR
{
	float p=1;
	float z=1;
	
	for(int i=1; i<=n; i++)
	{
		if (!(i%2))	//could write if(i%2==0) <=> test if is't odd
		{
			z=1/(float)i;
			p=p*z;
		}
		else p=p*i;	//for odd
	}
	return p;
}
float product2(int n)		//with the instruction WHILE
{
	float p=1;
	float z=1;
	int i=1;
	while(i<=n)
	{
		if (!(i%2))	//could write if(i%2==0) <=>  test if is't odd
		{
			z=1/(float)i;
			p=p*z;
		}
		else p=p*i;	//for odd
		i++;
	}
	return p;
}
float product3(int n)		//with the instruction DO WHILE
{
	float p=1;
	float z=1;
	int i=1;
	do
	{
		if (!(i%2))	//could write if if(i%2==0) <=> test if is even
		{
			z=1/(float)i;
			p=p*z;
		}
	
		else p=p*i;	//for even
		i++;
	}
	while(i<=n);
	return p;
}
void main()
{
	int n;
	printf("Enter n: ");
	scanf("%d", &n);
 
	printf("\nproduct calculated with FOR has value: %.5f\n", product1(n));
	printf("\nproduct calculated with WHILE has value: %.5f\n", product2(n));
	printf("\nproduct calculated with DO WHILE has value: %.5f\n", product3(n));
 
	getchar();
	int k;
	scanf("%d", k);
}
9) Write a program that calculates the value of the expression: S=1+1*2+1*2*3+1*2*3*4+1*2*3*4*5+...+1*2*...*n .
#include<stdio.h>
void main()
{
	int n;
	int s=0;
	int p=1;
 
	printf("Enter n:");
	scanf("%d", &n);
 
	for(int i=1; i<=n; i++)
	{
		p=p*i;
		s=s+p;
	}
	printf("The result is: %d", s);
 
	getchar();
	int k;
	scanf("%d", k);
}
10) Write a program that calculates x to the power y (using pow function).
#include <math.h>
#include<stdio.h>
void main()
{
	double x, y, z; 
 
	printf("\nEnter the coefficient: ");
	scanf("%lf", &x);
	printf("\nYou introduced x= %.2lf", x);
 
	printf("\n\nEnter the power: ");
	scanf("%lf", &y);
	printf("\nYou introduced y= %.2lf", y);
 
	z=pow(x,y);
 
	printf("\n\n\%.2lf^%.2lf= %.2lf", x,y,z); 
 
	getchar();
	int k;
	scanf("%d", k);
}
11) It reads a text consisting of words that can be separated by spaces or comma. Text read ends with point or enter. Count the vowels and consonants of that text.
#include<stdio.h>
void main()
{
int v=0;
	int c=0;
 
	printf("Enter the desired text: ");
 
	char k;
k=getchar(); //read a character, which will be the first text that will be sent to variable k

	while(k!='.' && k!='\n')
	{
		if (k!=',' && k!=' ')
			switch (k)
		{
			case 'a':
			case 'A':
			case 'e':
			case 'E':
			case 'i':
			case 'I':
			case 'o':
			case 'O':
			case 'u':
			case 'U': v++;
			break;
			default: c++;
		}
		k=getchar(); //next character from text will be sent  in the variable k
	}
	printf("Number of vowels is: %u\n",v);
	printf("The number of consonants is: %u\n",c);
 
 
	getchar();
	int z;
	scanf("%d", z);
}
12) It reads a text consisting of words that can be separated by spaces or comma. Text read ends with point or enter. Count the vowels and consonants of that text. Solve the problem using a repetitive loop conditional posterior.
#include<stdio.h>
void main()
{
	int v=0;
	int c=0;
	printf("Enter the desired text: ");
	char k;
 
	do
	{
		k=getchar();
		if (k!=',' && k!=' ' && k!='\n' && k!='.')
		switch (k)
		{
			case 'a':
			case 'A':
			case 'e':
			case 'E':
			case 'i':
			case 'I':
			case 'o':
			case 'O':
			case 'u':
			case 'U': v++;
			break;
			default: c++;
		}
	}
 
	while(k!='.' && k!='\n');
 
	printf("Number of vowels is %u\n",v);
	printf("The number of consonants is %u\n",c);
 
	getchar();
	int z;
	scanf("%d", z);
}
13) Count the number of vowels and consonants of the alphabet, using the jump instruction goto.
#include<stdio.h>
void main()
{
	int v=0;
	int c=0;
	char letter='A';
	eticheta:
		switch (letter)
		{
			
			case 'A':
			case 'E':
			case 'I':
			case 'O':
			case 'U': v++;
			break;
			default: c++;
		}
	letter++;
	if(letter<='Z') goto label;
	printf("Number of vowels is %d\n",v);
	printf("The number of consonants is %d\n",c);
 
	getchar();
	int var;
	scanf("%d", var);
}
14) Display the entire alphabet in large letters just to 'M'. Loop ends deliberately by using instruction break.
#include<stdio.h>
void main()
{
for(char letter='A'; letter<='Z'; letter++)
{
		if(letter=='M') break;
		printf("%c", letter);
}
getchar();
	int var;
	scanf("%d", var);
}
15) To convert a number from base 10 in base p, 2<=p<=10.
/* We divide the sequence on X (number given) to p retaining  digits from basis p in a
xpi number, which is then inverted to obtain the number xp,  seen as
based representation p of x. 
To avoid problems if the first
 xpi's digit as 0, use a 1 additional of the figures transform
 x-> xpi, xpi ie a decrement transform xpi-> xp.*/
 
#include<stdio.h>
void main ()
{
int x,xpi,xp,p;
printf ("Enter x in base 10:");
scanf ("%d",&x);
printf("Enter new base p:");
scanf("%d",&p);
//another way to write an instruction for
for (xpi=0; x; xpi=10*xpi+x%p+1,x=x/p);
for (xp=0; xpi; xp=10*xp+(xpi-1)%10,xpi=xpi/10);
printf("Number in base %d is:%d",p,xp);
 
	getchar();
	int var;
	scanf("%d", var);
}
 
/* Note, in this example, using the operator "," in the
for instruction. For example, the first for shall read:
- Xpi is initialized to 0;
- While x is not 0 execute:
- Xpi is 10 * xpi + x% p +1
- X is divided by p (and converting to int).*/
16) Create a program which in a loop "while" calculates sum of the digits of a number entered from the keyboard and displays its inverse.
#include<stdio.h>
#include<math.h>
void main()
{
	int nr;
	int s=0;		//sum of the digits
	int inv=0;		//inverse number
	printf("Enter the number: ");
	scanf("%d",&nr);
	while (nr)		//or while(n!=0)
	{
		s=s+nr%10;
		inv=inv*10+nr%10;
		nr/=10;
	}
/* repetitive instruction above could write so: 
	while(ninv*=10,ninv+=n%10,s+=n%10,n/=10); */
	printf("Sum is: %d",s);
	printf("\nThe inverse is: %d",inv);
 
	getchar();
	int var;
	scanf("&d", var);
}
17) Being given three integers determine if they can be the measures of triangle and if so calculate the perimeter.
#include<stdio.h>
int triangle(int a,int b,int c)
{ 
	if((a>0)&&(b>0)&&(c>0)&&(a<b+c)&&(b<a+c)&&(c<a+b))
		return 1;
	else 
		return 0;
}
void main()
{
	int a,b,c,s=0;
	printf("a= ");scanf("%d",&a);
	printf("b= ");scanf("%d",&b);
	printf("c= ");scanf("%d",&c);
	if(triangle(a,b,c)==1)
	{
		s=a+b+c;
		printf("Perimeter of triangle ABC is: %d",s);
	}
	else
 printf("It is not triangle");
 
	getchar();
	int var;
	scanf("&d", var);
}
18) Calculate value of function f defined on the set of real numbers for x read:
     | x-3, ptr. x<5
   F=| x+1, ptr. 5>x>25
     | x-5x+6,ptr. x>25
#include<stdio.h>
void main()
{
	int x;
	printf("Enter the number: ");
	scanf("%d",&x);
 
	if(x>=5&&x<=25)
	printf("F(x)= %d",x+1);
	else
	{
		if(x<5)
			printf("F(x)= %d",x-3);
		else
			printf("F(x)= %d",x-5*x+6);
	}
	getchar();
	int var;
	scanf("&d", var);
}
19) Display sum of the digits of an given integer. Hint: Each digit detaches from the rest end calculating remainder of number 10, the number remaining after separation of digit is sin division to 10.
#include<stdio.h>
void main()
{
	int n,s=0;
	printf("n= ");scanf("%d",&n);
	while(n!=0)
	{
		s=s+n%10;
		n=n/10;
	}
	printf("Sum of the digits is: %d",s);
 
	getchar();
	int var;
	scanf("&d", var);
}
20) Calculate the sum: S=1!+2!+3!+...+n!
#include<stdio.h>
int factorial(int n)
{
	int p=1;
	for(int i=1;i<=n;i++)
	p*=i;
	return p;
}
void main()
{
	int m,i,s=0;
	printf("\n\tEnter the number: ");scanf("%d",&m);
	for(i=1;i<=m;i++)
	s=s+factorial(i);
	printf("\tSum is: %d",s);
	getchar();
	int var;
	scanf("&d", var);
}
21) Calculate product: P=2*4*6*8*10*...*n, where n=2k.
#include<stdio.h>
void main()
{
	int n,i,p=1;
	printf("Enter number: ");
	scanf("%d",&n);
	n=n*2;
 
	for(i=2;i<=n;i++)
	{
		if (!(i%2))	//could write if(i%2==0) <=> tests if is even			
        p*=i;		
	}
 	printf("The product is: %d",p);
	getchar();
	int var;
	scanf("&d", var);
}
22) Calculate the sum: S=1^2+2^2+3^2+4^2+...+n^2.
#include<stdio.h>
void main()
{
	int n,s=0,i;
	printf("n= ");
	scanf("%d",&n);
 
	for(i=1;i<=n;i++)
	s=s+i*i;
 
	printf("Sum is equal to: %d",s);
	
	getchar();
	int var;
	scanf("&d", var);
}
23) Calculate the sum: S=1-1*2+1*2*3-...±1*2*3*...*n.
#include<stdio.h>
int factorial(int n)
{
	int p=1;
	for(int i=1;i<=n;i++)
	p*=i;
	return p;}
 
void main()
 
{
	int n,i,s=0,sign=1;
	printf("n= ");scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		s=s+sign*factorial(i);
		sign=-1;
	}
	printf("Sum is equal to: %d",s);
	
	getchar();
	int var;
	scanf("&d", var);
}
24) Convert a number from base 10 to base p <10.
#include<stdio.h>
void main()
{
	int n,m,p,y=0,c;
	printf("Number in base 10 is: ");scanf("%d",&n);
	printf("Base in which is converts: ");scanf("%d",&p);
 
	while(n!=0)
	{
		c=n%p;
		y=y*10+c;
		n=n/p;
	}
 
	printf("Number in base %d is: %d",p,y);
	
	getchar();
	int var;
	scanf("&d", var);
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.