C program part b
C program part b
Here is source code of the C program to sort the numbers in ascending order
using bubble sort. The C program is successfully compiled and run on a Linux
system. The program output is also shown below.
1. /*
2. * C program to sort N numbers in ascending order using Bubble sort
3. * and print both the given and the sorted array
4. */
5. #include <stdio.h>
6. #define MAXSIZE 10
7.
8. void main()
9. {
10. int array[MAXSIZE];
11. int i, j, num, temp;
12.
13. printf("Enter the value of num \n");
14. scanf("%d", &num);
15. printf("Enter the elements one by one \n");
16. for (i = 0; i < num; i++)
17. {
18. scanf("%d", &array[i]);
19. }
20. printf("Input array is \n");
21. for (i = 0; i < num; i++)
22. {
23. printf("%d\n", array[i]);
24. }
25. /* Bubble sorting begins */
26. for (i = 0; i < num; i++)
27. {
28. for (j = 0; j < (num - i - 1); j++)
29. {
30. if (array[j] > array[j + 1])
31. {
32. temp = array[j];
33. array[j] = array[j + 1];
34. array[j + 1] = temp;
35. }
36. }
37. }
38. printf("Sorted array is...\n");
39. for (i = 0; i < num; i++)
40. {
41. printf("%d\n", array[i]);
42. }
43. }
$ cc pgm21.c
$ a.out
Enter the value of num
6
Enter the elements one by one
23
45
67
89
12
34
Input array is
23
45
67
89
12
34
Sorted array is...
12
23
34
45
67
89
Linear Search in C
#include <stdio.h>
int main()
{
scanf("%d",&n);
scanf("%d", &a[i]);
scanf("%d", &item);
if (item == a[i])
break;
if (i > 9)
return 0;
Copy
Enter elements:
2 3 5 7 8 6 4 1
C program to create two matrixes. Add the values of the two matrixes and store it in
another matrix. Display the new matrix.
C Program
1. #include <stdio.h>
2. #include <conio.h>
3. void main()
4. {
5. int a[2][3],b[2][3],c[2][3],i,j;
6. clrscr();
8. for(i=0;i<2;i++)
9. for(j=0;j<3;j++)
10. scanf("%d",&a[i][j]);
12. for(i=0;i<2;i++)
13. for(j=0;j<3;j++)
14. scanf("%d",&b[i][j]);
15. for(i=0;i<2;i++)
16. for(j=0;j<3;j++)
17. c[i][j]=a[i][j]+b[i][j];
19. for(i=0;i<2;i++)
20. {
21. for(j=0;j<3;j++)
22. printf("%5d",c[i][j]);
23. printf("\n");
24. }
25. getch();
26. }
Output
C program to find sum
and difference of two
matrices
#include <stdio.h>
int main()
printf("%d\t", a[ i ][ j ] + b[ i ][ j ]);
printf ("\n");
printf("%d\t", a[ i ][ j ] - b[ i ][ j ]);
printf("\n");
Output:
Output:
1 is a Fibonacci Number
2 is a Fibonacci Number
3 is a Fibonacci Number
4 is a not Fibonacci Number
5 is a Fibonacci Number
6 is a not Fibonacci Number
7 is a not Fibonacci Number
8 is a Fibonacci Number
9 is a not Fibonacci Number
10 is a not Fibonacci Number