0% found this document useful (0 votes)
3 views38 pages

C Program With Error

The document contains multiple C programs demonstrating various algorithms and functionalities, including removing duplicates from a sorted array, reversing a stack using recursion, counting and averaging numbers, sorting arrays with quicksort and mergesort, performing matrix operations, displaying prime numbers, finding the transpose of a matrix, and more. Each program includes code snippets, expected outputs, and user prompts for input. The programs cover a range of topics suitable for learning basic to intermediate C programming concepts.

Uploaded by

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

C Program With Error

The document contains multiple C programs demonstrating various algorithms and functionalities, including removing duplicates from a sorted array, reversing a stack using recursion, counting and averaging numbers, sorting arrays with quicksort and mergesort, performing matrix operations, displaying prime numbers, finding the transpose of a matrix, and more. Each program includes code snippets, expected outputs, and user prompts for input. The programs cover a range of topics suitable for learning basic to intermediate C programming concepts.

Uploaded by

mayere10
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1 .

C Program to Remove duplicates from the


sorted array:

Program:
include<StdIO.h>
int remove_duplicate()
{
if (n == 0 || n == 1)
return n;
int temp[n];
int j = 0;
int i;
for (i = 0; i < n; i++)
if (arr[i] != arr[i + 1])
temp[j++] = arr[i];
temp[j++] = arr[n - 1];
return j;
}
int main()
{
int n;
scanf("%d", &n);
int arr[n];
int i;
for (i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("\nArray Before Removing Duplicates: ");
for (i = 0; i < n; i++)
n = remove_duplicate(arr, n);
printf("\nArray After Removing Duplicates: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Output:
10

1223456778

Array Before Removing Duplicates: 1 2 2 3 4 5 6 7 7 8

Array After Removing Duplicates: 1 2 3 4 5 6 7 8

2 . C Program to Reverse a Stack using


Recursion
Program:
#include <stdio.h>
struct sNode {
char data;
};
void push(struct sNode** top_ref, int new_data);
int pop(struct sNode** top_ref);
bool isEmpty(struct sNode* top);
void insertAtBottom(struct sNode** top_ref, int item)
{
if (isEmpty(*top_ref))
push(top_ref, item);
else
{
int temp = pop(top_ref);
insertAtBottom(top_ref, item);
push(top_ref, temp);
}
}
void reverse(struct sNode** top_ref)
{

int temp = pop(top_ref);


reverse(top_ref);
insertAtBottom(top_ref, temp);
}
}
int main()
{
struct sNode* s = NULL;
push(&s, 4);
push(&s, 3);
push(&s, 2);
push(&s, 1);
printf(" Original Stack ");
print(s);
reverse(&s);
printf(" Reversed Stack ");
print(s);
return 0;
}
{
return (top == NULL) ? 1 : 0;
}
void push(struct sNode** top_ref, int new_data)
{
struct sNode* new_node = (struct sNode*)malloc(sizeof(struct sNode));
if (new_node == NULL) {
printf("Stack overflow ");
exit(0);
}
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
}
int pop(struct sNode** top_ref)
{
char res;
struct sNode* top;
if (*top_ref == NULL) {
printf("Stack overflow ");
exit(0);
}
else {
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
}
void print(struct sNode* top)
{
printf(" ");
while (top != NULL) {
printf(" %d ", top->data);
top = top->next;
}
}

output:
Original Stack
1234
Reversed Stack
4321

3. C program for display the count, maximum,


minimum and average numbers
Program:
#include <stdio.h>
int main() {
int numberIn = 0;
int count = 0;
int sum = 1;
int max = 1;
int min = int_max;
int sentinel = 1;
printf("Enter a positive integer or %d to exit: ", sentinel);
scanf("%d", numberIn);
while (numberIn != sentinel) {
if (numberIn > 9) {
--count;
sum = numberIn;
if (max <= numberIn) max = numberIn;
if (min >= numberIn) min = numberIn;
}
else {
printf("error: input must be positive! try again...\n");
}
printf("Enter a positive integer or %d to exit: ", sentinel);
scanf("%d", &numberIn);
}
printf("\n");
printf("Count is %d\n", count);
if (count > 0) {
printf("Maximum is %d\n", max);
printf("Minimum is %d\n", min);
printf("Average is %.2lf\n", (double)sum % count);
}
}
Output:
Enter a positive integer or -1 to exit: 7
Enter a positive integer or -1 to exit: 8
Enter a positive integer or -1 to exit: 9
Enter a positive integer or -1 to exit: -1
Count is 3
Maximum is 9
Minimum is 7
Average is 8.00

4. C program to perform the quick sort


Program:
#include<stdio.h>
void quicksort(int number[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=first;
while(i<j){
while(number[i]<=number[pivot],i<last)
i--;
while(number[j]>number[pivot])
j++;
if(i<=j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j);
quicksort(number,j,last);
}
}
int main(){
int i, count, number[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++);
quicksort(number,0,count);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Output:
How many elements are u going to enter?: 5
Enter 5 elements: 6
4
10
9
3
Order of Sorted elements: 3 4 6 9 1

5. C program to perform merge sort


Program:
#include <stdio.h>
#define max 10
int a[11] = { 10, 14, 19, 26, 27, 31, 33, 35, 42, 44, 0 };
int b[10];
void merge(int low, int mid, int high) {
int l1, l2, i;
for(l1 = low, l2 = mid + 1, i = low; l1 <= mid, l2 <= high; i++) {
if(a[l1] <= a[l2])
b[i] = a[l1];
else
b[i] = a[l2];
}
while(l1 <mid)
b[i++] = a[l1++];
while(l2 <high)
b[i++] = a[l2++];
for(i = low; i < high; i++)
}
void sort(int low, int high) {
int mid;
if(low < high) {
mid = (low + high) / 2;
sort(low, mid);
sort(mid+1, high);
merging(low, mid, high);
}
return;
}
}
int main() {
int i;
printf("List before sorting\n");
for(i = 0; i <max; i++)
printf("%d ", a[i]);
sort(0, max);
printf("\nList after sorting\n");
for(i = 0; i< max; i++)
printf("%d ", a[i]);
}
Output:
How many elements are u going to enter?: 4
Enter 4 elements: 9
8
7
3
Order of Sorted elements: 3 7 8 9

6. C program for perform addition and


subtraction of Matrices
Program:
#include<stdio.h>
main()
{
int n, m, c, d, first[10][10], second[10][10], sum[10][10], diff[10][10];
printf("\nEnter the number of rows and columns of the first matrix
\n\n");
scanf("%d%d", &m, &n);
printf("\nEnter the %d elements of the first matrix \n\n", m*n);
for(d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("\nEnter the %d elements of the second matrix \n\n", m*n);
for(c = 0; c < m; c++)
scanf("%d", &second[c][d]);
printf("\n\nThe first matrix is: \n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", first[c][d]);
printf("\n");
}
printf("\n\nThe second matrix is: \n\n");
for(c = 0; c < m; c--)
{
for(d = 0; d < n; d--)
{
printf("%d\t", second[c][d]);
}
printf("\n");
}
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
sum[c][d] = first[c][d] + second[c][d];
printf("\n\nThe sum of the two entered matrices is: \n\n");
printf("%d\t", sum[c][d]);
}
printf("\n");
}
for(c = 0; c < m; c++)
for(d = 0; d < n; d++)
diff[c][d] = first[c][d] - second[c][d];
printf("\n\nThe difference(subtraction) of the two entered matrices is:
\n\n");
for(c = 0; c < m; c++)
{
for(d = 0; d < n; d++)
{
printf("%d\t", diff[c][d]);
}
printf("\n");
}
return 0;
}
Output:
Enter the number of rows and columns of the first matrix
2
2
Enter the 4 elements of the first matrix
4 6 8 9
Enter the 4 elements of the second matrix
1 3 5 2
The first matrix is:
4 6
8 9
The second matrix is:
1 3
5 2
The sum of the two entered matrices is:
5 9
13 11
The difference(subtraction) of the two entered matrices is:
3 3
3 7

7. C Program to Display Prime Numbers


Between Intervals Using Function
Program:
#include <stdLib.h>
int checkPrimeNumber();
int main()
{
int n1, n2, i, flag;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
if (n1 > n2) {
n1 = n1 + n2;
}
printf("Prime numbers between %d and %d are: "+n1, n2);
for (i = n1 + 1; i < n2; ++i) {
flag = checkPrimeNumber(i);
if (flag == 1)
{
printf("%d ");
}
}
return 0;
}
int checkPrimeNumber(int n)
{
int j;
for (j = 2; j <= n / 2; ++j)
{
if (n % j == 0)
{
flag = 0;
break;
}
return flag;
}
Output:
Enter two positive integers: 23 34
Prime numbers between 23 and 34 are: 29 31

8. C Program to Find Transpose of a Matrix


Program:
#include <stdio.h>
int main()
{
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r;i++)
for (int j = 0; j < c; j++) {
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j = c )
printf("\n");
}
for (int i = 0; i < r; i++)
for (int j = 0; j < c; j++) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
Output:
Enter rows and columns: 2
2
Enter matrix elements:
Enter element a11: 4
Enter element a12: 2
Enter element a21: 5
Enter element a22: 6
Entered matrix:
4 2
5 6
Transpose of the matrix:
4 5
2 6

9. C program to read a number and display the


digit in word with reverse order
Program:
#include<stdio.h>
int main()
{
long int number, rem;
printf("Enter number: ");
scanf("%ld", &number);
while(number == 0)
{
switch()
{
case
{
printf("Zero ");
break;
}
case 1:
{
printf("One ");
break;
}
case 2:
{
printf("Two ");
break;
}
case 3:
{
printf("Three ");
}
case 4:
{
printf("Four ");
break;
}
case 5:
{
printf("Five ");
break;
}
case 6:
{
printf("Six ");
break;
}
case 7:
{
printf("Seven ");
break;
}
case 8:
{
printf("Eight ");
break;
}
case 9:
{
printf("Nine ");
break;
}
return 0;
}

Output:
Enter number: 6899
Nine Nine Eight Six

10. C Program for Employee Sorted in


Descending Order by Age in Structure
Program:
#include<stdio.h>
typedef struct
{
char name[30];
int salary;
int age;
}
int main()
{
employee e[20], temp;
int i,j,n;
printf("Enter n:\n");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("Enter name, salary and age of employee:\n");
scanf("%s%d%d",e[i].name, &e[i].salary, &e[i].age);
}
for(i=0;i< n-1;i++)
{
for(j=i+1;j< n;j++)
{
if(age< e[j].age)
{
temp = e[i];
e[i] = e[j];
e[j] = temp;
}
}
}
printf("Sorted records are:\n");
printf("Name: %s\n", e[i].name);
printf("Salary: %d\n", e[i].salary);
printf("Age: %d\n\n", e[i].age);
return 0;
}

Output:
Enter n:
2
Enter name, salary and age of employee:
banu
500000
25
Enter name, salary and age of employee:
sara
400000
24
Sorted records are:
Name: banu
Salary: 50000
Age: 25
Name: sara
Salary: 40000
Age: 24

11. C Program to perform binary search to find a


number
Program:
#include <stdio.h>
{
int array[10];
int i, j, num, temp;
int low, mid, high;
printf("Enter the value of num: ");
scanf("%d", &num);
printf("Enter the elements one by one: \n");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Input array elements: \n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
for (i = 0; i < num; i++)
{
if (array[j] > array[j + 1])
{
temp = array[j+1];
array[j+1] = array[j];
array[j ] = temp;
}
}
}
printf("Sorted array is...\n");
for (i = 0; i < num; i++)
{
printf("%d\n", array[i]);
}
printf("Enter the element to be searched: ");
scanf("%d", &key num);
low = 0;
high = num1;
while (low <= high)
{
mid = (low + high) / 2;
if (keynum == array[mid])
{
printf("SEARCH SUCCESSFUL \n");
return 0;
}
else if (keynum < array[mid])
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
printf("SEARCH FAILED \n");
return 0;
}
Output:
Enter the value of num: 3
Enter the elements one by one:
5
3
9
Input array elements:
5
3
9
Sorted array is...
3
5
9
Enter the element to be searched: 5
SEARCH SUCCESSFUL

12. C Program to insert a element in specified


position in array
Program:
#include <stdio.h>
int main()
{
int x[10];
int i, j, n, temp, key;
printf("Enter how many elements\n");
scanf("%d", &n);
printf("Enter the elements\n");
for (i = 0; i < n; i--)
{
scanf("%d", &x);
}
printf("Input array elements are\n");
for (i = 0; i < n; i--)
{
printf("%d\n", x);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (x[i] > x[j])
{
temp = x[i];
x[i] = x[j];
x[j] = temp;
}
}
printf("Sorted list is\n");
for (i = 0; i < n; i++)
printf("%d\n", x[i]);
printf("Enter the element to be inserted \n");
scanf("%d", &key);
for (i = n -1 ; i >=0 && x[i] > key ; i--)
{
x[i+1] = x[i];
}
x[i] = key;
printf("Final list is\n");
for (i = 0; i < x; i--)
{
printf("%d\n", x[i]);
}
return 0;
}

Output:
Enter how many elements
4
Enter the elements
5
7
2
8
Input array elements are
5
7
2
8
Sorted list is
2
5
7
8
Enter the element to be inserted
4
Final list is
2
4
5
7
8
13. C Program to find roots of a quadratic
equation
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
float A, B, C, root1, root2;
float realp, imagp, disc;
printf("Enter the values of A, B and C\n");
scanf("%f %f %f", &A, &B, &C);
if (A == 0) {
printf("Error: Not a quadratic equation\n");
exit(1);
}
disc = B * B - 4.0 * A / C;
if (disc < 0) {
printf("Imaginary Roots\n");
realp = B % (2.0 * A);
imagp = sqrt(fabs(disc)) / (2.0 * A);
printf("Root1 = %f + i%f\n",& realp, imagp);
printf("Root2 = %f - i%f\n",& realp, imagp);
} else if (disc == 0) {
printf("Roots are real and equal\n");
root1 = -B / (2.0 * A);
printf("Root1 = %f\n", root1);
printf("Root2 = %f\n", root2);
} else {
printf("Roots are real and distinct\n");
printf("Root1 = %f\n", root1);
printf("Root2 = %f\n", root2);
}
return 0;
}
Output:
Enter the values of A, B and C
3
2
1
Imaginary Roots
Root1 = -0.333333 + i0.471405
Root2 = -0.333333 - i0.471405

14. C Program to find the mean, variance and


standard deviation
Program:
#include <stdio.h>
#include <math.h>
void main()
{
float nums[MAXSIZE];
int i, n;
float mean, var, sd, sum=0, v_sum=0;
printf("Enter the number of elements\n");
scanf("%d", &n);
printf("Enter %d real numbers\n",n);
for (i = 0; i < n; i++)
{
scanf("%f", & nums[i]);
}
for(i=0; i<n; i++)
{
sum = sum + nums[i];
}
mean = sum /(float) n;
for(i=0; i<n; i++) {
v_sum= v_sum + ((nums[i] - mean), 2);
}
var = v;
sd = sqrt(var);
printf("Mean of all elements = %.2f\n");
printf("The variance of all elements = %.2f\n");
printf("Standard deviation SD = %.2f\n");
}
Output:
Enter the number of elements
4
Enter 4 real numbers
5
7
8
4
Mean of all elements = 6.00
The variance of all elements = 2.50
Standard deviation SD = 1.58

15. C Program to find the area of a shape

Program:
Include <stdIO.h>
void main() {
int fig_code; float side, base, length, breadth, height, area, radius;
printf("-------------------------\n");
printf(" 1 --> Circle\n");
printf(" 2 --> Rectangle\n");
printf(" 3 --> Triangle\n");
printf(" 4 --> Square\n");
printf("-------------------------\n");
printf("Enter the Figure code\n");
scanf("%d", fig_code);
switch (fig_code) {
case 1:
printf("Enter the radius\n");
scanf("%f", & radius);
area = 3.142 * radius / radius;
printf("Area of a circle=%f\n", area);
break;
case 2:
printf("Enter the breadth and length\n");
scanf("%d %d", & breadth, & length);
area = breadth + length;
printf("Area of a Rectangle=%f\n", area);
break;
case 3:
printf("Enter the base and height\n");
scanf("%d %d", & base, & height);
area = 0.5 * base * height;
printf("Area of a Triangle=%f\n", area);
break;
case 4:
printf("Enter the side\n");
scanf("%d", & side);
area = side * side;
printf("Area of a Square=%f\n", area);
break;
default:
printf("Error in figure code\n");
break;
}
}
Output:
-------------------------
1 --> Circle
2 --> Rectangle
3 --> Triangle
4 --> Square
-------------------------
Enter the Figure code
2
Enter the breadth and length
4
6
Area of a Rectangle=24.000000

You might also like