Tuesday, 21 January 2025

Basic-2 C programming

 16. Program to Check Prime Number

#include <stdio.h>

#include<conio.h>

int main() {

    int num = 7, i, flag = 0;

    for (i = 2; i <= num / 2; i++) {

        if (num % i == 0) {

            flag = 1;

            break;

        }

    }

    if (flag == 0)

        printf("Prime number");

    else

        printf("Not a prime number");


    return 0;

    getch();

}


Output

Prime number


17. Program to Print Prime Numbers from 1 to 10

 #include <stdio.h>

#include<conio.h>

int main() {

    int i, j, flag;

    for (i = 2; i <= 10; i++) {

        flag = 0;

        for (j = 2; j <= i / 2; j++) {

            if (i % j == 0) {

                flag = 1;

                break;

            }

        }

        if (flag == 0)

            printf("%d ", i);

    }

    return 0;

     getch();

}


Output

2 3 5 7


18. Program to Find Fibonacci Series

 #include <stdio.h>

#include<conio.h>

int main() {

    int n = 5, a = 0, b = 1, c, i;

    printf("%d %d ", a, b);

    for (i = 2; i < n; i++) {

        c = a + b;

        printf("%d ", c);

        a = b;

        b = c;

    }

    return 0;

    getch();

}


Output

0 1 1 2 3


19. Program Using Function (Addition)

#include <stdio.h>

#include<conio.h>

int add(int a, int b) {

    return a + b;

}

int main() {

    int result;

    result = add(10, 20);

    printf("Sum = %d", result);

    return 0;

    getch();

}


Output

Sum = 30


20. Program to Find String Length (Without strlen)

 #include <stdio.h>

#include<conio.h>

int main() {

    char str[] = "Hello";

    int i = 0;

    while (str[i] != '\0') {

        i++;

    }

    printf("Length = %d", i);

    return 0;

    getch();

}


Output

Length = 5


21. Program to Reverse a String

 #include <stdio.h>

#include<conio.h>

int main() {

    char str[] = "Cprogram";

    int i, len = 0;


    while (str[len] != '\0') {

        len++;

    }

    for (i = len - 1; i >= 0; i--) {

        printf("%c", str[i]);

    }

    return 0;

    getch();

}


Output

margorpC


22. Program to Copy One String to Another

 #include <stdio.h>

#include<conio.h>

int main() {

    char str1[] = "Hello";

    char str2[10];

    int i = 0;

    while (str1[i] != '\0') {

        str2[i] = str1[i];

        i++;

    }

    str2[i] = '\0';

    printf("Copied string: %s", str2);

    return 0;

    getch();

}


Output

Copied string: Hello


23. Program Using Pointer

 #include <stdio.h>

#include<conio.h>

int main() {

    int a = 10;

    int *p;

    p = &a;

    printf("Value of a = %d\n", a);

    printf("Value using pointer = %d", *p);

    return 0;

    getch();

}


Output

Value of a = 10

Value using pointer = 10


24. Program to Swap Two Numbers (Using Function)

 #include <stdio.h>

#include<conio.h>

void swap(int *x, int *y) {

    int temp;

    temp = *x;

    *x = *y;

    *y = temp;

}

int main() {

    int a = 5, b = 10;

    swap(&a, &b);

    printf("a = %d, b = %d", a, b);

    return 0;

    getch();

}


Output

a = 10, b = 5