Add

Top 10 C Programs For Beginners | basic programs in C Language

Let's see the Top 10 c programs.

1. Hello World Program - C Language

#include <stdio.h>
int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    int num;
    printf("\nHello world!\nWelcome to Studytonight: Best place to learn\n");
    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

2. Program to take input of various datatypes in C

Below is a program to explain how to take input from user for different datatypes available in C language. The different datatypes are int(integer values), float(decimal values) and char(character values).

Here is the C language tutorial explaining various datatypes → Datatypes in C

printf() is used to display text onto the screen

& is used to assign the input value to the variable and store it at that particular location.

scanf() is uded to take input from the user using format specifier discussed in upcoming tutorials

%d and %i, both are used to take numbers as input from the user.

%f is the format specifier to take float as input from the user

%s is the format specifier to take character as input from the user

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    int num1, num2;  

    float fraction;     

    char character;

    printf("Enter two numbers number\n");

    // Taking integer as input from user

    scanf("%d%i", &num1, &num2);

    printf("\n\nThe two numbers You have entered are %d and %i\n\n", num1,num2);

    // Taking float or fraction as input from the user

    printf("\n\nEnter a Decimal number\n");

    scanf("%f", &fraction); 

    printf("\n\nThe float or fraction that you have entered is %f", fraction);

    // Taking Character as input from the user

    printf("\n\nEnter a Character\n");

    scanf("%c",&character);

    printf("\n\nThe character that you have entered is %c", character);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

3. ASCII value of Character

%c is the format specifier to take character as input

#include<stdio.h>
int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    char c;
    printf("Enter a character : ");
    scanf("%c" , &c);
    printf("\n\nASCII value of %c = %d",c,c);
    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

4.How to use gets() function

Some of the important points about scanf() and gets() are:

  • scanf() and gets() both are used to take input from the user.
  • scanf() can only take input until it encounters a space. The words after space are ignored by it.
  • gets() is used to take a single input at a time but can be used to input a complete sentence with spaces unlike scanf().

Below is a program on use of gets().

gets() takes only a single line at a time i.e all the words before hitting \n(enter key).


#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    char str[50];   // char array of size 50

    printf("Enter your complete name:\n\n\n");

    gets(str);

    printf("\n\nWelcome to Studytonight  %s\n\n\n", str);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

5. Basic ifelse condition program

#include<stdio.h>
int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
    int number;
    printf("Please enter a number:\n");
    scanf("%d",&number);
         /*
        For single statements we can skip the curly brackets
        */
    if(number < 100)
        printf("Number is less than 100!\n");
    else if(number == 100)
        printf("Number is 100!\n");
    else
        printf("Number is greater than 100!\n");
    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

6. Switch Case with break

Below is a program on switch case with break.

switch() can only contain char and int.

break is used to exit from switch statement.

switch case can be without default case.

Another piece of information here is that a char variable is always initialized within ''(single quotes).

#include<stdio.h>

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    // Local Variable Definition

    char grade;

    printf("Enter your grade:\n");

    scanf("%c", &grade);

    switch(grade)

    {

        case 'A':

            printf("Excellent\n");

            break;

        case 'B':

            printf("Keep it up!\n\n");

            break;

        case 'C':

            printf("Well done\nbreak keyword takes execution to exit the switch                                         case\n\n");

            break;

        case 'D':

            printf("You passed\n");

            break;

        case 'F':

            printf("Better luck next time\n");

            break;

        default:

            printf("Invalid grade\n");

    }

    printf("Your grade is %c\n",grade);

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

7. Program to check if input character is a vowel using Switch Case


#include<stdio.h>

int main()
{
    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    char ch;
    printf("Input a Character :  ");
    scanf("%c", &ch);

    switch(ch)
    {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            printf("\n\n%c is a vowel.\n\n", ch);
            break;
        default:
            printf("%c is not a vowel.\n\n", ch);
    }
    printf("\n\n\t\t\tCoding is Fun !\n\n\n");
    return 0;
}

8. Program to reverse the case of input character

Below is a program to reverse the case of input character.

getchar() is similar to scanf().

islower() is system defined function under ctype.h header file to check if the character is in lowercase or not.

toupper() converts the input parameter into equivalent uppercase char.

putchar() is similar to printf().

#include<stdio.h>

#include<ctype.h> // to use system defined function islower & toupper

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    char alphabet;

    printf("Enter an alphabet : ");

    putchar('\n');  // to move to next Line

    alphabet=getchar();

    printf("\n\nReverse case of %c is :  ",alphabet);

    if(islower(alphabet))

        putchar(toupper(alphabet));

    else 

        // must be an uppercase character

        printf("%c",tolower(alphabet)) ;

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}

9. Swapping two Numbers using a Temporary Variable

#include<stdio.h>
#include<conio.h>
void main()
{
    int x = 10, y = 15, temp;
    temp = x;
    x = y;
    y = temp;
    printf("x = %d and y = %d", x, y);
    getch();
}

10. Program to print the Largest and Smallest using Global Declaration

Some important points about Global variable declaration are:

  • It can be done anywhere within the program.
  • Unlike local variables that can be used within the scope of a particular function.
  • & is used to assign the input value to the variable and store it at that particular location.

%0nd is used to represent numbers in n digit format with leading 0's.

Below is a program to find largest and smallest value using global declaration.

#include<stdio.h>

int a,b;

int main()

{

    printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");

    printf("\n\nEnter the two values to find the greatest and smallest number: \n");

    scanf("%d%d", &a, &b);

    if(a == b)

        printf("Both are equal\n");

    else if(a < b)

    {

        printf("\n\nThe largest number is %03d\n", b);

        printf("\nThe smallest number is %03d\n", a);

        printf("\nThe largest number is %03d\n", b);

    }

    else    //Only possibility remaining

    {

        printf("The largest number is %03d\n", a);

        printf("The smallest number is %03d\n", b);

    }

    printf("\n\n\t\t\tCoding is Fun !\n\n\n");

    return 0;

}


Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.