You are on page 1of 13

printf("enter time in seconds\n");

LAB 1
scanf("%d",&t);

Ex 4 h= t/3600;

#include<stdio.h> m= (t - 3600*h)/60;

main() s= (t-3600*h-60*m);

{ printf("\n\nTime is %d hours and %d mins

int a,b; and %d secs", h, m, s);

printf("enter a and b:\n"); }

scanf("%d%d",&a,&b);

a= a + b; Ex 6

b= a-b; #include<stdio.h>

a= a-b ; main()

printf("the new a is %d\n", a); {

printf("the new b is %d\n", b); float money, curren;

} #define _dol 0.437905;

printf("give me the amount of money in

Ex 5 Dollar\n");

#include<stdio.h> scanf("%f", &money);

int main() curren= money/ _dol;

{ printf("it is %f in TND", curren);

int t, h, m, s; }
printf("your character %c is a special Ex 3
LAB 2
character", c); #include<stdio.h>

Ex 1 } main()

#include<stdio.h> {

main() Ex 2 float gs, s;

{ #include<stdio.h> printf("enter your salary \n");

char c; main() scanf("%f", &s);

printf("enter a character \n"); { if (s<= 10000)

scanf("%c",&c); float a, b, c; gs= 2*s;

printf("your character is %c=%d \n", c, c ); printf ("enter three numbers a, b and c\n"); else

if (c>= 'a' && c <= 'z') scanf("%f%f%f", &a, &b, &c); if (s<10000 && s<2000)

printf("your character %c is a lowercase", if (a>b && a>c) gs= 2.15* s;

c); printf("%.2f is the largest number", a); else

else else gs= 2.25*s ;

if (c>= 'A' && c <= 'Z') if (b>a && b>c) printf("%.2f is the gross salary", gs);

printf("your character %c is an uppercase", printf("%.2f is the largest number", b); }

c); else

else printf("%.2f is the largest number", c);

if (c>= '0' && c <= '9') }

printf("your character %c is a digit", c);

else
Ex 4 sr= sqrt(sum); break;

#include<stdio.h> printf("the square root is %f \n", sr); case 'y':

#include<math.h> } printf("its a vowel");

main() else break;

{ {power= (sum*sum); case 'e':

int sum, a, b, c, d, power; printf("the power is %d\n ", power); printf("its a vowel");

float sr; } break;

printf("enter an integer containing 3 digits } case 'i':

\n"); SWITCH CASE printf("its a vowel");

scanf("%d", &a); break;

d= a/100; Ex 1 case 'o':

b= (a- 100*d - c )/10 ; #include<stdio.h> printf("its a vowel");

c= a%10; main() break;

sum= c+b+d; { case 'u':

printf(" its sum of digits is %d \n", sum); char c; printf("its a vowel");

if (sum %2 == 1 ) printf("enter the chatacter \n"); break;

printf("your number is odd\n"); scanf("%c", &c); case 'A':

else switch(c) printf("its a vowel");

printf("your number is even\n"); { break;

if (sum %2 == 1 ) case 'a': case 'Y':

{ printf("its a vowel"); printf("its a vowel");


break; main() break ;

case 'E': { case '/':

printf("its a vowel"); float a, b, s; s= a/b;

break; char op; printf("a divided by b is %.2f ", s);

case 'I': printf ("enter the operator\n"); break ;

printf("its a vowel"); scanf("%c", &op); default:

break; printf("enter two numbers a and b \n"); printf("error");

case 'O': scanf("%f%f", &a, &b); }

printf("its a vowel"); switch (op) }

break; {

case 'U': case '+':

printf("its a vowel"); s= a+b;

break; printf("the sum is %.2f ", s);

default: break ;

printf("its a consonant"); case '-':

} s= a-b;

} printf(" b substracted from a is %.2f ", s);

break ;

Ex 2 case '*':

#include<stdio.h> s= a*b;

#include<math.h> printf("the multiplication is is %.2f ", s);


} {int i,n,s=0;
LAB3
} do{
ex 1 Ex 2 printf("enter the number n:\n");
FOR : #include<stdio.h> scanf("%d",&n);
#include<stdio.h> main() }while(n<=0);
main() { for (i=1;i<=n;i++)
{ int b, i,r; {
char i; do s=s+i;
for (i='a'; i<='z'; i++) { printf("the sum of the first numbers of %d is
printf("%c\n", i); printf("enter a variable b\n"); %d\n",i,s);}
} scanf("%d", &b);} }
WHILE while (b<0);
#include<stdio.h> for (i= 0; i<11; i++)
main() {
{ r= b*i;
char c; printf("%d*%d = %d \n ",b,i,r );
c='a'; }
while(c<='z') }
{ EX 3
printf("%c\n",c); #include<stdio.h>
c++; main()
} for (i=5; i>x; i--)
LAB 4
for (i=0; i<6; i++) {
ex 1
printf ("%d | ", t[i]); t[i]=t[i-1];
#include<stdio.h>
} }
main()
Ex 2 t[x]=v;
{
#include<stdio.h> for (i=0; i<5; i++)
int t[6];
main() printf("%d |", t[i]);
int n, j, i, num;
{ }
for (i=0; i<6; i++)
int t[5]; Ex 3
{printf("enter the element number %d in the
int n, aux, i,x,v; #include<stdio.h>
table \n", i+1);
for (i=0; i<5; i++) main()
scanf("%d", &t[i]);
{printf("enter the element number %d in the {
}
table \n", i+1); int t[10];
for (i=0; i<5; i++)
scanf("%d", &t[i]); int n, aux, i,x,v, j;
{for (j=i+1; j<6; j++)
} for (i=0; i<10; i++)
{if (t[i]>t[j])
do {printf("enter the element number %d in the
{
{printf("enter the position i \n"); table \n", i+1);
num= t[i];
scanf("%d", &x); scanf("%d", &t[i]);
t[i]= t[j];
}while ((x<0) || (x>5)); }
t[j]=num;}
printf("enter the number V \n"); printf("enter the number V \n");
}
scanf("%d", &v); scanf("%d", &v);
for (i=0; i<10;i++) printf("enter the size of the matrix as the scanf("%d",&B[row][col]);

{ form of n*m\n"); }

if (t[i]== v) scanf("%d,%d",&n,&m); }

{ printf("enter the number of columns in the for (row=0;row<n;row++)

for (j=i; j<9; j++) matrix B\n"); {

{t[j]=t[j+1]; scanf("%d",&p); for(col=0;col<p;col++)

} printf("Enter elements in matrix A of size {

t[9]=0; %d*%d \n",n,m); for(k=0;k<m;k++)

i=10; for (row=0;row<n;row++) {s=s+A[row][k]*B[k][col];}

} { C[row][col]=s;

} for(col=0;col<m;col++) s=0;

for (i=0; i<10; i++) { }

printf("%d |", t[i]); scanf("%d",&A[row][col]); }

} } for (row=0;row<n;row++)

Part II: Multi-dimensional array } {for(col=0;col<p;col++)

Ex 1 printf("\nEnter elements in matrix B of size {

#include<stdio.h> %d*%d \n", m,p); printf("%d |",C[row][col]);

main() for (row=0;row<m;row++) }printf("\n");

{ { }

int A[100][100],B[100][100],C[100][100]; for(col=0;col<p;col++) }

int row,col,n, b,k,s=0,m,p; { Ex 2


#include<stdio.h> for (row=0;row<n;row++) for (row=0;row<n;row++)

#include <stdlib.h> { {

main() for(column=0;column<m;column++) for(column=0;column<=row;column++)

{ int row, column,n,s,m; { {

int T[100][100], B[100][100]; { {

printf("enter the size of the square matrix as B[row][column]=T[column][row]; printf("%d|", T[row][column]);

the form of n*m \n"); } }

scanf("%d,%d",&n,&m); } }

printf("the values of the matrices are chosen printf("\n"); printf("\n");

randomly\n"); } }

for (row=0;row<n;row++) for (row=0;row<n;row++) }

{ {

for(column=0;column<m;column++) for(column=0;column<m;column++)

{ {

{ {

T[row][column] =rand()%200; printf("%d|", B[row][column]);

printf("%d|", T[row][column]); }

} }

} printf("\n");

printf("\n"); }

} printf("\n");
{ printf("After concatenation the string is : \n
Lab5 Strings
char str1[100], str2[100], i, j,l,m,k; ");
Exercice 1: printf("Concatenate Two Strings for(i=0; i<k; ++i)
#include<stdio.h> Manually :\n"); {
main() printf("-------------------------------------\n"); printf("%c",str1[i]); } }
{ printf("Input the first string : "); Exercice 3 :
char s1[100], s2[100]; gets(str1); #include<stdio.h>
int i; printf("Input the second string : "); main(){
printf("Enter string s1: "); gets(str2); char line[150];
scanf("%s",s1); l=strlen(str1); /* int i,v,c,ch,d,s,o;
for(i=0; s1[i]!='\0'; ++i) m=strlen(str2); o=v=c=ch=d=s=0; */
{ for(i=0; i<l-1; ++i); /* value i contains int i,v,c,d,s;
s2[i]=s1[i]; reaches the end of string str1. */ v=c=d=s=0;
} str1[i]=' '; /* add a space with string str1. */ printf("Enter a line of string:\n");
s2[i]='\0'; i++; /* value i increase by 1 for the blank gets(line);
printf("String s2: %s",s2); space */ for(i=0;line[i]!='\0';++i)
} for(j=0; j<m-1; ++j, ++i) {
Exercise 2: { if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
#include <stdio.h> str1[i]=str2[j]; line[i]=='o' || line[i]=='u' || line[i]=='A' ||
#include <string.h> } line[i]=='E' || line[i]=='I' || line[i]=='O' ||
main() k=strlen(str1); line[i]=='U')
++v; #include <stdio.h> void main(){

else if((line[i]>'a'&& line[i]<'z') || #include <stdlib.h> char str[150];

(line[i]>'A'&& line[i]<'Z')) void main() int i,j;

++c; { printf("\n\nRemove characters in String

else if(line[i]>='0'&&c<='9') char str[100]; /* Declares a string of size 100 Except Alphabets :\n");

++d; */ printf("---------------------------------------------

else if (line[i]==' ') int l= 0; -----\n");

++s;} printf("\n\nSeparate the individual characters printf("Input the string : ");

printf("Vowels: %d",v); from a string :\n"); gets(str);

printf("\nConsonants: %d",c); printf("--------------------------------------------- for(i=0; str[i]!='\0'; ++i)

printf("\nDigits: %d",d); ---------\n"); {

printf("\nWhite spaces: %d",s);} printf("Input the string : "); while (!((str[i]>='a'&&str[i]<='z') ||

int x = 'A'; // x = 65 gets(str); (str[i]>='A'&&str[i]<='Z' || str[i]=='\0')))

int y = 'a'; // x = 97 printf("The characters of the string are : \n"); {

int z = 'Z'; // x = while(str[l]!='\0') for(j=i;str[j]!='\0';++j)

int a = 'z'; // x =122 {printf("%c ", str[l]); {

printf("\nAscii code of A: %d",x); l++;} str[j]=str[j+1];

printf("\nAscii code of a: %d",y); printf("\n");} }str[j]='\0';} }

printf("\nAscii code of Z: %d",z); Exercise 5: printf("After removing the Output String :

printf("\nAscii code of z: %d",a); #include <stdio.h> %s\n\n",str); }

Exercise4: #include <string.h>


printf("5. Modulus\n");} printf("Choose the operation choice from 1
Correction of LAB6: C
int Add(int number1, int number2) to 5\n");

programing Functions {return number1+number2;} scanf("%d",&op);

int Substract(int number1, int number2) }while(op>5 || op<1);


Exercise 1:
{return number1-number2;} printf("Enter 2 integers \n");
#include<stdio.h>
int Multiply(int number1, int number2) scanf("%d%d",&number1,&number2);
//void Display();
{return number1*number2;} switch(op)
//int Add();
float Divide(int number1, int number2) {
//int Substract();
{return number1/number2;} case 1: result= Add(number1,number2);
//int Multiply ();
int Modulus(int number1, int number2) break;
//float Divide ();
{return number1%number2;} case 2: result= Substract(number1,number2);
//int Modulus();
void Display_result(float result) break;
void Display()
{printf("the result of your choice is %2.2f", case 3: result= Multiply(number1,number2);
{
result);} break;
printf("------------------------------------\n");
void main() case 4: result= Divide(number1,number2);
printf(" MENU \n");
{ break;
printf("-----------------------------------\n\n");
int op ,number1, number2; case 5: result= Modulus(number1,number2);
printf("1. Add\n");
float result; break;
printf("2. Substract\n");
Display(); default:("There is a problem");
printf("3. Multiply\n");
do{ }
printf("4. Divide\n");
Display_result(result);
} printf("\n\n");} {

Exercise 2: } if(T[i]==X)

# include<stdio.h> /*find the value x in the array */ nb++;

/******function void Fill_array(int T[], int int Lookup(int T[], int N, int X) }

N);*****/ { return nb;

void Fill_array(int T[], int N) int test=0,i; }

{ for(i=0;i<N;i++) main()

int i; { {int T[100],N, v;

for(i=0;i<N;i++) if(T[i]==X) do{

{ { printf("Give the size of the array");

do{ test =1; scanf("%d",&N);

printf("T[%d]=\t",i); break; }

scanf("%d",&T[i]); } while(N<0);

} else printf("Fill array\n");

while(T[i]<0);}} test=0; Fill_array(T, N);

/******function display array*****/ }return test;} printf("Display array\n");

void DisplayArray (int T[],int N) /*find the Occurence value x in the array */ DisplayArray(T, N);

{ int Occurence(int T[], int N, int X) printf("give a value X");

int i; { scanf("%d",&v);

for(i=0;i<N;i++) int test=0, nb=0,i; printf("The fucntion Lookup

{printf("%d\t",T[i]); for(i=0;i<N;i++) %d\n",Lookup(T, N, v));


printf("The fucntion Occurence printf("random generation\n"); void bottomHalfofMatrix(int tab[50][50], int

%d\n",Occurence(T, N, v)); for(int i = 0; i < rows; i++){ n)

} for( int j = 0; j < columns; j++){ {

Exercise 3: M1[i][j] = rand()%10; for(int i = 0; i < n; i++){

#include <stdio.h> printf("[%d]",M1[i][j]); for(int j = 0; j < i; j++){

#include <stdlib.h> } printf("[%d]",tab[i][j]);

#include <time.h> printf("\n"); }

int dimensionOftheMatrix() }} printf("\n");

{int n; void transposedMatrix(int transposed[50] }}

do{ [50], int tab[50][50],int rows, int columns) main()

printf("enter a number of rows less than 10: { {

"); printf("transposed matrix\n"); int tab[50][50];

scanf("%d",&n); //transpose the matrix int transposed [50][50];

}while((n <= 0) || (n > 10)); for(int i = 0; i < columns; i++){ int rows, columns;

return(n); for( int j = 0; j < rows; j++){ int i, j, n;

} transposed[i][j] = tab[j][i]; n=dimensionOftheMatrix();

void fillMatrix( int M1[50][50],int rows, int printf("[%d]",transposed[i][j]); fillMatrix(tab,n,n);

columns) } transposedMatrix(transposed,tab, n, n) ;

{ printf("\n"); bottomHalfofMatrix(tab, n);}

//int M1[50][50]; }}

srand(time(NULL)) ;

You might also like