c class- 1

c

Awasthi sir

8/25/202410 min read

Keywords in C are reserved words that have special meaning to the compiler and cannot be used as identifiers (like variable names, function names, etc.). Here are some of the common keywords in C:

  1. auto - Used to declare automatic variables.

  2. break - Exits from a loop or a switch statement.

  3. case - Used in switch statements to specify different cases.

  4. char - Declares a character data type variable.

  5. const - Declares a constant value that cannot be modified.

  6. continue - Skips the remaining statements in the current iteration of a loop and proceeds with the next iteration.

  7. default - Specifies the default block of code in a switch statement.

  8. do - Used with while to create a do-while loop.

  9. double - Declares a double-precision floating-point variable.

  10. else - Specifies the block of code to be executed if the condition in an if statement is false.

  11. enum - Defines a set of named integer constants.

  12. extern - Declares a variable or function as external, meaning it is defined elsewhere.

  13. float - Declares a floating-point variable.

  14. for - Used to create a for loop.

  15. goto - Transfers control to a labeled statement within a function.

  16. if - Specifies a block of code to be executed if a condition is true.

  17. int - Declares an integer variable.

  18. long - Declares a long integer variable.

  19. register - Suggests that the variable be stored in a CPU register.

  20. return - Exits from a function and optionally returns a value.

  21. short - Declares a short integer variable.

  22. signed - Specifies that a variable can hold both positive and negative values.

  23. sizeof - Returns the size of a variable or data type.

  24. static - Declares a static variable or function.

  25. struct - Defines a structure (a user-defined data type).

  26. switch - Creates a switch statement for multi-way branching.

  27. typedef - Defines a new data type name.

  28. union - Defines a union (a user-defined data type that can store different data types in the same memory location).

  29. unsigned - Specifies that a variable can hold only positive values.

  30. void - Specifies that a function does not return a value.

  31. volatile - Indicates that a variable's value may be changed by something outside the control of the program.

  32. while - Creates a while loop.

In the C programming language, an identifier is a name used to identify a variable, function, array, or any other user-defined item in the program. Identifiers are fundamental components of any program because they are used to refer to the various data and functions within the code.

Characteristics of Identifiers:

  • Uniqueness: Each identifier must be unique within its scope. For example, you can't have two variables with the same name in the same function.

  • Case-Sensitive: In C, identifiers are case-sensitive. This means Variable, variable, and VARIABLE would all be considered different identifiers.

  • Allowed Characters: Identifiers can consist of letters (both uppercase and lowercase), digits, and underscores (_). However, they must begin with a letter or an underscore. They cannot begin with a digit.

  • Keywords Restriction: Identifiers cannot be the same as C's reserved keywords (e.g., int, return, for, etc.).

Question:

Write a C program that asks the user to enter their age and then prints out whether they are a "Child," "Teenager," "Adult," or "Senior" based on the following criteria:

  • Child: Age is less than 13.

  • Teenager: Age is between 13 and 19 (inclusive).

  • Adult: Age is between 20 and 64 (inclusive).

  • Senior: Age is 65 or older.

Question 1: Leap Year Checker

Write a C program that asks the user to enter a year and then checks if the year is a leap year or not. A year is a leap year if:

  • It is divisible by 4, and

    • It is not divisible by 100, unless

    • It is also divisible by 400.

Example:
If the user enters 2024, the program should print: "2024 is a leap year."

Question 2: Grade Calculator

Write a C program that asks the user to enter their marks for a subject and then prints out their grade based on the following criteria:

  • A: Marks are 90 or above.

  • B: Marks are between 80 and 89.

  • C: Marks are between 70 and 79.

  • D: Marks are between 60 and 69.

  • F: Marks are below 60.

Example:
If the user enters 85, the program should print: "You got a B."

Question 3: Even or Odd

Write a C program that asks the user to enter a number and checks whether it is even or odd.

Example:
If the user enters 7, the program should print: "7 is odd."

Question 4: Temperature Converter

Write a C program that converts a temperature given in Fahrenheit to Celsius and then categorizes it as "Cold," "Warm," or "Hot" based on the Celsius temperature:

  • Cold: Less than 10°C.

  • Warm: Between 10°C and 25°C (inclusive).

  • Hot: Greater than 25°C.

Formula to Convert Fahrenheit to Celsius:
Celsius=59×(Fahrenheit−32)text{Celsius} = frac{5}{9} times (text{Fahrenheit} - 32)Celsius=95​×(Fahrenheit−32)

Example:
If the user enters 77 Fahrenheit, the program should print: "25°C, Warm."

Question 5: Basic Calculator

Write a C program that performs basic arithmetic operations (addition, subtraction, multiplication, and division). The program should ask the user to enter two numbers and an operator (+, -, *, /), and then it should display the result of the operation.

Example:
If the user enters 5, 3, and , the program should print: "5 3 = 15."

Question 6: Vowel or Consonant

Write a C program that asks the user to enter a single character and checks whether it is a vowel (a, e, i, o, u) or a consonant.

Example:
If the user enters a, the program should print: "a is a vowel."

Question 7: Number Classification

Write a C program that asks the user to enter an integer and then classifies it as:

  • Positive and even

  • Positive and odd

  • Negative and even

  • Negative and odd

  • Zero

Example:
If the user enters -4, the program should print: "-4 is negative and even."

Question 8: Triangle Type Checker

Write a C program that asks the user to enter the lengths of three sides of a triangle and then determines what type of triangle it is:

  • Equilateral: All three sides are equal.

  • Isosceles: Two sides are equal.

  • Scalene: No sides are equal.

  • Invalid: The sides do not form a valid triangle (hint: check the triangle inequality theorem).

Example:
If the user enters 3, 4, and 5, the program should print: "The triangle is Scalene."

Question 9: Simple Interest Calculator

Write a C program that asks the user to enter the principal amount, rate of interest, and time in years. Then calculate the simple interest and categorize the result:

  • Low Interest: Simple interest is less than 1000.

  • Moderate Interest: Simple interest is between 1000 and 5000.

  • High Interest: Simple interest is above 5000.

Formula:
Simple Interest=P×R×T100text{Simple Interest} = frac{P times R times T}{100}Simple Interest=100P×R×T​

Example:
If the user enters 5000 (Principal), 5 (Rate), and 2 (Time), the program should print: "The interest is 500, Low Interest."

Question 10: Password Strength Checker

Write a C program that asks the user to enter a password and then checks its strength based on the following criteria:

  • Strong: The password is at least 8 characters long, contains at least one uppercase letter, one lowercase letter, one digit, and one special character.

  • Moderate: The password is at least 6 characters long and meets two of the criteria for a strong password.

  • Weak: The password does not meet the criteria for a moderate or strong password.

Example:
If the user enters P@ssw0rd, the program should print: "The password is Strong."

Question 11: BMI Calculator

Write a C program that calculates a user's Body Mass Index (BMI) and then classifies it as:

  • Underweight: BMI < 18.5

  • Normal weight: 18.5 ≤ BMI < 24.9

  • Overweight: 25 ≤ BMI < 29.9

  • Obesity: BMI ≥ 30

Formula:
BMI=weight in kg(height in meters)2text{BMI} = frac{text{weight in kg}}{text{(height in meters)}^2}BMI=(height in meters)2weight in kg​

Example:
If the user enters 70 kg (weight) and 1.75 meters (height), the program should print: "Your BMI is 22.86, Normal weight."

Question 12: Shopping Discount Calculator

Write a C program that calculates the final price after applying a discount based on the total purchase amount:

  • No Discount: Purchase amount less than $100.

  • 10% Discount: Purchase amount between $100 and $500.

  • 20% Discount: Purchase amount between $500 and $1000.

  • 30% Discount: Purchase amount greater than $1000.

Example:
If the user enters 750, the program should print: "Final price after discount: $600."

Question 13: Time of Day Greeting

Write a C program that asks the user to enter the current hour in 24-hour format (0-23) and then prints a greeting:

  • "Good Morning": 5 ≤ hour < 12

  • "Good Afternoon": 12 ≤ hour < 17

  • "Good Evening": 17 ≤ hour < 21

  • "Good Night": 21 ≤ hour < 24 or 0 ≤ hour < 5

Example:
If the user enters 14, the program should print: "Good Afternoon."

Question 14: Days in a Month

Write a C program that asks the user to enter a month (1-12) and a year, then determines how many days are in that month. Account for leap years in February.

Example:
If the user enters 2 and 2024, the program should print: "February 2024 has 29 days."

Question 15: ATM Withdrawal

Write a C program that simulates an ATM withdrawal. The user should enter the amount they wish to withdraw. The program should check if the amount is a multiple of 100 and does not exceed the account balance (assume a balance of $5000). Then, it should print the remaining balance after the withdrawal or an error message if the conditions are not met.

Example:
If the user enters 1500, the program should print: "Transaction successful. Remaining balance: $3500."

LOOPS

  1. Write a C program to print all natural numbers from 1 to n. – using while loop

  2. Write a C program to print all natural numbers in reverse (from n to 1). – using while loop

  3. Write a C program to print all alphabets from a to z. – using while loop

  4. Write a C program to print all even numbers between 1 to 100. – using while loop

  5. Write a C program to print all odd number between 1 to 100.

  6. Write a C program to find sum of all natural numbers between 1 to n.

  7. Write a C program to find sum of all even numbers between 1 to n.

  8. Write a C program to find sum of all odd numbers between 1 to n.

  9. Write a C program to print multiplication table of any number.

  10. Write a C program to count number of digits in a number.

  11. Write a C program to find first and last digit of a number.

  12. Write a C program to find sum of first and last digit of a number.

  13. Write a C program to swap first and last digits of a number.

  14. Write a C program to calculate sum of digits of a number.

  15. Write a C program to calculate product of digits of a number.

  16. Write a C program to enter a number and print its reverse.

  17. Write a C program to check whether a number is palindrome or not.

  18. Write a C program to find frequency of each digit in a given integer.

  19. Write a C program to enter a number and print it in words.

  20. Write a C program to print all ASCII character with their values.

  21. Write a C program to find power of a number using for loop.

  22. Write a C program to find all factors of a number.

  23. Write a C program to calculate factorial of a number.

  24. Write a C program to find HCF (GCD) of two numbers.

  25. Write a C program to find LCM of two numbers.

  26. Write a C program to check whether a number is Prime number or not.

  27. Write a C program to print all Prime numbers between 1 to n.

  28. Write a C program to find sum of all prime numbers between 1 to n.

  29. Write a C program to find all prime factors of a number.

  30. Write a C program to check whether a number is Armstrong number or not.

  31. Write a C program to print all Armstrong numbers between 1 to n.

  32. Write a C program to check whether a number is Perfect number or not.

  33. Write a C program to print all Perfect numbers between 1 to n.

  34. Write a C program to check whether a number is Strong number or not.

  35. Write a C program to print all Strong numbers between 1 to n.

  36. Write a C program to print Fibonacci series up to n terms.

  37. Write a C program to find one’s complement of a binary number.

  38. Write a C program to find two’s complement of a binary number.

  39. Write a C program to convert Binary to Octal number system.

  40. Write a C program to convert Binary to Decimal number system.

  41. Write a C program to convert Binary to Hexadecimal number system.

  42. Write a C program to convert Octal to Binary number system.

  43. Write a C program to convert Octal to Decimal number system.

  44. Write a C program to convert Octal to Hexadecimal number system.

  45. Write a C program to convert Decimal to Binary number system.

  46. Write a C program to convert Decimal to Octal number system.

  47. Write a C program to convert Decimal to Hexadecimal number system.

  48. Write a C program to convert Hexadecimal to Binary number system.

  49. Write a C program to convert Hexadecimal to Octal number system.

  50. Write a C program to convert Hexadecimal to Decimal number system.

  51. Write a C program to print Pascal triangle upto n rows.

  52. Star pattern programs – Write a C program to print the given star patterns.

  53. Number pattern programs – Write a C program to print the given number patterns.

Array Questions

  1. Write a C program that takes an array of integers from the user, finds the maximum number in the array, and prints it.

  2. Write a C program to find the sum of all elements in an array entered by the user.

  3. Write a C program that calculates and prints the average of numbers stored in an array.

  4. Write a C program to find the smallest element in an array entered by the user.

  5. Write a C program to reverse the elements of an array entered by the user.

  6. Write a C program to count the number of even and odd numbers in an array entered by the user.