Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

C program part b

Uploaded by

Manhar M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

C program part b

Uploaded by

Manhar M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

C Program to Sort N Numbers in Ascending

Order using Bubble Sort


This C Program sorts the numbers in ascending order using bubble sort.
Bubble sort is a simple sorting algorithm that works by repeatedly stepping
through the list to be sorted, comparing each pair of adjacent items and
swapping them if they are in the wrong order. Here we need to sort a number
in ascending order.

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()
{

int a[10], i, item,n;

printf("\nEnter number of elements of an array:\n");

scanf("%d",&n);

printf("\nEnter elements: \n");

for (i=0; i<n; i++)

scanf("%d", &a[i]);

printf("\nEnter item to search: ");

scanf("%d", &item);

for (i=0; i<=9; i++)

if (item == a[i])

printf("\nItem found at location %d", i+1);

break;

if (i > 9)

printf("\nItem does not exist.");

return 0;

Copy

Output for Linear Search in C:


Enter number of elements of an array:
8

Enter elements:
2 3 5 7 8 6 4 1

Enter item to search: 1

Item found at location 8


C program to accept two square
matrix and find sum of two
matrix
Addition of two matrix using C
Levels of difficulty: Basic / perform operation: Matrix

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();

7. printf("\nENTER VALUES FOR MATRIX A:\n");

8. for(i=0;i<2;i++)

9. for(j=0;j<3;j++)

10. scanf("%d",&a[i][j]);

11. printf("\nENTER VALUES FOR MATRIX B:\n");

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];

18. printf("\nTHE VALUES OF MATRIX C ARE:\n");

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()

int i,j,r1,c1, a[10][10], b[10][10];

printf("Enter Order of Matrix A & B: ");

scanf("%d %d", &r1, &c1);

printf("Enter Elements of Matrix of A: \n");

for( i=0; i< r1; i++)

for( j=0; j<c1; j++)

scanf("%d", &a[ i ][ j ]);

printf("Enter Elements of Matrix of B: \n");

for( i=0; i< r1; i++)

for( j=0; j < c1; j++)

scanf("%d", &b[ i ][ j ]);

printf("\n Matrix Addition \n");

for( i=0; i< r1; i++)

for( j=0; j < c1; j++)

printf("%d\t", a[ i ][ j ] + b[ i ][ j ]);
printf ("\n");

printf("\nMatrix Subtraction/Difference \n");

for( i=0; i< r1; i++)

for( j=0; j < c1; j++)

printf("%d\t", a[ i ][ j ] - b[ i ][ j ]);

printf("\n");

Output:

c program to find a given number fabonancia or not

// C++ program to check if x is a perfect square


#include <bits/stdc++.h>
using namespace std;
// A utility function that returns true if x is perfect
// square
bool isPerfectSquare(int x)
{
int s = sqrt(x);
return (s * s == x);
}

// Returns true if n is a Fibonacci Number, else false


bool isFibonacci(int n)
{
// n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or
// both is a perfect square
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}

// A utility function to test above functions


int main()
{
for (int i = 1; i <= 10; i++)
isFibonacci(i)
? cout << i << " is a Fibonacci Number \n"
: cout << i << " is a not Fibonacci Number \n";
return 0;
}

// This code is contributed by Sania Kumari Gupta (kriSania804)

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

You might also like