Monday, 20 January 2025

Basic-1 C programming

 1. Hello World Program

#include <stdio.h>

#include<conio.h>

int main() {

    printf("Hello, World!");

    return 0;

    getch();

}

Output

Hello, World!


2. Program to Add Two Numbers

#include <stdio.h>

#include<conio.h>

int main() {

    int a, b, sum;

    a = 10;

    b = 20;

    sum = a + b;

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

    return 0;

    getch();

}

Output

Sum = 30


3. Program to Find Even or Odd Number

#include <stdio.h>

#include<conio.h>

int main() {

    int num = 7;

    if (num % 2 == 0)

        printf("Even number");

    else

        printf("Odd number");


    return 0;

     getch();

}

Output

Odd number


4. Program to Find Largest of Two Numbers

#include <stdio.h>

#include<conio.h>

int main() {

    int a = 15, b = 25;


    if (a > b)

        printf("A is greater");

    else

        printf("B is greater");

    return 0;

    getch();

}

Output

B is greater


5. Program to Print Numbers from 1 to 5 (Using Loop)

#include <stdio.h>

#include<conio.h>

int main() {

    int i;

    for (i = 1; i <= 5; i++) {

        printf("%d ", i);

    }

    return 0;

}

Output

1 2 3 4 5


6. Program to Find Factorial of a Number

#include <stdio.h>

#include<conio.h>

int main() {

    int i, n = 5;

    int fact = 1;


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

        fact = fact * i;

    }

    printf("Factorial = %d", fact);

    return 0;

    getch();

}


Output

Factorial = 120


7. Program to Check Vowel or Consonant

#include <stdio.h>

#include<conio.h>

int main() {

    char ch = 'a';

    if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')

        printf("Vowel");

    else

        printf("Consonant");

    return 0;

    gecth();

}


Output

Vowel

8. Program to Take Input from User

#include <stdio.h>

#include<conio.h>


int main() {

    int num;

    printf("Enter a number: ");

    scanf("%d", &num);


    printf("You entered: %d", num);

    return 0;

    gecth();

}

Output

Enter a number: 5

You entered: 5


9. Program to Find Largest of Three Numbers

 #include <stdio.h>

#include<conio.h>

int main() {

    int a, b, c;

    a = 10;

    b = 25;

    c = 15;

    if (a > b && a > c)

        printf("A is largest");

    else if (b > c)

        printf("B is largest");

    else

        printf("C is largest");


    return 0;

    gecth();

}


Output

B is largest


10. Program to Print Multiplication Table

 #include <stdio.h>

#include<conio.h>

int main() {

    int i, n = 5;


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

        printf("%d x %d = %d\n", n, i, n * i);

    }


    return 0;

    getch();

}

Output

5 x 1 = 5

5 x 2 = 10

...

5 x 10 = 50

11. Program to Reverse a Number

 #include <stdio.h>

#include<conio.h>

int main() {

    int num = 123, rev = 0;


    while (num != 0) {

        rev = rev * 10 + num % 10;

        num = num / 10;

    }

    printf("Reversed number = %d", rev);

    return 0;

    gecth();

}


Output

Reversed number = 321


12. Program to Check Palindrome Number

 #include <stdio.h>

#include<conio.h>

int main() {

    int num = 121, temp, rev = 0;

    temp = num;

    while (num != 0) {

        rev = rev * 10 + num % 10;

        num = num / 10;

    }

    if (temp == rev)

        printf("Palindrome number");

    else

        printf("Not a palindrome");


    return 0;

    getch();

}


Output

Palindrome number


13. Program to Find Sum of Digits

 #include <stdio.h>

#include<conio.h>

int main() {

    int num = 123, sum = 0;


    while (num != 0) {

        sum = sum + num % 10;

        num = num / 10;

    }

    printf("Sum of digits = %d", sum);

    return 0;

    getch();

}


Output

Sum of digits = 6


14. Program Using Array (Print Elements)

 #include <stdio.h>

#include<conio.h>

int main() {

    int i;

    int arr[5] = {10, 20, 30, 40, 50};

    for (i = 0; i < 5; i++) {

        printf("%d ", arr[i]);

    }

    return 0;

    getch();

}


Output

10 20 30 40 50


15. Program to Find Sum of Array Elements

 #include <stdio.h>

#include<conio.h>

int main() {

    int i, sum = 0;

    int arr[5] = {1, 2, 3, 4, 5};

    for (i = 0; i < 5; i++) {

        sum = sum + arr[i];

    }

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

    return 0;

getch():

}


Output

Sum = 15