The Ultimate C Programming Practice Guide: 100+ Exercises
(Comprehensive, "ultimate guide" and "exercises")
Q11. AIM:
Write a program in C language to perform arithmetic operation like +, - , X, / and % and display the result.
(i) By declaring int type variables.
(ii) By declaring float type variables.
PROGRAM By declaring int type variables
solution :
/*pgm for performing arithmetic operations using int type*/
#include<stdio.h>
void main() {
int num1,num2; num1=150;
num2=100;
printf("Sum is %d\n",num1+num2);
printf("Difference %d\n",num1-num2);
printf("Product is %d\n",num1*num2);
printf("Quotient is %d\n",num1/num2);
printf("Remainder is %d\n",num1%num2);
}
CLICK TO GET SOURCE CODE OF HELLO WORLD
AIM
Q12.: Write a program in C language to calculate the area of the circle. Formula for calculating area of a circle A is A=πr2 where r is the radius and π is a constant.
solution:-
/*pgm for calculating area of circle*/
#include <stdio.h>
#define PI 3.14
void main() {
floatr,area;
printf("Enter the radius of circle\n");
scanf("%f",&r);
area=PI*r*r;
printf("Area of the circle is %f\n",area);
}
AIM:
Q13. Write a program in C language to check whether an entered alphabet is vowel, consonant, digit or special character.
PROGRAM
/*pgm for checking characters*/
#include<stdio.h>
#include<ctype.h>
void main()
{
char a;
printf("Enter a character\n");
scanf("%c",&a);
if(isalpha(a))
{ if(a=='A'||a=='E'||a=='I'||a=='O'||a=='U'||a=='a'||a=='e'||a=='i'||a=='o'||a=='u')
printf("Entered character is a vowel\n");
else
printf("Entered character is a consonant\n");
} else
if(isdigit(a))
printf("Entered character is a digit\n");
else
printf("Entered character is a special character\n");
}
AIM:
Q14.Write a program in C language to print counting numbers from 1 to 100 using ‘for loop’
. PROGRAM
/*pgm for printing counting numbers using for loop*/
#include<stdio.h>
void main() {
int i;
for(i=1;i<=100;i++)
{
printf("%d\t",i);
}
}
Q15 . Write a program in C language to find the sum of digits of an input number less than 99999
SOLUTION:-
#include <stdio.h>
int main() {
int number, remainder, sum = 0;
printf("Enter a number less than 99999: ");
scanf("%d", &number);
// Check if the number is within the valid range (less than 99999)
if (number < 0 || number >= 99999) {
printf("Invalid input: Number must be less than 99999.\n");
return 1; // Indicate error (non-zero return value)
}
// Efficient loop to extract digits and add them
while (number > 0) {
remainder = number % 10;
sum += remainder;
number /= 10;
}
printf("The sum of digits of %d is %d\n", number, sum); // Print the original number along with the sum
return 0; // Indicate successful execution
}
IF YOU NOT SOLVE FROM STARTING CLICK HERE
Explanation:
- #include <stdio.h>: Includes standard input/output library.
- int main(): The main function where the program starts.
- Variables:
number
: Stores the user's input number.remainder
: Stores the remainder after division by 10 (extracted digit).sum
: Accumulates the sum of digits.
- Input: Prompts the user to enter a number and reads it using
scanf
. - Input Validation: Checks if the entered number is within the valid range (less than 99999) using an
if
statement. Prints an error message and exits the program (returns 1) if the number is invalid. - Loop for Digit Extraction:
while (number > 0)
: The loop continues as long asnumber
is greater than 0, ensuring all digits are processed.remainder = number % 10
: Calculates the remainder after dividingnumber
by 10, effectively extracting the rightmost digit.sum += remainder
: Adds the extracted digit to thesum
variable.number /= 10
: Dividesnumber
by 10 to remove the processed rightmost digit.
- Output: Prints the original number and the calculated sum of its digits.
- Return Values:
return 0;
: Indicates successful program termination.return 1;
: Used in the validation section to signal an error.
This program incorporates input validation to ensure the user enters a valid number within the specified range. It also uses a more efficient loop to avoid unnecessary calculations.
Q16. Write a program in C language to display a menu as given below and Execute arithmetic operations. Use switch case
SOLUTION:
#include <stdio.h>
int main() {
int num1, num2, choice;
printf("** Arithmetic Operations Menu **\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Sum: %d\n", num1 + num2);
break;
case 2:
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Difference: %d\n", num1 - num2);
break;
case 3:
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Product: %d\n", num1 * num2);
break;
case 4:
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
// Handle division by zero
if (num2 == 0) {
printf("Error: Division by zero!\n");
} else {
printf("Division: %.2f\n", (float)num1 / num2);
}
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
EXPANATION:-
Header and Variables:
#include <stdio.h>
: Includes standard input/output library.int main()
: The main function where the program starts.num1
,num2
: Integers to store user-entered numbers.choice
: Integer to store the user's menu choice.
-
Menu Display: Prints the menu options for addition, subtraction, multiplication, division, and exit.
-
User Input: Prompts the user to enter their choice using
scanf
. -
Switch Case:
switch (choice)
: Evaluates the user's choice.case 1
,case 2
,case 3
: For addition, subtraction, and multiplication, respectively, it prompts for numbers, performs the operation, and prints the result.case 4
: For division, it checks for division by zero and prints an error message if necessary. Otherwise, it performs the division as a float to ensure decimal results and prints the answer.case 5
: Exits the program with a message.default
: If the choice is invalid, it prints an error message.
-
Return Statement:
return 0;
indicates successful program execution.
This program improves upon the previous examples by:
- Handling division by zero to prevent runtime errors.
- Casting the numerator to a float when dividing for a decimal result.
- Providing a clear exit message
Q17. Create an array and store data into the array and print the same.
solution:
#include <stdio.h>
int main() {
int numbers[5]; // Array declaration with size 5 (can hold 5 integers)
// Input loop to get values from the user
for (int i = 0; i < 5; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &numbers[i]);
}
// Print the elements of the array
printf("Elements of the array: \n");
for (int i = 0; i < 5; i++) {
printf("%d ", numbers[i]);
}
printf("\n");
return 0;
}
Explanation:
- #include <stdio.h>: Includes the standard input/output library.
- int main(): The main function where the program starts.
- Array Declaration:
int numbers[5];
: Declares an array namednumbers
of typeint
(integer) with a size of 5. This means it can store 5 integer values.
- Input Loop:
for (int i = 0; i < 5; i++)
: Afor
loop iterates 5 times (from i=0 to i=4). This loop is used to get data from the user for each element of the array.printf("Enter element %d: ", i + 1);
: Prompts the user to enter a value for the current element (i + 1
for clearer numbering starting from 1).scanf("%d", &numbers[i]);
: Reads the user's input usingscanf
and stores it in thenumbers
array at indexi
.
- Output Loop:
printf("Elements of the array: \n");
: Prints a message indicating the array elements.for (int i = 0; i < 5; i++)
: Anotherfor
loop iterates 5 times to print each element of the array.printf("%d ", numbers[i]);
: Prints the current element (numbers[i]
) followed by a space for better formatting.
printf("\n");
: Prints a newline character after all elements are printed.
- Return Statement:
return 0;
indicates successful program execution.
This program demonstrates how to create an array, take user input for each element, and print the stored values.