0% found this document useful (0 votes)
20 views

C Langage Exercises

The document contains 4 coding problems in C: 1. Write a calculator program that takes a mathematical operation as input from the user, performs the calculation, and outputs the result. It uses a switch statement. 2. Write a program to calculate the square root of a number without using the sqrt() function, instead using a while loop. 3. Write a program to graph the cubic function x^3 by printing asterisks in the appropriate positions. 4. Write a program to find the smallest positive number that is divisible by all integers between 1 and 10, which is 2520. It uses a while loop to test divisibility.

Uploaded by

Abdelhak Bajji
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

C Langage Exercises

The document contains 4 coding problems in C: 1. Write a calculator program that takes a mathematical operation as input from the user, performs the calculation, and outputs the result. It uses a switch statement. 2. Write a program to calculate the square root of a number without using the sqrt() function, instead using a while loop. 3. Write a program to graph the cubic function x^3 by printing asterisks in the appropriate positions. 4. Write a program to find the smallest positive number that is divisible by all integers between 1 and 10, which is 2520. It uses a while loop to test divisibility.

Uploaded by

Abdelhak Bajji
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1-Write a code in C to create a calculator, user input the full operation and the result is as shown

bellow :

Input : 5.7*3

Output : =17.10

Use switch statement !

#include<stdio.h>

int main()

float first,second,result;

char operation;

printf("Enter your operation:\n");

scanf("%f%c%f",&first,&operation,&second);

switch(operation) {

case '+' :

result=first + second;

break;

case '-':

result=first - second;

break;

case '*' :

result=first * second;

break;

case '/':

result=first / second;

break;

default:

printf("Erreur!");

exit(1) ;

}
printf("=%.2f",result);

2-Write a code in C that find the square root of a number given with in the format of 0.000,
without using sqrt() function, but using a while loop:

Exemple :

Input : 5 Output : 2.2360

#include<stdio.h>

int main()

float number,squareRoot=0;

printf("input a number:\t");

scanf("%f",&number);

while(squareRoot*squareRoot<number){

squareRoot+=0.00001;

printf("%.4f",squareRoot);

3-Write a code in C that draw the graph of x^3:

Exemple :

***

*
*

#include<stdio.h>

#define func (x*x*x)

int main()

for(int y=30;y>=-30;y--){

for(float x=-4.2;x<=4.2;x+=0.2){

if(func>y-0.2&func<y+0.2){

printf("*");

else {printf(" ");}

printf("\n");

4-Write a code in C that find the number divisible by all the number between 1 to 10 :

The ouptut should be 2520 !

#include<stdio.h>

int main()

int tr,x=0;

while(tr<10) {
tr=0;

x++;

for(int i=1; x%i==0; i++)

tr++;

printf("%d",x);

You might also like