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

lab c 4

The document contains four C programming examples. Example 1 generates a random 2D array, flattens it, and sorts it in ascending and descending order. Example 2 checks if a number is even or odd, Example 3 solves a quadratic equation, and Example 4 reverses an integer.

Uploaded by

An Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lab c 4

The document contains four C programming examples. Example 1 generates a random 2D array, flattens it, and sorts it in ascending and descending order. Example 2 checks if a number is even or odd, Example 3 solves a quadratic equation, and Example 4 reverses an integer.

Uploaded by

An Tran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex 1:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main(void){
int m,n;
printf("Enter column and row : ");
scanf("%d %d", &m,&n);
int a[m][n];
srand(time(NULL));
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
a[i][j] = (rand()%(101));
}
}
printf("The original array is :\n ");
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++){
printf("%d ", a[i][j]);
}
printf("\n");
}
int b[m*n],l = 0,size = m*n;
for(int i = 0 ; i < m ; i++){
for(int j = 0 ; j < n ; j++ ){
b[l] = a[i][j];
l++;
}}
for(int i = 0 ; i < l; i++){
printf("%d ", b[i]);
}
printf("\nThe ascending order is :\n");
for(int i = 0 ; i < size ; i++){
for( int j = i+1; j< size ; j++){
if(b[i] > b[j]){
int temp = b[i];
b[i] = b[j];
b[j] = temp;
}

}
}
for(int i = 1 ; i <l+1 ; i++){
printf("%d ", b[i-1]);
if(i % 4 == 0 ){
printf("\n");
}
}

printf("\nThe descending order is :\n");


for(int i = 0 ; i < size ; i++){
for( int j = i+1; j< size ; j++){
if(b[i] < b[j]){
int temp = b[i];
b[i] = b[j];
b[j] = temp;
}

}
}
for(int i = 1 ; i <l+1 ; i++){
printf("%d ", b[i-1]);
if(i % 4 == 0 ){
printf("\n");
}
}

return 0;
}

Ex 2 :
#include <stdio.h>
int main(void){
int a;
printf("Enter a number : ");
scanf("%d",&a);
if(a % 2 == 0 ){
printf("This number is even\n");
}
else{
printf("This number is odd\n");
}
return 0;
}

Ex 3:
#include <stdio.h>
int main(void){
float a;
float b;
float c;
printf("Please input a :");
scanf("%f/n", &a);
printf("Please input b :");
scanf("%f/n", &b);
printf("Please input c :");
scanf("%f/n", &c);
printf("For ax^2 + bx + c + 0\n");
if (b*b - 4*a*c > 0 ) {
printf("This equation has 2 distinctive solutions\n");
printf(" The first solution is : %.3f\n", (-b - (b*b - 4*a*c )) / 2*a);
printf(" The first solution is : %.3f\n", (- b + (b*b - 4*a*c ))/ 2*a);
}
if (b*b - 4*a*c < 0 ){
printf("The equation has no real solution\t");}
else{
printf("The equation has 1 solution : %.3f\n",- b / (2*a) );
}
return 0;
}

Ex 4:
#include <stdio.h>
int main(void){
int a,b = 0 ,c;
printf("Input integer a:");
scanf("%d", &a);

while(a != 0 ){
c = a % 10;
b = b*10 + c;
a /= 10 ;
}
printf("Reverse number is : %d ", b);
}

You might also like