Professional Documents
Culture Documents
Chapitre 4-Informatique 1 C Programming
Chapitre 4-Informatique 1 C Programming
Chapter 4 :
C programming
language
1
© Semestre 1
Ecole Supérieure des Sciences Appliquées - Tlemcen
Table of contents
OVERVIEW
c Programs
c library
c variables declaration
Basic statements
My First c program
Conditional statements (if..else)
switch statement
Iteration statement/Loop
Break and Continue statement 2
Semestre 1
Chapter 4: C programming language
OVERVIEW
3
© Semestre 1
Chapter 4: C programming language
C program
Main Function
4
© Semestre 1
Chapter 4 : C programming language
C Comments
In C, you can insert text as comments to provide explanations, notes,
or documentation within your code. Comments are ignored by the C
compiler and are intended solely for human readers.
C library
6
© Semestre 1
Cours 4 : Algorithmique et programmation
C variables declaration
In C, you declare variables with a specific data type to specify the
type of data and name that variable can store.
Example for
Integer Variables (int):
int age;
int x, y, z; // You can declare multiple int variables in one line.
Assignment
8
© Semestre 1
Chapter 4 : C programming language
Input statement(scanf)
The scanf function is a standard input function in the C
programming language, and it is used to read formatted data
from the standard input (usually the keyboard) into variables.
scanf("%f", &Variable);
10
© Semestre 1
Chapter 4: C programming language
My First c program
#include <stdio.h>
float number, square;
int main() {
printf("Enter a number: ");
scanf("%f", &number);
square = number * number;
printf("The square of %lf is %lf\n", number, square);
return 0; }.
11
© Semestre 1
Chapter 4 : C programming language
C code
#include <stdio.h>
int A,B;
int main ()
{
printf("donnez le premier nombre");
scanf(" %d ", &A);
printf("donnez le deuxième nombre");
scanf(" %d ", &B);
if (A > B) {
printf(" %d est supérieur à %d", A,B);
}
else {
printf(" %d est supérieur à %d", B,A);
}
13
return 0; }
© Semestre 1
Chapter 4 : C programming language
switch statement
14
© Semestre 1
Cours 4 : Algorithmique et programmation
switch statement
Example (basic calculator)
#include <stdio.h>
char c; float a,b,res;
int main () {
scanf("%f",&a) ;
scanf("%f",&b) ;
scanf("%c",&c) ;
switch(c) { case '+': res=a+b;
break;
case '-': res=a-b;
break;
case '*': res=a*b;
break;
case '/': if (b!=0) res=a/b;
else printf("erreur de division par zero");
break;
default: printf(" cette opération est erronées");
}
printf("Le Résultat = %f",res);
return 0 ;} 15
© Semestre 1
Chapter 4 : C programming language
Iteration statement/Loop
Repeat begin_repeat do {
instructions instructions;
end_repeat }
while (Condition) While (Condition);
16
© Semestre 1
Chapter 4 : C programming language
#include <stdio.h>
#include <math.h>
int N,i; float S;
int main ()
{ printf("donner un nombre N:");
scanf("%d",&N) ;
S=0 ;
for (i=1; i<=N ; i++)
{
S=S+ (i+1)/(-pow(2*i+1,2));
}
printf("La somme = %f ",S) ;
return 0 ;}
17
© Semestre 1
Chapter 4 : C programming language
#include <stdio.h>
int i,j;
int main() {
for(i=0,j=10; i <=5,j>=5; i++,j--){
printf("i=%d ", i);
printf("j=%d \n", j);
}
return 0; }
18
© Semestre 1
Chapter 4 : C programming language
i=1 ;
F=1 ;
while (i<=n)
{
F=F*i ;
i++ ;
}
printf("le factoriel de %d est %d",n,F) ;
return 0 ;}
20
© Semestre 1
Chapter 4 : C programming language
21
© Semestre 1
Chapter 4 : C programming language
22
© Semestre 1
Chapter 4 : C programming language
#include <stdio.h>
int d ;
int main () {
for (d=100; d>=0; d-=10){
if(d == 50) {
continue; }
printf("d= %d \n", d);
}
return 0 ;}
23
© Semestre 1