Introduction to C - Applications solved
1) Write a program that displays the text: "Hello!".
#include<stdio.h> void main() { printf("Hello!"); getchar(); }
2) ) Write a program that displays the text "Hello!",
so that the message must be sent through a variable of a type adapted correctly.
#include<stdio.h> void main() { char a[]="Hello!"; printf("%s",a); getchar(); }
3) Write a program that displays the text "Hello!"
so that the message appears to be sent through a variable of a type well suited to other types of memory.
#include<stdio.h> void main() { char* a="Hello!"; printf("%s",a); getchar(); }
4) Write a program that displays the text "Hello!"
so that the display is made by means of a user-defined function.
#include<stdio.h> void display() { printf("Hello!"); } void main() { display(); getchar(); }
5) Write a program that displays the text "Hello!"
so that the display is carried out through a universal function, with the possibility of transmission of the chain parameter.
#include<stdio.h> void display(char a[]) { printf("%s", a); } void main() { display("Hello!"); getchar(); }
6) Write a program that displays the text "Hello!"
so that the display is achieved by a universal function, with the possibility of transmission of the string parameter, a different kind of writing.
#include<stdio.h> void display(char* a) { printf("%s", a); } void main() { display("Hello!"); getchar(); }
7) Write a program to enter the desired string from the keyboard, then displays it on the screen.
#include<stdio.h> void main() { char* a; printf("Enter the string from the keyboard:"); scanf("%s",a); printf(a); getchar(); }
8) Write a program that introduced from the keyboard the desired string, then it displays on the screen (other than the above version).
#include<stdio.h> void main() { char* a; printf("Enter the string from the keyboard:"); gets(a); printf(a); getchar(); }
9) Calculate the sum of the integers as follows: given two values directly in the programs stored in two memory locations and calculate their sum.
#include<stdio.h> void main() { int a=3, b=7, c; c=a+b; printf("The sum of two numbers is: %d\n", c); getchar(); }
10) Calculate the sum of integers as follows: read two numbers from the keyboard and calculate their sum.
#include<stdio.h> void main() { int a,b; printf("Enter a="); scanf("%d", &a); printf("Enter b="); scanf("%d", &b); printf("Sum a+b=%d\n", a+b); getchar(); int k; scanf("%d", &k); }
11) Calculate the sum of integers as follows: read two numbers from the keyboard and calculate their sum and the result is obtained by means of a variable.
#include<stdio.h> void main() { int a,b; printf("Enter a:"); scanf("%d", &a); printf("Enter b:"); scanf("%d", &b); int c; c=a+b; printf("a+b=%d\n", c); getchar(); int k; scanf("%d",k); }
12) Calculate the sum of integers as follows: write a function that will sum two integers.
#include<stdio.h> int sum (int x, int y) { int s; s=x+y; return s; } void main() { int a,b; printf("Enter a: "); scanf("%d",&a); printf("Enter b: "); scanf("%d",&b); int c; c=sum(a,b); printf("Sum is: %d\n",c); getchar(); int k; scanf("%d", k); }
13) Calculate the sum of integers as follows: read two nombers from the keyboard and calculate their sum. Will write a function sum of two integers. The function returns the result by line parameters.
#include<stdio.h> void sum(int x,int y,int* s) { *s=x+y; } void main() { int a,b; printf("Enter a: "); scanf("%d",&a); printf("Enter b: "); scanf("%d",&b); int c; sum(a,b,&c); printf("Sum is: %d\n",c); getchar(); int k; scanf("%d", k); }
14) You have to write a function that calculates the arithmetic average of two integers.
#include<stdio.h> int average (int x, int y) { int av; av=(x+y)/2; return av; } void main () { int a, b; printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); scanf("%d", &b); int m; m=average(a,b); printf("Arithmetic average is: %d\n", m); getchar(); int k; scanf("%d", k); }
15) Calculate the arithmetic average of two integers, as follows: read two numbers from the keyboard and their arithmetic mean is calculated. Will write a function that calculates the average of two integers. The function returns the result of the line parameters.
#include<stdio.h> void average(int a, int b, int* c) { *c=(a+b)/2; } void main() { int x,y; printf("Enter x: "); scanf("%d", &x); printf("Enter y: "); scanf("%d", &y); int z; average(x,y,&z); printf("Arithmetic average is: %d", z); getchar(); int k; scanf("%d", k); }
16) Calculate the arithmetic average of two integers.
#include<stdio.h> void main() { int a,b; printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); scanf("%d", &b); int s=a+b; float m=(float)s/2; printf("Sum is: %d\n", s); printf("Arithmetic average is: %.2f", m); getchar(); int k; scanf("%d", k); }
17) Calculate 2 to the power n, where n is an integer read from the keyboard.
#include<stdio.h> void main() { int n; printf("Enter n:"); scanf("%d", &n); int p=1; printf("2^%d=%d",n,p=(p<<n)); //The shift operators shift their first operand left (<<) //or right (>>) by the number of positions the second operand specifies getchar(); int k; scanf("%d", k); }
18) It is read a decimal number and one in hexa up to 4 digits each and are required to display those values in the systems numbered 10, 8 and 16.
#include<stdio.h> void main() { unsigned int a,b; printf("Enter a decimal integer: "); scanf("%d",&a); printf("Enter a hexadecimal integer: "); scanf("%x",&b); printf("\ndecimal integer:\tBase 10: %u\tBase 8: %#o\tBase 16: %#X",a,a,a); printf("\nhexadecimal integer:\tBase 10: %u\tBase 8: %#o\tBase 16: %#X",b,b,b); getchar(); int k; scanf("%d", k); }
19) Make a routine that uses increment (decrement) after and prefixed.
#include<stdio.h> void main() { int val=1; printf("using postfix %d\n", val++); printf("use after incrementing %d\n", val); printf("use prefix%d\n", ++val); printf("use after incrementing %d\n", val); printf("use postfix %d\n", val--); printf("use after decrement %d\n", val); printf("use prefix %d\n", --val); printf("use after decrement %d\n", val); getchar(); int k; scanf("%d", k); }
20) Execute a routine that performs work on bits.
#include<stdio.h> void main() { unsigned int a=5; const int b=7; printf("%d <or on bits> %d is %d\n", a,b, a|b); printf("%d <and on bits> %d is %d\n", a,b, a&b); printf("%d <or exclusively on bits> %d is %d\n", a,b, a^b); printf("5 <complemented on bits> is %u variable is on %u bytes\n",~a,sizeof(a)); getchar(); int k; scanf("%d", k); }
21) Write a program that reads the value of x, positive, displays the values of x, [x], {x},
calculates and displays the value of the following expression : 5*x*[x]-3*{x}+15.
#include<stdio.h> void main() { double x; int y; //for the entire double z; //for the decimal printf("Enter x:"); scanf("%lf", &x); printf("x= %g\t", x); printf("[x]= %d\t", y=x); printf("{x}= %f\t", z=x-y); printf("e=%g\n", 5*x*x-3*z+15); getchar(); int k; scanf("%d", k); }
22) Determine the maximum and minimum of two integers.
#define max(x,y) x>y?x:y //#define=give meaningful name to a constant from program #define min(x,y) x<y?x:y #include<stdio.h> void main() { int x,y; printf("Enter x: "); scanf("%d", &x); printf("Enter y: "); scanf("%d", &y); printf("\n\Max is: %d\n", max(x,y)); printf("\n\Min is: %d\n", min(x,y)); getchar(); int k; scanf("%d", k); }
23) It is given three real nombers a, b, c. They can represent the lengths of the sides of a triangle?
#include<stdio.h> void main() { float a,b,c; printf("Give side lengths:\n"); scanf("%f %f %f", &a, &b, &c); (a>=0 && b>=0 && c>=0 && a<b+c && b<a+c && c<a+b)?printf("\t %4.2f, %4.2f si %4.2f triangle formed.",a,b,c):printf ("\t Not form triangle."); getchar(); int k; scanf("%d", k); }
24) Write a program that reads n integer and displays the value of the expression:
n / (n +1) with 15 decimal places.
#include<stdio.h> void main() { int n; printf("Enter n:"); scanf("%d", &n); float e; e=(float)n/(n+1); printf("Expression value is: %.15f", e); getchar(); int k; scanf("%d", k); }
25) Determine the maximum and the minimum of two integers, using macros.
#define max(x,y) x>y?x:y #define min(x,y) x<y?x:y #include<stdio.h> void main() { int x, y; printf( "Enter x and y:\n"); scanf("%d %d", &x, &y); printf("\n\tMax is: %d", max(x,y)); printf("\n\tMin is: %d", min(x,y)); getchar(); int k; scanf("%d", k); }
26) Write a program to calculate the number of feet in a yard, where he yag g chickens, p cats and a man.
#include<stdio.h> void main() { int g,p; printf("\nEnter the number of chickens:" ); scanf("%d", &g); printf("\nEnter the number of cats:" ); scanf("%d", &p); printf("\n\tNumber of legs is: %d", g*2+p*4+2); getchar(); int k; scanf("%d", k); }
27) Use conditional operator to check if x is a real number in [a, b), where a and b are given by the keyboard.
#include<stdio.h> void main() { float x; printf("Enter x:"); scanf("%f", &x); int a,b; printf("Enter a: "); scanf("%d", &a); printf("Enter b: "); scanf("%d", &b); (x>=a&&x<b) ? printf("\n\tX is in the interval[%d %d)", a,b) : printf("\n\tX not in the interval [%d %d)", a,b); getchar(); int k; scanf("%d", k); }
28) Write a program that reads the value of the variable x and displays the value of the function f(x) :
|3*x*x+7*x-10 x<0 f(x)= |2 x=0 |4*x*x x>0
#include<stdio.h> void main() { int x; printf("Enter x:"); scanf("%d", &x); if(x==0) printf("F(x)=2"); else if(x<0) printf("F(x)=%d",3*x*x+7*x-10); else printf("F(x)=%d",4*x*x); getchar(); int k; scanf("%d", k); }