Saturday, March 18, 2023

C Programming. Program to merge two arrays and get an ordered array.

 Program to merge two arrays and get an ordered array.

// Program to merge two array and get an ordered array.

#include<stdio.h>

int main() {
    int n1, n2, i, j, t;
    printf("Enter the size of 1st array : ");
    scanf("%d", &n1);
    printf("Enter the size of 2nd array : ");
    scanf("%d", &n2);
    int a[n1], b[n2], c[n1+n2];
    printf("Enter the elements of 1st array in ordered manner : \n");
    for (i=0; i<n1; i++){
        scanf("%d",&a[i]);
    }
    printf("Enter the elements of 2nd array in ordered manner : \n");
    for (i=0; i<n2; i++){
        scanf("%d",&b[i]);
    }
    i=0;
    while (i<n1){
        c[i]=a[i];
        i++;
    }
    j=0;
    while (j<n2){
        c[i]=b[j];
        i++;
        j++;
    }
    for (i=0; i<n1+n2; i++){
        for (j=i; j<n1+n2; j++){
            if (c[i]>c[j]){
                t=c[i];
                c[i]=c[j];
                c[j]=t;
            }
        }
    }
    printf("After merging the array is :\n");
    for (i=0; i<n1+n2; i++){
        printf("%d ",c[i]);
    }
    return 0;
}

Output :

Enter the size of 1st array : 4 Enter the size of 2nd array : 5 Enter the elements of 1st array in ordered manner : 1 3 5 7 Enter the elements of 2nd array in ordered manner : 2 4 6 8 9 After merging the array is : 1 2 3 4 5 6 7 8 9

C Programming. Program to swap two numbers using macros.

 Program to swap two numbers using macros.

// Program to swap two numbers using macros.

#include<stdio.h>

#define swap(a, b) int t=a; a=b; b=t;

int main() {
    int a, b;
    printf("Enter two numbers : ");
    scanf("%d%d", &a, &b);
    printf("Before swapping a = %d, b = %d\n", a, b);
    swap(a, b);
    printf("After swapping a = %d, b = %d", a, b);
    return 0;
}

Output :

Enter two numbers : 5 7 Before swapping a = 5, b = 7 After swapping a = 7, b = 5

Friday, March 17, 2023

C Programming. Program to copy the content of one file to another.

 Program to copy the content of one file to another.

//  Program to copy the content of one file to another.

#include<stdio.h>

int main() {
    FILE *f1, *f2;
    char c;
    f1 = fopen("File1.txt", "r");
    f2 = fopen ("File2.txt", "w");
    while ((c=getc(f1))!=EOF){
        putc(c, f2);
    }
    fclose(f1);
    fclose(f2);
    return 0;
}

Output :

File1:
Hello guys, This Program is to copy the content of file 1 to file 2.
File2:
Hello guys, This Program is to copy the content of file 1 to file 2.

C Programming. Allocate memory dynamically using malloc() / calloc() functions.

Program to find sum and average of n elements entered by the user. Allocate memory dynamically using malloc() / calloc() functions.

// Program to find sum and average of n elements entered by the user.
// Allocate memory dynamically using malloc()/calloc() functions.

#include<stdio.h>
#include<stdlib.h>

int main() {
    int n, i, sum=0;
    float avg;
    printf("Enter the number of elements : ");
    scanf("%d",&n);
    int *p = (int *)calloc(n, sizeof(int));

    for (i=0; i<n; i++){
        printf("\nEnter the %dth element : ", i+1);
        scanf("%d", &p[i]);
    }
    for (i=0; i<n; i++){
        sum += p[i];
    }
    avg = (float)sum / n;
    printf("\nThe sum of %d elements is : %d\nAverage is : %0.2f ", n, sum, avg);

    return 0;
}

Output :

Enter the number of elements : 5 Enter the 1th element : 4 Enter the 2th element : 3 Enter the 3th element : 5 Enter the 4th element : 7 Enter the 5th element : 3 The sum of 5 elements is : 22 Average is : 4.40

C Programming. Program to find area and circumference using function.

 Program to find area and circumference using function.

// Program to find area and circumference using function
#include <stdio.h>

float Area(int);
float Circ(int);

int main()
{
    int r;
    float area, circ;
    printf("Enter the radius of a circle : ");
    scanf("%d", &r);
    area = Area(r);
    circ = Circ(r);
    printf("The area of the circle is : %.2f", area);
    printf("\nThe circumference of the circle is : %.2f", circ);
    return 0;
}

float Area(int r)
{
    float area = 3.141 * r * r;
    return area;
}
float Circ(int r)
{
    float circ = 2 * 3.141 * r;
    return circ;
}

Output :

Enter the radius of a circle : 5 The area of the circle is : 78.53 The circumference of the circle is : 31.41

C Programming. Program in which function is passed address of two variables and alter its contents.

Program in which function is passed address of two variables and alter its contents.


// Program in which function is passed address of two variables and alter its contents.

#include<stdio.h>

void alter(int* , int*);

int main() {
    int a, b;
    printf("Enter two numbers : ");
    scanf("%d%d",&a, &b);
    printf("Before swapping a = %d, b = %d", a, b);
    alter(&a, &b);
    printf("\nAfter swapping a = %d, b = %d", a, b);
    return 0;
}
void alter(int*a , int*b){
    int t;
    t=*a;
    *a=*b;
    *b=t;
}

Output :

Enter two numbers : 5 7 Before swapping a = 5, b = 7 After swapping a = 7, b = 5

C Programming. Program to swap two numbers using pointers.

 Program to swap two numbers using pointers.

// Program to swap two numbers using pointers.

#include<stdio.h>

int main() {
    int a, b, *p1, *p2, t;
    p1 = &a;
    p2 = &b;
    printf("Enter two numbers : ");
    scanf("%d%d",&a, &b);
    printf("Before swapping a = %d, b = %d", a, b);
    t=*p1;
    *p1=*p2;
    *p2=t;
    printf("\nAfter swapping a = %d, b = %d", a, b);
    return 0;
}

Output :

Enter two numbers : 5 7 Before swapping a = 5, b = 7 After swapping a = 7, b = 5

C Programming. Printing the occurrence of each alphabet in the text entered as command line arguments.

Printing the occurrence of each alphabet in the text entered as command line arguments.

// Program that prints a table indicating
// occurrence of each alphabet in the text entered as command line arguments.

#include<stdio.h>
#include<string.h>

int main(int argc, char **argv) {
    char s[100];
    strcpy(s, argv[1]);
    int n = strlen(s);
    int count;
    printf("Occurrence of each alphabet in given string \"%s\" is :\n");
    for (int i=0; i<n; i++){
        count = 1;
        if (s[i] >= 'A' && s[i]<='Z' || s[i] >= 'a' && s[i]<='z') {
            if (s[i]!='\0'){
                for (int j=i+1; j<n; j++){
                    if (s[i]==s[j]){
                        count++;
                        s[j]='\0';
                    }
                }
                printf("'%c' = %d\n", s[i], count);
            }
        }
    }
    return 0;
}

Output :

filename.exe "Hello World" Occurrence of each alphabet in given string "Hello World" is : 'H' = 1 'e' = 1 'l' = 3 'o' = 2 'W' = 1 'r' = 1 'd' = 1

C Programming. Playing with array.

 Playing with array.

// Program to perform following actions on an array : -
/*
    (a) Print the even valued elements.
    (b) Print the odd valued elements.
    (c) Calculate and print the sum and average of the elements of array.
    (d) Print the maximum and minimum element of array.
    (e) Remove the duplicate from the array.
    (a) Print the array in reverse order.
*/

#include <stdio.h>

int main()
{
    int n, i, j;
    int sum = 0;
    float avg;
    int max, min;
    printf("Enter the size of the array : ");
    scanf("%d", &n);
    int arr[n], arr1[n], revarr[n];
    printf("Enter the elements of the array : \n");
    for (i = 0; i < n; i++)
    {
        printf("arr[%d] = ", i);
        scanf("%d", &arr[i]);
    }

    // For even numbers...
    printf("\n(a) The even numbers are : ");
    for (i = 0; i < n; i++)
    {
        if (arr[i] != 0){
            if (arr[i] % 2 == 0)
            {
                printf("%d ", arr[i]);
            }
        }
    }

    // For odd numbers...
    printf("\n(b) The odd numbers are : ");
    for (i = 0; i < n; i++)
    {
        if (arr[i] % 2 != 0)
        {
            printf("%d ", arr[i]);
        }
    }

    // For sum and average...
    for (i = 0; i < n; i++)
    {
        sum += arr[i];
    }
    avg = (float)sum / n;
    printf("\n(c) The sum of all elements is : %d\n    Average is : %0.2f ", sum, avg);

    // For maximum and minimum...
    max = min = arr[0];
    for (i = 0; i < n; i++)
    {
        if (arr[i] > max)
        {
            max = arr[i];
        }
        if (arr[i] < min)
        {
            min = arr[i];
        }
    }
    printf("\n(d) The maximum number is : %d\n    The minimum number is : %d",max, min);

    // For removing duplicate elements...
    for (i=0; i<n; i++){
        arr1[i]=arr[i];
    }

    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (arr1[i] == arr1[j])
            {
                arr1[j] = -1;
            }
        }
    }
   
    printf("\n(e) The array after removing duplicate element is : ");
    for (i = 0; i < n; i++)
    {
        if (arr1[i] != -1)
        {
            printf("%d ", arr1[i]);
        }
    }

    // For reverse order...
    for (i=0; i<n; i++){
        revarr[i]=arr[n-i-1];
    }
    printf("\n(f) The reverse of the array is : ");
    for (i=0; i<n; i++){
        printf("%d ", revarr[i]);
    }
    return 0;
}

Output :

Enter the elements of the array : arr[0] = 2 arr[1] = 4 arr[2] = 7 arr[3] = 2 arr[4] = 9 (a) The even numbers are : 2 4 2 (b) The odd numbers are : 7 9 (c) The sum of all elements is : 24 Average is : 4.80 (d) The maximum number is : 9 The minimum number is : 2 (e) The array after removing duplicate element is : 2 4 7 9 (f) The reverse of the array is : 9 2 7 4 2

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...