Latest Post:
Loading...

Introduction to C (Beginners)

C Language Complete Solution C Programming Language Complete Solution's

C Programming Language Solution's Related to Grade 10 and 12


Introduction of c language


C is the programming language developed by Demis M. Ritchie at AT and I's Bell laboratories in 1970 S

Features of C language:

  • 1) Portability
  • 2) Redability
  • 3) Maintainability
  • 4) Extendibility
  • 5) faster and efficent
  • 6) Flexible

Advantages of C

  • 1) It is easy to learn and use
  • 2) Adding a new feature is easier and faster.
  • 3) It is faster and efficent to use.
  • 4) It is a flexible language

Disadvantages

  1. a)It Takes more time to design and implement the slw
  2. b)It doesnot contain run time checking
  3. c)AS the program extends difficult to fix the errors .
  4. d)It doesnot support opp (object oriented programming)

C is also called as :

  1. a)High level language
  2. b)Mother language
  3. c)Midddle level language
  4. d)Structure programming language
  5. e)Flexible language
  1. C Language basic Structure

    #include<stdio.h>
        int main(void) {
        printf("Hello World");
        return 0;
       }
    /*
    Output:
    Hello World 
    */
    
    
  2. Explanation of upper code:

    a) The first line #include is a preprocessor directive that includes the header file stdio.h, which provides the definition of the printf function used in the program.

    b) The main function is the entry point of the program. It is defined with the int return type, which indicates that the function returns an integer value. The void in the parentheses indicates that the function takes no arguments.

    Within the body of the main function, we use the printf function to print the string "Hello, World!" to the console.

    Finally, the return 0; statement at the end of the main function returns the value 0 to indicate that the program has executed successfully.



    Header file IN C

    In C programming, header files are used to store declarations and prototypes of functions, variables, and other constructs that can be used across multiple source files. Here are some commonly used header files in C with a brief description:

    1. stdio.h (Standard Input/Output): This header file provides definitions for the standard input and output functions, such as printf, scanf, getchar, and putchar.
    2. string.h (String handling): This header file provides definitions for various string-related functions, such as strcpy, strcat, strlen, strcmp, etc.
    3. math.h (Mathematical functions): This header file provides definitions for various mathematical functions, such as sqrt, pow, exp, log, etc.
    4. stdlib.h (Standard Library): This header file provides definitions for various standard library functions, such as malloc, free, atoi, itoa, etc.
    5. time.h (Time and date functions): This header file provides definitions for various time and date related functions, such as time, gmtime, mktime, etc.
    6. ctype.h (Character handling): This header file provides definitions for various character-related functions, such as islower, isupper, tolower, toupper, etc.
  3. The character set used in C programming


    The character set used in C programming is the ASCII character set, which consists of 128 characters and includes the 26 uppercase and lowercase letters of the English alphabet, the digits 0-9, various punctuation marks, and a number of special characters

    Example of Character

      1):Uppercase characters


      'A ','B 'to 'Z'

      2) Lowercase characters:


      'a''b''c' TO 'z'

      3) Punctuation marks:

    1. '.' (period)
    2. ',' (comma)
    3. ':' (colon)
    4. ';' (semicolon)
    5. '!' (exclamation mark)
    6. '?' (question mark)

    7. 4) Special characters:

    8. '$' (dollar sign)
    9. '@' (at sign)
    10. '&' (ampersand)
    11. '*' (asterisk)
    12. '+' (plus sign)


  4. Comment in C


    Comments are non-executable statements that are ignored by the compiler. They are used to add annotations, explanations, or notes to the source code to make it easier to understand for humans. There are two types of comments in C:

    1) Single-line comments:


    Single-line comments start with // and continue until the end of the line. For example:
                     
                          // This is a single-line comment
    
    

    2) Multi-line comments:


    Multi-line comments start with /* and end with */. Everything in between is considered a comment and can span multiple lines. For example:
                     
                        /* This is a
                        multi-line comment */
                        
    
    
  • Introduction to C



    C tokens

    In C programming language, tokens are the basic building blocks of a program. Tokens are defined as the smallest individual units in a program. There are several types of tokens in C:

    Keywords: Keywords are reserved words in C that have a special meaning and cannot be used as identifiers. Examples of keywords in C are int, char, if, while, etc.

    Identifiers: Identifiers are names given to variables, functions, arrays, etc. in a C program. They can be any combination of letters, digits, and underscores, as long as they do not start with a digit.

    Constants: Constants are fixed values that do not change during the execution of a program. In C, there are several types of constants, such as integer constants, floating-point constants, character constants, and string.

    Strings: Strings are sequences of characters surrounded by double quotes. They are used to represent text in C programs.

    Operators: Operators are symbols that perform operations on one or more operands. In C, there are several types of operators, such as arithmetic operators, comparison operators, logical operators, etc.
  • Data Type in C

    In C programming language, data types are used to define the type of values that a variable can hold.There are two main types of data types:

    Primitive data types: Primitive data types are the basic data types that are built into the language. Examples of primitive data types in C are int, char, float, double, void, etc. These data types can store values of different sizes and ranges, and they cannot be further divided into simpler data types.

    Example of a program in C that uses several primitive data types:

    #include<stdio.h>
         int main()
          {
          int age = 30;
          char grade = 'A';
          float price = 12.99;
          double weight = 68.5;           
          printf("Age: %d\n", age);
          printf("Grade: %c\n", grade);
          printf("Price: %f\n", price);
          printf("Weight: %lf\n", weight);
                            
          return 0;
                            }
        /*
     Age: 30
     Grade: A
     Price: 12.990000
     Weight: 68.500000
        */
    Derived data types: Derived data types are data types that are derived from primitive data types or other derived data types. Examples of derived data types in C are arrays, structures, unions, pointers, and function types. These data types are used to store collections of data or to represent complex data structures.

    Example of a program in C that uses an array of int values as a derived data type:

    #include<stdio.h>
            int main()
       { 
        int numbers[5] = {1, 2, 3, 4, 5};
        for (int i = 0; i < 5; i++)
        {
            printf("Number %d: %d\n", i + 1, numbers[i]);
        }
    return 0;
    }
       }
          /*
         Number 1: 1
         Number 2: 2
         Number 3: 3
         Number 4: 4
         Number 5: 5
         */
  • Constants


    A constant is a value in a C program that cannot be changed once it has been set. Constants are used to represent values that remain the same throughout the execution of a program.
    Constants are declared using the const keyword, followed by the data type of the constant and its name.

    For example :

  • #include<stdio.h>
     int main()
     {
       const double PI = 3.14159265;
     printf("The value of PI is: %lf\n", PI);
            return 0;
                }
    /*Output:
    The value of PI is: 3.141593
    */
    

    Types of Constant

    C has four basic types of constants . They are :

    1)Integer constant: It is an integer-valued number. It can be positive or negative. It can also be decimal, octal, or hexadecimal integer constant.

    2) Floating-point constant: It is a fractional number. It can be written either in decimal form or an exponent form or both. It can also be decimal, octal, or hexadecimal floating-point constant.

    3) Character constant: It is a single character, enclosed in apostrophes (single quotation marks).

    4) String constant: It consists of any number of consecutive characters enclosed in double quotation marks.

  • Variable

    Variable is the name of a memory location which stores some data.Variables are the most fundamental part of any programming language .

  • For example, the following code declares a variable age with a value of 30, and then changes its value to 35:
    #include<stdio.h>
    int main()
    {
     int age = 30;
     printf("Age: %d\n", age);
     age = 35;
    printf("New Age: %d\n", age);
    return 0;
     }                 
    /*
    Output:
    Age: 30
    New Age: 35
    */
    

    In C programming, there are a few rules that must be followed when naming variables:

    1) Variable names must begin with a letter or an underscore (_).
    2)Variable names can only contain letters, digits, and underscores.
    3)Variable names cannot contain spaces.
    4)Variable names cannot be a keyword or a reserved word in the C language.
    5)Variable names should be descriptive and meaningful.
    6)Variable names are case sensitive in C, so age and Age are considered to be two different variables.

  • Escape Sequences

    Escape sequences are a set of special characters in C programming language that represent non-printable characters such as a newline, tab, backspace, and others. The escape sequences are represented by a backslash (\) followed by a character.
    Some of the most commonly used escape sequences in C are:

    Escape Sequence Description
    \n Represents a newline character
    \t Represents a tab character
    \b Represents a backspace character
    \\ Represents a backslash character
    \' Represents a single quote character
    \" Represents a double quote character
    \r Represents a carriage return character
  • Operators

    C programming language provides several types of operators to perform operations on variables and values.
    Some of the most commonly used operators in C are:

  • Arithmetic operators
  • Logical operators
  • Equality operators
  • Relational operators
  • Assignment operators
  • Special operators
  • Operator Type Operators Example
    Arithmetic Operators + (Addition), - (Subtraction), * (Multiplication), / (Division), % (Modulus), ** (Exponentiation) 10 + 5 = 15, 20 - 10 = 10, 5 * 5 = 25, 20 / 5 = 4, 20 % 3 = 2, 2 ** 3 = 8
    Logical Operators && (And), || (Or), ! (Not) true && false = false, true || false = true, !true = false
    Equality Operators == (Equal to), != (Not equal to), === (Strict equal to), !== (Strict not equal to) 10 == "10" = true, 10 != "10" = false, 10 === "10" = false, 10 !== "10" = true
    Relational Operators < (Less than), > (Greater than), <= (Less than or equal to), >= (Greater than or equal to) 10 < 20 = true, 10 > 20 = false, 10 <= 20 = true, 10 >= 20 = false
    Assignment Operators = (Assign), += (Add and assign), -= (Subtract and assign), *= (Multiply and assign), /= (Divide and assign), %= (Modulus and assign), **= (Exponentiation and assign) x = 10, x += 5 (x = x + 5), x -= 5 (x = x - 5), x *= 5 (x = x * 5), x /= 5 (x = x / 5),
  • Area Of circle Using C Programming
    #include<stdio.h>
    int main()
    {
        float area, r, PI=3.14;
        printf("Enter Radius Of Circle: \n");
        scanf("%f", &r);
        area=PI*r*r;
        printf("The Area Of Circle Is: %f ", area);    
        return 0;
    }
    /*
    Output:
    Enter Radius Of Circle: 
    7
    The Area Of Circle Is: 153.860016 
    */
    
  • Aera Of Rectangle Using C Programming
    #include<stdio.h>
    int main()
    {
        int l, b, area;
        printf("Enter The Length Of Rectangle:\n");
        scanf("%d", &l);
        printf("Enter The Breadth Of Rectangle: \n");
        scanf("%d", &b);
        area=l*b;
        printf("The area of Rectangle is: %d", area);    
        return 0;
    }
    /*
    Output:
    Enter The Length Of Rectangle:
    4
    Enter The Breadth Of Rectangle: 
    5
    The area of Rectangle is: 20
    */
    
  • Swapping Value Using C Programming
    #include<stdio.h>
    int main()
    {
    	int x = 10, y = 15, temp;
    	printf("Value Before Swapping x=%d and y=%d\n", x, y);
    	temp = x;
    	x = y;
    	y = temp;
    	printf("Value After Swapping x = %d and y = %d", x, y);
    	return 0;    
    }
    /*
    Output:
    Value Before Swapping x=10 and y=15
    Value After Swapping x = 15 and y = 10
    */
  • Program To Find Input Number Is Odd Or even
    #include<stdio.h>
    int main()
    {
        int a;
        printf("Enter A Number:\n");
        scanf("%d", &a);
        if(a%2==0){
            printf("The Input Number Is Even.");
        }
        else{
            printf("The Input Number Is Odd.");
        }
        return 0;
    }
    /*
    Output:
    Enter A Number:
    5
    The Input Number Is Odd.
    */
    
  • Program To Find a Input Number Is Divisible By 5
    #include<stdio.h>
    int main()
    {
        int a;
        printf("Enter A Number:\n");
        scanf("%d", &a);
        if(a%5==0){
            printf("The Number %d Is Divisible By 5", a);
        }
        else{
            printf("The Number %d Is Not Divisible By 5", a);
        }
        return 0;
    }
    /*
    Output:
    Enter A Number:
    25
    The Number 25 Is Divisible By 5
    */
    
  • Find The Greatest Number Among 3 Numbers
    
    #include<stdio.h>
    int main()
    {
    	int a, b, c;
    	printf("Enter 3 Number's:\n");
    	scanf("%d %d %d", &a, &b, &c);
    	if(a>b && a>c){
    		printf("The %d is Greatest Number.", a);
    	}    
    	else if(b>a && b>c){
    		printf("The %d is Greatest Number.", b);
    	}
    	else{
    		printf("The %d is Greatest Number.", c);
    	}
    	return 0;
    }
    				
    /*
    Output:
    Enter 3 Number's:
    7
    1
    2
    The 7 is Greatest Number.
    */
  • Find Middle Number Among 3 Number's
    #include <stdio.h>
    
    int main()
    {
        int a,b,c;
        printf("Enter three numbers\n");
        scanf("%d %d %d",&a,&b,&c);
    
        if(b>a && a>c || c>a && a>b){
            printf("\n%d is a middle number",a);
        }
    
    
        if(a>b && b>c || c>b && b>a){
            printf("\n%d is a middle number",b);
        }
    
        if(a>c && c>b || b>c && c>a){
            printf("\n%d is a middle number",c);
        }
        return 0;
    }
    /*
    Output:
    Enter three numbers
    5
    6
    7
    
    6 is a middle number
    */
    
  • Find Smallest Number Among 3 Numbers
    
    	#include<stdio.h>
    int main()
    {
        int a, b, c;
        printf("Enter 3 Number's:\n");
        scanf("%d %d %d", &a, &b, &c);
        if(a<b && a<c){
            printf("The %d is Smallest Number.", a);
        }    
        else if(b<a && b<c){
            printf("The %d is Smallest Number.", b);
        }
        else{
            printf("The %d is Smallest Number.", c);
        }
        return 0;
    }
    
    
  • Program To Check Input Character Is Vowel Or Not
    					
    #include<stdio.h>
    int main()
    {
    
    	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);
    			}
    								
    		return 0;
    	}
    /*
    	Output:
    	Input a Character :  A
    	A is a vowel.
    */
    				
  • Basic While Loop Program For Print 1--10
    #include<stdio.h>
    int main()
    {
    	int a;
    	while (a<10)
    		{
    			printf("%d\t", a);
    			a++;
    		}					
    	return 0;
    }
    /*
    Output:
    0       1       2       3       4       5       6       7       8       9
    */
  • Basic Do-While Loop For Print 10-0
    #include<stdio.h>
    int main()
    {
    	int c=10;
    		do
    			{
    				printf("%d\t", c);
    				c=c-1;
    			} while (c>0);				
    	return 0;
    }
    /*
    Output:
    10      9       8       7       6       5       4       3       2       1
    */		
  • Program To Find Factorial using For Loop
    #include<stdio.h>
    int main()
    {
    	int fact, i, n;
    	fact = 1;
    	printf("Enter the number:\n");
    	scanf("%d" , &n);
    	for(i = 1; i <= n; i++)
    	{
    		fact = fact*i;
    	}
    	printf("Factorial of %d is %d", n , fact);
    	return 0;
    }
    /*
    Output:
    Enter the number:
    5
    Factorial of 5 is 120
    */
    
  • Nested For Loop Simple Program to Simple Pattern
    #include<stdio.h>
    int main()
    {
    	int i, j;
    	for(i=0;i<=5;i++){
    		for(j=0;j<=i;j++){
    			printf("*");
    		}
    		printf("\n");
    	}
    	return 0;
    }
    /*
    Output:
    *
    **
    ***
    ****
    *****
    ******
    */
  • Average Of 5 Numbers Using C Programming
    #include<stdio.h>
    int main()
    {
    	int a, b, c, d, e;
    	float avg;
    	printf("Enter 5 Number's :\n");
    	scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
    	avg=(a+b+c+d+e)/5;
    	printf("The Average Of 5 Number's is: %f", avg);
    	return 0;
    }/*
    Output:
    Enter 5 Number's :
    1
    2
    3
    5
    4
    The Average Of 5 Number's is: 3.000000
    */
  • Print Multipication Table Of Any Number
    #include <stdio.h>
    int main() {
      int n, i;
      printf("Enter an integer: ");
      scanf("%d", &n);
      for (i = 1; i <= 10; ++i) {
        printf("%d * %d = %d \n", n, i, n * i);
      }
      return 0;
    }
    /*
    Output:
    Enter an integer: 6
    6 * 1 = 6 
    6 * 2 = 12
    6 * 3 = 18
    6 * 4 = 24
    6 * 5 = 30
    6 * 6 = 36
    6 * 7 = 42
    6 * 8 = 48
    6 * 9 = 54
    6 * 10 = 60
    */
    
  • Sum Of N Number's Using C Programming
    #include <stdio.h>
    int main() {
        int n, i, sum = 0;
    
        printf("Enter A integer: ");
        scanf("%d", &n);
    
        for (i = 1; i <= n; ++i) {
            sum += i;
        }
    
        printf("Sum of 0 from %d Is: %d",n, sum);
        return 0;
    }
    /*
    Output:
    Enter A integer: 5
    Sum of 0 from 5 Is: 15
    */
    
  • Program in C To Find That The Weather Number Is Palindrome or not
    #include <stdio.h>
    int main() {
      int n, reversed = 0, remainder, original;
        printf("Enter an integer: ");
        scanf("%d", &n);
        original = n;
    
        while (n != 0) {
            remainder = n % 10;
            reversed = reversed * 10 + remainder;
            n /= 10;
        }
    
        if (original == reversed)
            printf("%d is a palindrome.", original);
        else
            printf("%d is not a palindrome.", original);
    
        return 0;
    }
    /*
    Output:
    Enter an integer: 101
    101 is a palindrome.
    */
    
  • C Programming To Find Average Using Array
    #include <stdio.h>
    int main()
    {
         int marks[10], i, n, sum = 0, average;
    
         printf("Enter number of elements: ");
         scanf("%d", &n);
    
         for(i=0; i<n; ++i)
         {
              printf("Enter number%d: ",i+1);
              scanf("%d", &marks[i]);
              
              sum += marks[i];
         }
    
         average = sum/n;
         printf("Average = %d", average);
    
         return 0;
    }
    /*
    Output:
    Enter number of elements: 5
    Enter number1: 1
    Enter number2: 2
    Enter number3: 3
    Enter number4: 4
    Enter number5: 5
    Average = 3
    */
    
  • Find The Variable Address Using Pointer
    #include <stdio.h>
    int main()
    {
      int Variable = 15;
      printf("Variable: %d\n", Variable);
    
      printf("address of Variable: %p", &Variable);  
      return 0;
    }
    /*
    Output:
    Variable: 15
    address of Variable: 000000000062FE1C
    */
    
  • C Program To Take Input Name and Print That
    #include <stdio.h>
    int main()
    {
        char name[20];
        printf("Enter name: ");
        scanf("%s", name);
        printf("Your name is %s.", name);
        return 0;
    }
    /*
    Output:
    Enter name: Tech 
    Your name is Tech.
    */
    
  • Find The Length Of String Using strlen() Function In C
    #include <stdio.h>
    #include <string.h>
    int main()
    {
      char a[100];
      int length;
    
      printf("Enter a string: \n");
      gets(a);
    
      length = strlen(a);
    
      printf("Length of the string = %d\n", length);
    
      return 0;
    }
    
  • Concatenates Two Strings using strcat() Function
    #include <stdio.h>
    #include <string.h>
    int main() {
       char str1[100] = "C Programming ", str2[] = "Language";
       strcat(str1, str2);
       puts(str1);
       return 0;
    }
    /*
    Output:
    C Programming Language
    */
    
  • Compare Two Strings Using strcmp() Function
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        char a[100], b[100];    
        printf("Enter the first string\n");    
        gets(a);    
        printf("Enter the second string\n");    
        gets(b);
        if( strcmp(a,b) == 0 )
            printf("Entered strings are equal.\n");
        else
            printf("Entered strings are not equal.\n");
    	return 0;
    }
    /*
    Output:
    Enter the first string
    Programming
    Enter the second string
    Programming
    Entered strings are equal.
    */
    
  • Convert Uppercase String using strlwr() Function
    #include<stdio.h>
    #include<string.h>
    int main()
    {
       char s[100];
       printf("Enter a string in Uppercase: ");
       scanf("%[^\n]",s);
       printf("In Lower Case:\n");
       puts(strlwr(s));
       return 0;
     }
    /*
    Output:
    Enter a string in Uppercase: PROGRAMMING
    In Lower Case:
    programming
     */
    
  • Convert Lowercase String Into Uppercase String Uisng strupr() Function
    #include<stdio.h>
    #include<string.h>
    int main()
    {
       char s[100];
       printf("Enter a string in Lowercase: ");
       scanf("%[^\n]",s);
       printf("In Uppercase Case:\n");
       puts(strupr(s));
       return 0;
     }
    /*
    Output:
    Enter a string in lowercase: programming
    In Uppercase Case:
    PROGRAMMING
    */
    
  • Reverse A String Using C Programming
    #include <stdio.h>
    #include <string.h>
    #define MAX_SIZE 100 
    
    int main()
    {
        char str[MAX_SIZE];
    
        printf("Enter any string: ");
        gets(str);
    
        printf("Original string = %s\n", str);
    
        strrev(str);
        printf("Reverse string = %s", str);
    
        return 0;
    }
    /*
    Output:
    Enter any string: Hello
    Original string = Hello
    Reverse string = olleH
    */
  • Check The String Is Palindrome Or Not Using C Programing
    
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_SIZE 100 
    
    int main()
    {
        char str[MAX_SIZE], reverse[MAX_SIZE];
        int flag;
    
        printf("Enter any string: ");
        gets(str);
    
    
        strcpy(reverse, str); 
        strrev(reverse);      
    
        flag = strcmp(str, reverse); 
    
        if(flag == 0)
        {
            printf("String is Palindrome.");
        }
        else
        {
            printf("String is Not Palindrome.");
        }
    
        return 0;
    }
    /*
    Output:
    Enter any string: mam 
    String is Palindrome.
    */
    
  • Create a Txt file and Save Details Of A Person
    #include<stdio.h>
    int main()
    {
        FILE *fptr;
        char name[20];
        int age;
        float salary;
    
        fptr = fopen("details.txt", "w");
    
        if (fptr == NULL)
        {
            printf("File does not exist.\n");
            return;
        }
        printf("Enter the name:\n");
        scanf("%s", name);
        fprintf(fptr, "Name  = %s\n", name);
    
        printf("Enter the age:\n");
        scanf("%d", &age);
        fprintf(fptr, "Age  = %d\n", age);
    
        printf("Enter the salary:\n");
        scanf("%f", &salary);
        fprintf(fptr, "Salary  = %.2f\n", salary);
    
        fclose(fptr);
        return 0;
    }
    /*Output:
    details.txt Will be created and ask for some details
    Enter the name:
    John
    Enter the age:
    27
    Enter the salary:
    26000
    
    this details will be saved on details.txt file in that format
    Name  = John
    Age  = 27
    Salary  = 26000.00
    */			
  • Length Conversion Using c Pogramming (Centimerter To Meter and KiloMeter)
    #include <stdio.h>
    
    int main()
    {
        float cm, meter, km;
    
        printf("Enter length in centimeter: ");
        scanf("%f", &cm);
    
        meter = cm / 100.0;
        km    = cm / 100000.0;
    
        printf("Length in Meter = %f m \n", meter);
        printf("Length in Kilometer = %f km", km);
    
        return 0;
    }
    /*
    Output:
    Enter length in centimeter: 1000
    Length in Meter = 10.000000 m 
    Length in Kilometer = 0.010000 km
    */
    
  • Find The Power Of Any Number
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        int base, expo, power;
    
        printf("Enter base: ");
        scanf("%d", &base);
        printf("Enter exponent: ");
        scanf("%d", &expo);
    
        power = pow(base, expo);
    
        printf("%d ^ %d = %d", base, expo, power);
    
        return 0;
    }
    /*
    Output:
    Enter base: 2
    Enter exponent: 2
    2 ^ 2 = 4
    */
    
  • Find The Square Root of Any Number
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        float num, root;
    
        printf("Enter any number to find square root: ");
        scanf("%f", &num);
    
        root = sqrt(num);
    
        printf("Square root of %.2f = %.2f", num, root);//%.2f is used for 2 numbers after decimal
    
        return 0;
    }
    /*
    Output:
    Enter any number to find square root: 100
    Square root of 100.00 = 10.00
    */
    
  • C Program to Find Simple Interest
    #include <stdio.h>
    int main()
    {
        float principle, time, rate, SI;
    
        printf("Enter principle (amount): ");
        scanf("%f", &principle);
    
        printf("Enter time: ");
        scanf("%f", &time);
    
        printf("Enter rate: ");
        scanf("%f", &rate);
        SI = (principle * time * rate) / 100;
        printf("Simple Interest = %.2f", SI);
        return 0;
    }
    /*
    Output:
    Enter principle (amount): 100000
    Enter time: 1
    Enter rate: 20
    Simple Interest = 20000.00
    */
    
    
  • C Program To Check The Weather Year Is Leap Year Or not
    #include <stdio.h>
    int main() {
       int year;
       printf("Enter a year: ");
       scanf("%d", &year);
    
       if (year % 400 == 0) {
          printf("%d is a leap year.", year);
       }
    
       else if (year % 100 == 0) {
          printf("%d is not a leap year.", year);
       }
    
       else if (year % 4 == 0) {
          printf("%d is a leap year.", year);
       }
       else {
          printf("%d is not a leap year.", year);
       }
    
       return 0;
    }
    /*
    Output:
    Enter a year: 2016
    2016 is a leap year.
    */
    
  • C Program To Check Alphabet or not
    #include<stdio.h>
    int main()
    {
      char ch;
      printf("Enter A Character:\n");
      scanf("%c", &ch);
    
      if((ch>='a' && ch<='z') ||(ch>='A' && ch<='Z')){
        printf("This is an alphabet");
      } 
      else{
          printf("This is Not Alphabet");
      }
      return 0;
    }
    /*
    Output:
    Enter A Character:
    a
    This is an alphabet
    */
    
  • C Program to print the day of week Using Switch-Case
    #include <stdio.h>
    int main()
    {
        int week;
        printf("Enter week number(1-7): ");
        scanf("%d", &week);
        
        switch(week)
        {
            case 1: 
                printf("Sunday");
                break;
            case 2: 
                printf("Monday");
                break;
            case 3: 
                printf("Tuesday");
                break;
            case 4: 
                printf("Wednesday");
                break;
            case 5: 
                printf("Thursday");
                break;
            case 6: 
                printf("Friday");
                break;
            case 7: 
                printf("Saturday");
                break;
            default: 
                printf("Invalid input! Please enter week number between 1-7.");
        }
        return 0;
    }
    /*
    Output:
    Enter week number(1-7): 4
    Wednesday
    */
    
  • C Program to Print All Alphabets from a-z
    #include<stdio.h>
    int main()
    {
        char ch;
    
        printf("Alphabets from a - z are: \n");
        for(ch='a'; ch<='z'; ch++)
        {
            printf("%c\t", ch);
        }
    
        return 0;
    }
    /*
    Output:
    Alphabets from a - z are: 
    a       b       c       d       e       f       g       h       i       j       
    k       l       m       n       o       p       q		r       s       t       
    u       v       w       x       y       z
    */
    
  • C Program To Print Even Number's from 0 To N
    #include<stdio.h>
    int main()
    {
        int i, n;
      
        printf("Print all even numbers till: ");
        scanf("%d", &n);
    
        printf("Even numbers from 1 to %d are: \n", n);
    
        for(i=1; i<=n; i++)
        {
            if(i%2 == 0)
            {
                printf("%d\n", i);
            }
        }
    
        return 0;
    }
    /*
    Output:
    Print all even numbers till: 10
    Even numbers from 1 to 10 are: 
    2
    4
    6
    8
    10
    */
    
  • C Program To Print Odd Number's from 0 To N
    #include<stdio.h>
    int main()
    {
        int i, n;
      
        printf("Print all Odd numbers till: ");
        scanf("%d", &n);
    
        printf("Odd numbers from 1 to %d are: \n", n);
    
        for(i=1; i<=n; i++)
        {
            if(i%2!= 0)
            {
                printf("%d\n", i);
            }
        }
    
        return 0;
    }
    /*
    Output:
    Print all Odd numbers till: 10
    Odd numbers from 1 to 10 are: 
    1
    3
    5
    7
    9
    */
    
  • C Program To Print Multiplication Table of Any Number
    #include <stdio.h>
    int main()
    {
        int i, num;
    
        printf("Enter number to print table: ");
        scanf("%d", &num);
    
        for(i=1; i<=10; i++)
        {
            printf("%d * %d = %d\n", num, i, (num*i));
        }
    
        return 0;
    }
    /*
    Output:
    Enter number to print table: 6
    6 * 1 = 6
    6 * 2 = 12
    6 * 3 = 18
    6 * 4 = 24
    6 * 5 = 30
    6 * 6 = 36
    6 * 7 = 42
    6 * 8 = 48
    6 * 9 = 54
    6 * 10 = 60
    */
    
  • C Program to find a number is negative, Zero(0) and Positive
    #include<stdio.h>
    int main()
    {
        int n;
        printf("Enter A Number:\n");
        scanf("%d", &n);
        if(n<0){
            printf("%d Is Negative", n);
        }    
        else if(n==0){
            printf("%d is Zero(0)", n);
        }
        else{
            printf("%d Is Positive", n);
        }
        return 0;
    }
    /*
    Output:
    Enter A Number:
    55
    55 Is Positive
    */
                    
  • C Program to input a employee data and print that
    #include<stdio.h>
    #include<string.h>
    struct Employee
    {
        char name[30];
        int roll_no;
        char favChar;
        char address[40];
    };
    int main()
    {
        struct Employee s1;
    
        printf("\nEnter Employee Name:\n");
    	scanf("%s", s1.name);
        printf("Enter Employee Salary.:\n");
        scanf("%d", &s1.roll_no);
    
        printf("Enter Employee Address:\n");
        scanf("%s", &s1.address);
    
    
        printf("\n\n\n\tThe Employee Name Is:%s", s1.name);
        printf("\n\tThe Employee Roll No. Is:%d", s1.roll_no);
        printf("\n\tThe Employee Address Is:%s", s1.address);
    
        return 0;
    }
    /*
    Output:
    Enter Employee Name:
    John
    Enter Employee Salary.:
    15000
    Enter Employee Address:
    Canada
    
    
    
            The Employee Name Is:John
            The Employee Roll No. Is:15000
            The Employee Address Is:Canada
    */
    
  • C Program to Input Student Detais and Print That
    #include <stdio.h>
    struct student
    {
        char name[30];
        int roll_no;
        char favChar;
        char address[40];
    };
    int main()
    {
        struct student s1;
    
        printf("\nEnter Student Name:\n");
    	scanf("%s", s1.name);
        printf("Enter Student Roll No.:\n");
        scanf("%d", &s1.roll_no);
    
        printf("Enter Student Favourate Chracter:\n");
        scanf(" %c", &s1.favChar);
    
        printf("Enter Student Address:\n");
        scanf("%s", &s1.address);
    
        printf("\nStudent Details");
        printf("\n\nThe Student Name Is:%s", s1.name);
        printf("\nThe Student Roll No. Is:%d", s1.roll_no);
        printf("\nThe Student Favourate Chracter Is:%c", s1.favChar);
        printf("\nThe Student Address Is:%s", s1.address);
    
        return 0;
    }
    /*
    Output:
    Enter Student Name:
    John
    Enter Student Roll No.:
    12 
    Enter Student Favourate Chracter:
    W
    Enter Student Address:
    France
    
    Student Details
    The Student Name Is:John
    The Student Roll No. Is:12
    The Student Favourate Chracter Is:W
    The Student Address Is:France
    */
  • C Program to Convert Celcius to Fahrenheit
    #include <stdio.h>
    int main()
    {
        float celsius, fahrenheit;
        printf("Enter temperature in Celsius: ");
        scanf("%f", &celsius);
        
        fahrenheit = (celsius * 9 / 5) + 32;
        printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);
        return 0;
    }
    /*
    Output:
    Enter temperature in Celsius: 0
    0.00 Celsius = 32.00 Fahrenheit
    */
    
  • C Program to reverse a number
    #include <stdio.h>
    int main() {
        int n, rev = 0, remainder;
        printf("Enter an integer: ");
        scanf("%d", &n);
        while (n != 0) {
            remainder = n % 10;
            rev = rev * 10 + remainder;
            n /= 10;
        }
        printf("Reversed number = %d", rev);
        return 0;
    }
    /*
    Output:
    Enter an integer: 5542
    Reversed number = 2455
    */
    
  • C Program to find a factorial of number using recursion
    #include<stdio.h>
    long int multiplyNumbers(int n);
    int main() {
        int n;
        printf("Enter a positive integer: ");
        scanf("%d",&n);
        printf("Factorial of %d = %ld", n, multiplyNumbers(n));
        return 0;
    }
    
    long int multiplyNumbers(int n) {
        if (n>=1)
            return n*multiplyNumbers(n-1);
        else
            return 1;
    }
    /*
    Output:
    Enter a positive integer: 5
    Factorial of 5 = 120
    */
    
  • Star Patter's Programs

  • C Program to print star pyramid Pattern
    #include <stdio.h>
    int main()
    {
        int n, s, i, j;
        printf("Enter number of rows: ");
        scanf("%d", &n);
        for (i = 1; i <= n; i++)
        {
            for (s = i; s < n; s++)
            {
                printf(" ");
            }
            for (j = 1; j <= (2 * i - 1); j++)
            {
                printf("*");
            }
            printf("\n");
        }
        return 0;
    }
    /*
    Output:
    Enter number of rows: 5
        *
       ***
      *****
     *******
    *********
    */
    
  • C Program to print half star Pattern Pyramid
    #include <stdio.h>
    int main()
    {
        int i, j, n;
        printf("Enter number of rows: ");
        scanf("%d", &n);
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
        return 0;
    }
    /*
    OUtput:
    Enter number of rows: 5
    * 
    * *
    * * *
    * * * *
    * * * * *
    
    */
    
  • C Program to print inverted half star pyramid pattern
    #include <stdio.h>
    int main()
    {
        int i, j, n;
        printf("Enter number of rows: ");
        scanf("%d", &n);
        for (i = n; i >= 1; i--)
        {
            for (j = 1; j <= i; j++)
            {
                printf("* ");
            }
            printf("\n");
        }
        return 0;
    }
    /*
    Output:
    Enter number of rows: 5
    * * * * * 
    * * * *
    * * *
    * *
    *
    
    */
  • C Program to print Hollow star pyramid pattern
    #include <stdio.h>
    int main()
    {
        int r, i, j, s;
        printf("Enter number of rows: ");
        scanf("%d", &r);
        for (i = 1; i <= r; i++)
        {
            for (s = i; s < r; s++)
                printf(" ");
            for (j = 1; j <= (2 * r - 1); j++)
            {
                if (i == r || j == 1 || j == 2 * i - 1)
                    printf("*");
                else
                    printf(" ");
            }
            printf("\n");
        }
        return 0;
    }
    /*
    Output:
    Enter number of rows: 5
        *        
       * *
      *   *
     *     *
    *********
    */
    
  • C Program to print reverse hollow star pyramid pattern
    #include <stdio.h>
    int main()
    {
        int r, i, j, s;
        printf("Enter number of rows: ");
        scanf("%d", &r);
        for (i = r; i >= 1; i--)
        {
            for (s = i; s < r; s++)
                printf(" ");
            for (j = 1; j <= 2 * i - 1; j++)
            {
                if (i == r || j == 1 || j == 2 * i - 1)
                    printf("*");
                else
                    printf(" ");
            }
            printf("\n");
        }
        return 0;
    }
    /*
    Output:
    Enter number of rows: 5
    *********
     *     *
      *   *
       * *
        *
    */
    
  • C Program To Print Square Star Pattern
    #include <stdio.h>
    int main()
    {
        int n, i, j;
        printf("Enter the number of rows:\n");
        scanf("%d", &n);
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < n; j++)
            {
                printf("*");
            }
            printf("\n");
        }
    
        return 0;
    }
    /*
    Output:
    Enter the number of rows:
    5
    *****
    *****
    *****
    *****
    *****
    */
    
  • C Program To Print Plus Pattern
    #include <stdio.h>
    
    int main()
    {
        int n, i, j;
        printf("Enter The Number To Print Plus Pattern(Odd Only):\n");
        scanf("%d", &n);
        for (i = 1; i <= n; i++)
        {
            if (i == ((n / 2) + 1))
            {
                for (j = 1; j <= n; j++)
                {
                    printf("+");
                }
            }
            else
            {
                for (j = 1; j <= n / 2; j++)
                {
                    printf(" ");
                }
                printf("+");
            }
            printf("\n");
        }
        return 0;
    }
    /*
    Output:
    Enter The Number To Print Plus Pattern(Odd Only):
    5
      +
      +
    +++++
      +
      +
    */
    
  • C Program To Print X Star Pattern
    #include <stdio.h>
    
    int main()
    {
        int n, m, i, j;
        printf("Enter the number:\n");
        scanf("%d", &n);
        m = 2 * n - 1;
        for (i = 1; i <= m; i++)
        {
            for (j = 1; j <= m; j++)
            {
                if (i == j || j == (m - i + 1))
                {
                    printf("*");
                }
                else
                {
                    printf(" ");
                }
            }
            printf("\n");
        }
        return 0;
    }
    /*
    Output:
    Enter the number5
    *       *
     *     *
      *   *
       * *
        *
       * *
      *   *
     *     * 
    *       *
    */
    
  • C Programming Project

  • Calculator Using Case Statement
    
    #include <stdio.h>
    int main()
    {
        int a, b;
        char op;
        float result;
    
        printf("Enter First Number:");
        scanf("%d", &a);
    
        printf("Enter Second Number:");
        scanf("%d", &b);
    
        printf("Enter OPerater:");
        scanf(" %c", &op);
    
        switch (op)
        {
        case '+':
            result = a + b;
            printf("The Sum Is %f", result);
            break;
    
        case '-':
            result = a - b;
            printf("The Subtracr Is %f", result);
            break;
    
        case '*':
            result = a * b;
            printf("The Multiply Is %f", result);
            break;
    
        case '/':
            result = a / b;
            printf("The Division Is %f", result);
            break;
    
        default:
            printf("Please Envalid Operater");
            break;
        }
        return 0;
    }
    /*
    Output:
    Enter First Number:4
    Enter Second Number:5
    Enter OPerater:+
    The Sum Is 9.000000
    */
        
  • 1 Comments