Friday, March 17, 2023

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

C Programming. Program to print star pattern.

 Program to print star pattern.

// Program to print star triangle.(take number of lines from user).
/*        *
         ***  
        *****
       *******
*/

#include<stdio.h>

int main() {
    int n, i, j;
    printf("Enter number of lines : ");
    scanf("%d", &n);
    for (i=1; i<=n; i++) {
        for (j=1; j<=n-i; j++){
            printf(" ");
        }
        for (j=1; j<=2*i-1; j++){
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output :

Enter number of lines : 7 * *** ***** ******* ********* *********** *************

C Programming. Factors of a given number.

 Factors of a given number.

// Program to compute the factors of a given number.

#include<stdio.h>

int main() {
    int n, i;
    printf("Enter a number : ");
    scanf("%d", &n);
    printf("The factors of %d is : \n", n);
    for (i=1; i<=n; i++){
        if (n%i == 0){
            printf("%d ", i);
        }
    }
    return 0;
}


Output :
Enter a number : 12 The factors of 12 is : 1 2 3 4 6 12

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...