C Programming

 C Programming 

1. Write a program in C to print any name and give to a welcome message. 
Ans:


        #include <stdio.h>

        int main() {
            char name[100];
            printf("Please enter your name: ");
            scanf("%s", name);

            printf("Welcome, %s! This is C programming.\n", name);

            return 0;
        }




2. Write a program in C to accept two number and find there sum, subtract, multiple and divided.

Ans:


        #include <stdio.h>

        int main() {
            float num1, num2;
            float sum, subtract, multiple, division;

            // Accepting input for the two numbers
            printf("Enter the first number: ");
            scanf("%f", &num1);

            printf("Enter the second number: ");
            scanf("%f", &num2);

            // Calculating the sum, subtract, multiple, and division
            sum = num1 + num2;
            subtract = num1 - num2;
            multiple = num1 * num2;
            division = num1 / num2;

            // Printing the results
            printf("Sum: %.2f\n", sum);
            printf("subtract: %.2f\n", subtract);
            printf("multiple: %.2f\n", multiple);
            printf("Division: %.2f\n", division);

            return 0;
        }




3. Write a program in C to print Name, Qualification, Address, Age and postal address using input function.

Ans:


    #include <stdio.h>

    int main() {
        char name[50];
        char qualification[100];
        char address[100];
        int age;
        long int postalAddress;

        // Accepting input for personal details
        printf("Enter your name: ");
        scanf("%s", name);

        printf("Enter your qualification: ");
        scanf("%s", qualification);

        printf("Enter your address: ");
        scanf("%s", address);

        printf("Enter your age: ");
        scanf("%d", &age);

        printf("Enter your postal address: ");
        scanf("%ld", &postalAddress);

        // Printing the personal details
        printf("\n\n***************Output***************\n\n");
   
        printf("The name is %s\n", name);
        printf("The Qualification is %s\n", qualification);
        printf("The postal Address is %s\n", address);
        printf("The age is %d\n", age);
        printf("And Postal Address is %ld\n", postalAddress);

        return 0;
    }




4.  Write a program in C to print  a car structure that have CarName, Cardata, CarNumber, CarPrice and car dealer name.



    #include <stdio.h>

    // Define the structure for a car
    struct Car {
        char carName[50];
        char carOwner[50];
        char carNumber[20];
        double carPrice;
        char dealerName[50];
    };

    int main() {
        // Create an instance of the Car structure
        struct Car myCar;

        // Accept input for car details
        printf("Enter Car Name: ");
        scanf("%s", myCar.carName);

        printf("Enter Car Owner: ");
        scanf("%s", myCar.carOwner);

        printf("Enter Car Number: ");
        scanf("%s", myCar.carNumber);

        printf("Enter Car Price: ");
        scanf("%lf", &myCar.carPrice);

        printf("Enter Dealer Name: ");
        scanf("%s", myCar.dealerName);

        // Print the car details
        printf("\n*************Output************\n\n\n");
        printf("Car Name: %s\n", myCar.carName);
        printf("Car Owner: %s\n", myCar.carOwner);
        printf("Car Number: %s\n", myCar.carNumber);
        printf("Car Price: $%.2lf\n", myCar.carPrice);
        printf("Dealer Name: %s\n", myCar.dealerName);

        return 0;
    }




5. Write a code in C to determine if a candidate is selected or not selected on the base of candidate experience like Qualification and years of experience using input method.



    #include <stdio.h>

    int main() {
        char qualification;
        int experience;

        // Accept input for qualification
        printf("Enter candidate's qualification (G for Graduation, N for Not Graduated): ");
        scanf(" %c", &qualification);

        // Accept input for years of experience
        printf("Enter candidate's years of experience: ");
        scanf("%d", &experience);
    printf("\n*************Output************\n");
        // Check if the candidate is selected
        if (qualification == 'G' && experience >= 5) {
            printf("Candidate is selected!\n");
        } else {
            printf("Candidate is not selected.\n");
        }

        return 0;
    }




6. Determine if an input character is a Vowel or Consonant.


    #include <stdio.h>

    int main() {
        char ch;

        // Accept input for the character
        printf("Enter a character: ");
        scanf(" %c", &ch);

        // Check if the character is a vowel or a consonant
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
            ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
            printf("The character is vowel.\n");
        } else {
            printf("The character is consonant.\n");
        }

        return 0;
    }




7. Write a C program to find the simple interest, given principle * rate of interest and times.


    #include <stdio.h>

    int main() {
        float principal, rate, time, simpleInterest;

        // Accept input for principal amount
        printf("Enter the principal amount: ");
        scanf("%f", &principal);

        // Accept input for rate of interest
        printf("Enter the rate of interest (in percentage): ");
        scanf("%f", &rate);

        // Convert rate of interest from percentage to decimal
        rate = rate / 100.0;

        // Accept input for time in years
        printf("Enter the time (in years): ");
        scanf("%f", &time);

        // Calculate simple interest
        simpleInterest = (principal * rate * time);

    printf("\n\n************Output***********\n");
        // Print the calculated simple interest
        printf("Simple Interest: %.2f\n", simpleInterest);

        return 0;
    }









8. This program accepts a name and a number and print the name many times as the number entered.



    #include <stdio.h>

    int main() {
        char name[100];
        int number;

        // Prompt the user to enter a name
        printf("Enter a name: ");
        scanf("%s", name);

        // Prompt the user to enter a number
        printf("Enter a number: ");
        scanf("%d", &number);

        if (number <= 0) {
            printf("Number should be a positive integer.\n");
            return 1; // Exit with an error code
        }

        // Print the name 'number' times
        for (int i = 0; i < number; i++) {
            printf("Name: %s\n", name);
        }

        return 0; // Exit the program successfully
    }



9. Write a C program to find the biggest of three numbers.


    #include <stdio.h>

    int main() {
        double num1, num2, num3;

        // Prompt the user to enter three numbers
        printf("Enter the first number: ");
        scanf("%lf", &num1);
       
        printf("Enter the second number: ");
        scanf("%lf", &num2);
       
        printf("Enter the third number: ");
        scanf("%lf", &num3);

        // Compare the numbers to find the largest
        if (num1 >= num2 && num1 >= num3) {
            printf("The largest number is: %.2lf\n", num1);
        } else if (num2 >= num1 && num2 >= num3) {
            printf("The largest number is: %.2lf\n", num2);
        } else {
            printf("The largest number is: %.2lf\n", num3);
        }

        return 0;
    }





10. Make the program.

*
**
***
****
*****


    #include <stdio.h>

    int main() {
        int rows;
       
        // Prompt the user to enter the number of rows
        printf("Enter the number of rows: ");
        scanf("%d", &rows);

        // Loop through each row
        for (int i = 1; i <= rows; i++) {
            // Loop to print 'i' number of stars in the current row
            for (int j = 1; j <= i; j++) {
                printf("*");
            }
            printf("\n"); // Move to the next row
        }

        return 0;
    }




11. Write a C program that calculates the sum of three numbers entered by the user and displays the result.



    #include <stdio.h>

    long int adder(long int a, long int b, long int c) {
        return (a + b + c);
    }

    int main() {
        long int x, y, z, sum;

        printf("\nEnter a number: ");
        scanf("%ld", &x);

        printf("Enter the second number: ");
        scanf("%ld", &y);

        printf("Enter the third number: ");
        scanf("%ld", &z);

        sum = adder(x, y, z);
        printf("\nThe total of %ld + %ld + %ld = %ld\n", x, y, z, sum);

        return 0;
    }




12. Write a C program that allows the user to perform arithmetic operations (+, -, *, /) on two numbers. The program should take input from the user and display the result.


    #include <stdio.h>

    char calculate(int, int, char);

    int main() {
        int x, y;
        char ans, oper;

        ans = 'y';
        while (ans != 'n' && ans != 'N') {
            printf("\nThis function allows you to perform arithmetic operations on two numbers.\n");
            printf("Enter the first number: ");
            scanf("%d", &x);
            fflush(stdin);
            printf("Enter the second number: ");
            scanf("%d", &y);
            fflush(stdin);
            printf("Enter Arithmetic operator [+,-,*,/]: ");
            scanf(" %c", &oper);
            fflush(stdin);
            ans = calculate(x, y, oper);
        }

        printf("Program End\n"); // Added "Program End" message

        return 0;
    }

    char calculate(int a, int b, char oper) {
        int result;
        char ch;

        switch (oper) {
            case '+':
                result = a + b;
                break;
            case '-':
                result = a - b;
                break;
            case '*':
                result = a * b;
                break;
            case '/':
                if (b != 0) {
                    result = a / b;
                } else {
                    printf("\n\nDivision by zero is not allowed.");
                    printf("\nTo return......");
                    break;
                }
                break;
            default:
                printf("\n\nInvalid arithmetic operator.");
                printf("\nTo return......");
                break;
        }
        if (oper != '/' || b != 0) {
            printf("\n\n%d %c %d = %d", a, oper, b, result);
        }
        printf("\n\nContinue y/n or Y/N: ");
        scanf(" %c", &ch);
        fflush(stdin);
        return ch;
    }




13. Write a C program that allows a user to input their username and password, with password characters masked by asterisks. After entering the password, display it.

    #include <stdio.h>
    #include <string.h>

    int main() {
        char password[10], userName[10];
        int i;

        printf("Enter user name: ");
        fgets(userName, sizeof(userName), stdin);
        userName[strcspn(userName, "\n")] = '\0'; // Remove the newline character from the user name

        printf("Enter the password (8 characters): ");
        for (i = 0; i < 8; i++) {
            password[i] = getchar();
            putchar('*');
        }
        password[i] = '\0';  // Null-terminate the password string

        printf("\nYour Password is: %s\n", password);

        return 0;
    }
   



14. Explain the purpose and functionality of the C program provided. Discuss how it handles user input, including both valid and invalid choices. Also, describe the mechanism by which the program ensures continuous execution until the user chooses to exit.



    #include <stdio.h>

    int main() {
        char choice;

        while (1) {
            printf("\n\n Menu of the 7 Days");
            printf("\n\n Enter a number [1-7]: \t");
            choice = getchar();
            while (getchar() != '\n'); // Clear the input buffer

            switch (choice) {
                case '1':
                    printf("\n Yes, Today is Monday\n Have a Nice Day");
                    break;
                case '2':
                    printf("\n Yes, Today is Tuesday\n Have a Nice Day");
                    break;
                case '3':
                    printf("\n Yes, Today is Wednesday\n Have a Nice Day");
                    break;
                case '4':
                    printf("\n Yes, Today is Thursday\n Have a Nice Day");
                    break;
                case '5':
                    printf("\n Yes, Today is Friday\n Have a Nice Day");
                    break;
                case '6':
                    printf("\n Yes, Today is Saturday\n Have a Nice Day");
                    break;
                case '7':
                    printf("\n Yes, Today is Sunday\n Have a Nice Day");
                    break;
                case '8':
                    printf("\n Exiting the program.");
                    return 0;
                default:
                    printf("\n Invalid Choice entered. [1-7]");
            }

            printf("\nPress Enter to continue...");
            while (getchar() != '\n'); // Wait for the user to press Enter
        }

        return 0;
    }



15. write a C program to display the inventory of item in a store/shop.
The inventory maintains details such as name, price quantity and manufacturing date of each item. using input function and output should be show like below.

Sl No        Name                Code         Quantity      Prince         MFG Date
1.               Tea Powder       123               23               40                   12/03/2007
2.               Milk                      345             20                 80                  30/03/2007
3.               Soap                    510              10                  30                  01/04/2007
4.               Washing Soap   890           25                 12                   10/03/2007
5.                Shampoo             777          8                      50                17/05/2007


    #include <stdio.h>

    // Structure to store item details
    struct Item {
        int slNo;
        char name[30];
        int code;
        int quantity;
        float price;
        char mfgDate[12];
    };

    int main() {
        // Create an array of structures to store item details
        struct Item inventory[5];

        // Input item details
        for (int i = 0; i < 5; i++) {
            inventory[i].slNo = i + 1;

            printf("Enter the name of item %d: ", i + 1);
            scanf("%s", inventory[i].name);

            printf("Enter the code of item %d: ", i + 1);
            scanf("%d", &inventory[i].code);

            printf("Enter the quantity of item %d: ", i + 1);
            scanf("%d", &inventory[i].quantity);

            printf("Enter the price of item %d: ", i + 1);
            scanf("%f", &inventory[i].price);

            printf("Enter the manufacturing date of item %d (dd/mm/yyyy): ", i + 1);
            scanf("%s", inventory[i].mfgDate);

            // Clear the input buffer
            while (getchar() != '\n');
        }

        // Display the header
        printf("Sl No\t\t\tName\t\t\tCode\t\t\tQuantity\t\t\tPrice\t\t\tMFG Date\n");

        // Display the inventory details
        for (int i = 0; i < 5; i++) {
            printf("%d.\t%-20s%d\t%d\t%.2f\t%s\n",
                inventory[i].slNo,
                inventory[i].name,
                inventory[i].code,
                inventory[i].quantity,
                inventory[i].price,
                inventory[i].mfgDate);
        }

        return 0;
    }





16. write a function in C program to convert an input string to uppercase


    #include <stdio.h>
    #include <ctype.h>

    // Function to convert a string to uppercase
    void stringToUpper(char *str) {
        for (int i = 0; str[i]; i++) {
            str[i] = toupper((unsigned char)str[i]);
        }
    }

    int main() {
        char inputString[100];

        printf("Enter a string: ");
        fgets(inputString, sizeof(inputString), stdin);

        // Remove the newline character at the end of the string
        if (inputString[strlen(inputString) - 1] == '\n') {
            inputString[strlen(inputString) - 1] = '\0';
        }

        stringToUpper(inputString);

        printf("Uppercase String: %s\n", inputString);

        return 0;
    }





17. Write a function to display the highest and lowest of 10 number input.


    #include <stdio.h>

    int main() {
        int numbers[10];
        int highest, lowest;

        // Input 10 numbers separated by spaces
        printf("Enter 10 numbers separated by spaces: ");
       
        for (int i = 0; i < 10; i++) {
            scanf("%d", &numbers[i]);
        }

        // Initialize highest and lowest with the first number
        highest = lowest = numbers[0];

        // Find the highest and lowest numbers
        for (int i = 1; i < 10; i++) {
            if (numbers[i] > highest) {
                highest = numbers[i];
            }
            if (numbers[i] < lowest) {
                lowest = numbers[i];
            }
        }

        // Display the highest and lowest numbers
        printf("The highest number is: %d\n", highest);
        printf("The lowest number is: %d\n", lowest);

        return 0;
    }



18.


    #include <stdio.h>
    int main()
    {
        int i,j,matrix [3] [4];
       
        for (i=0; i<3;i++)
    {
        printf("\n\n enter numbers for row %d\n\t",i+1);
        for(j=0; j<4;j++)
    {
        scanf("%d",& matrix[i][j]);
        fflush(stdin);
        printf("\t");
    }
    }
    printf("\n\n\n you entered...");
    for(i=0;i<3;i++)
    {
        printf("\n row &d\t",j=1);
        for(j=0;j<4;j++)
        {
            printf("%d\t", matrix[i][j]);
        }
        return 0;
    }
    }
       












Comments

Popular posts from this blog

HTML

MS Excel

CSS