Pages

Saturday, January 25, 2025

Write a program in C for grade calculation using if…else if ladder and switch Statement. Accept marks of 3 subjects calculate total and based on it calculate Grade.

FLOWCHART
  • Start
  • Input marks for 3 subjects (sub1, sub2, sub3)
  • Calculate Total Marks (total = sub1 + sub2 + sub3)
  • Calculate Average Marks (average = total / 3)
  • Check if all subjects have marks >= 33 (to pass)
    • If YES, proceed to calculate the grade.
    • If NO, print "FAILED" and stop.
  • Check the grade based on the average using if-else conditions:
    • If average >= 80, assign grade 'A'.
    • Else if average >= 65, assign grade 'B'.
    • Else if average >= 45, assign grade 'C'.
    • Else if average >= 33, assign grade 'D'.
    • Else assign grade 'F' (failed).
  • Display total marks, average, and grade.
  • End


    #include <stdio.h>

    int main() {
        int sub1, sub2, sub3, total;
        float average;
        char grade;
        
        // Input marks for 3 subjects
        printf("Enter marks of 1st Subject: ");
        scanf("%d", &sub1);
        
        printf("\nEnter marks of 2nd Subject: ");
        scanf("%d", &sub2);
        
        printf("\nEnter marks of 3rd Subject: ");
        scanf("%d", &sub3);
        
        // Total Marks and Average
        total = sub1 + sub2 + sub3;
        average = total / 3.0;
        
        // Check if passed
        if (sub1 >= 33 && sub2 >= 33 && sub3 >= 33) {
            printf("\nTotal Marks: %d", total);
            printf("\nAverage Marks: %.2f", average);
            
            // Grade calculation using if...else if ladder
            if (average >= 80) {
                grade = 'A';
            } else if (average >= 65) {
                grade = 'B';
            } else if (average >= 45) {
                grade = 'C';
            } else if (average >= 33) {
                grade = 'D';
            } else {
                grade = 'F'; // F for fail if below 33
            }
            
            // Display grade using switch statement
            switch(grade) {
                case 'A':
                    printf("\nStudent has A grade.");
                    break;
                case 'B':
                    printf("\nStudent has B grade.");
                    break;
                case 'C':
                    printf("\nStudent has C grade.");
                    break;
                case 'D':
                    printf("\nStudent has D grade.");
                    break;
                case 'F':
                    printf("\nStudent has FAILED, advised to re-take exam.");
                    break;
                default:
                    printf("\nInvalid grade.");
            }
        } else {
            printf("\nStudent has FAILED, advised to re-take exam.");
        }

        return 0;
    }
  • Friday, January 24, 2025

    To check whether the triangle is isosceles, scalene or equilateral triangle. in C language

     Mistakes:- 
    1. Difference b/w || and &&

    || (Logical OR)

    • ||(OR) operator checks whether one of condition is true or not..
    • It returns:
      • True (1): If either or both conditions are true.
      • False (0): If both conditions are false.

    && (Logical AND)

    • &&(AND) operator  Checks whether both conditions are true or not.
    • It returns:
      • True (1): If both conditions are true.
      • False (0): If either one or both conditions are false.

    // Check if the triangle is equilateral , Isoceles, scalene.

    #include <stdio.h>


    int main() {

        int a,b,c;

        

        

        printf("Enter a side of Triangle: ");

        scanf("%d", &a);

        

         printf("Enter b side of Triangle: ");

        scanf("%d", &b);

        

         printf("Enter c side of Triangle: ");

        scanf("%d", &c);

        

        // || is OR operator it checks whether one of the condition is true or not

        // && is AND operator it checks whether both conditions are true or not.

        

        

        if(a==b && b==c){

        printf("Triangle is Equilateral"); 

        } else if(a==b || b==c || a==c) {

            printf("Triangle is Isoceles");

            } else {

                printf("Triangle is Scalene.");

        }


        return 0;

    }




    Thursday, January 23, 2025

    Calculate Compound interest in C Language

    #include<stdio.h>
    #include<math.h>

    int main() {
    float CompoundInterest, rate, time, principal, n, amount;

    /*

    1. Take Input from User
    principal amount, rate(%), n(per yr), time(in yrs)

    n = no. of times interest compounded per year

    */

    rate = rate /100;

    amount = principal * pow((1+r/n), n*time);

    CompoundInterest = amount-principal;


    /*

    2. Display the Result

    printf("The Total Amount: %.2f\n", amount);

    printf("The Compound Interest %.2f\n", CompoundInterest);

    */


    return 0;

    }


    CODE TESTED and RAN!

    Update:-
    - Reduced complexity, confusion.
    - Made it understandable & readable.

    Mistakes:- 
    - don't forget to put comma "," after pow here's an example:-
    principal * pow(1+r/n),n*time);

    Calculate Simple Interest in C language

     #include<stdio.h>


    int main() {

        int principal, t, rate;

        

        printf("Enter the Principal Rate: ");

        scanf("%d", &principal);

        

        printf("Enter the Time in years: ");

        scanf("%d", &t);

        

        printf("Enter the Rate: ");

        scanf("%d", &rate);

        

        int SI = (principal*t*rate)/100;

        printf("The Simple Interest is: %d", SI);

        

        


    return 0;


    }

    CODED and Tested

    1. Write a c program to display your Name, address and city in different lines.in C Language

    #include<stdio.h>

    int main() {

    char Name[100], Address[100], City[100];

    // Input Name, Address and City...

    printf("Enter your Name: ");

    fgets(Name, sizeof(Name), stdin);


    printf("Enter your Address: ");

    fgets(Address, sizeof(Address), stdin);


    printf("Enter your City: ");

    fgets(City, sizeof(City), stdin);


    printf("---Displaying all the information below---");

    printf("\nName: %s", Name);

    printf("Address: %s", Address);

    printf("City: %s", City);


    return 0;

    }


    CODE:- Tested- Works.

    Tuesday, January 14, 2025

    Write a program to print multiplication table of 10 in reversed order. in C Language

     My mistakes:-

    1. couldn't figure out CONDITION. just i ( ZERO OR NONE)


    // Write a program to print multiplication table of 10 in reversed order.
    #include <stdio.h>

    int main()
    {
        int n;
        scanf("%d", &n);
        for( int i = 10; i ; i--) // for(initialize; conditon; increment or decrement)
        {
            printf("%d x %d = %d\n", n, i, n*i);
        }

       
        return 0;

    }

    Monday, January 13, 2025

    First sum of natural numbers in C language

    The actual calculation behind:-

    1. while(i<=n) {

        sum = sum + i;
        i++;
    }

    or

    2. for(i = 1; i<=n; i++) {
    sum += i; // sum = sum + i;
    }

    advantages of for loop:-
    -  more easier.
    - makes code concise and more clean.


    // First sum of natural numbers in C language
    #include <stdio.h>

    int main() {
        int n, sum = 0; /// sum = 0 stores the number in memory
        printf("Enter your first N natural number: ");
        scanf("%d", &n);
        
        // Using a loop to calculate the sum
        // for(int i = 1; i <= n; i++) {
        //     printf("%d\n", i);
        //     sum += i;
        // }
        sum = (n*(n+1))/2;
        
        printf("The first sum of %d Natural numbers are: %d\n", n, sum);
        
        

        return 0;
    }


    FORMULA:- 


    Thursday, January 2, 2025

    Check whether a number is Prime Number or not

     Check whether a number is Prime Number or not

    Mistakes:- 

    Theory-wise:-
    I didn't knew what is Prime Numbers:
    **Prime Number:- Divisible by 1 and itself only.
    Composite Number:- Divisible by 1 and itself as well as other numbers too.


    Table of any given number in C language

     Table of any given number in C language

    Mistakes:-
    1.) I put "if" instead of "for" loop which led to a huge error in the beginning.

    if loop:- 
    if (condition) { // Code to be executed if the condition is true }

    **for loop:-
    - for (initialization; condition; update){
    }
    -initializes a value once then tests the condition after each iteration, 
    -stops when condition is false.

    2.) after putting the loop control statement, i forgot to put {  }



    // Table of any given number upto 10.
    #include <stdio.h>

    int main() {
        // Declare the variables num, n
       int num, n;

    // User enters the value of any given number
    printf("Enter the value:- ");
    scanf("%d", &num);

    //Shows which table is being printed
    printf("\nThe table of %d", num);

    for (n = 1;n <= 10; n++) // for is initialization, condition, increment
    {
    // Prints the table upto 10 
    printf("\n%d x %d = %d", num, n, num*n);

    }

        return 0;
    }


    Palindromic Number checker with C

     Mistakes:- 

    1. 1. i forgot to put } before "else" statement.
    2. Logic: Store original, then compare original == reversed.
    1. Key Operations: Use % to extract digits, / to remove digits, and *10 to build reversed.
    2. Important: Preserve original for comparison.


    Tip:-  Always close if block with } before starting an else block.

    correct:- if (condition) { /* Code */ } else { /* Code */ }


    #include <stdio.h>

    int main() {
        int n, original, reversed = 0, remainder;

        printf("Enter the integer: ");  // Input the integer value
        scanf("%d", &n);

        // Store the original value of n
        original = n;

        // Get the reversed value of the number
        while (n != 0) {
            remainder = n % 10;          // Get the last digit
            reversed = reversed * 10 + remainder;  // Build the reversed number
            n /= 10;                     // Remove the last digit
        }

        // Check if the original number is equal to the reversed number
        if (original == reversed) {
            printf("The number is a Palindrome.\n");
        } else {
            printf("The number is not a Palindrome.\n");
        }

        return 0;
    }






    Wednesday, January 1, 2025

    C- Operator and their uses with examples

    C- Operator and their uses with examples



    1. Unary Operator
    a) num-- means decrement of 5 to 4 as an example! :)

    // Online C compiler to run C program online
    #include <stdio.h>

    int main() {
       int num = 5;
       
       printf("The value of 5 before decrement is:- %d", num);
       printf("\nThe value of 5 using decrement is:- %d", num--);
       printf("\nThe value of 5 after decrement is:- %d", num);
       
        return 0;
    }

    OUTPUT:-
    The value of 5 before decrement is:- 5
    The value of 5 using decrement is:- 5
    The value of 5 after decrement is:- 4

    === Code Execution Successful ===

    b) num++ is the vice-e-versa of num--
    // Online C compiler to run C program online
    #include <stdio.h>

    int main() {
       int num = 5;
       
       printf("The value of 5 before decrement is:- %d", num);
       printf("\nThe value of 5 using decrement is:- %d", num++);
       printf("\nThe value of 5 after decrement is:- %d", num);
       
        return 0;
    }


    OUTPUT :-
    The value of 5 before decrement is:- 5
    The value of 5 using decrement is:- 5
    The value of 5 after decrement is:- 6

    === Code Execution Successful ===

    Binary Search in C++ (University Exam Topic) - Review Program

     #include <iostream> using namespace std; int binarySearch(int arr[], int size, int key) {     int si = 0;      int ei = size-1;      ...