C Programming
1. Write a C program to input a number and
a) Check if it is even or odd
b) Check if it is divisible by 5 or 7
A)
#include <stdio.h>
#include <conio.h>
void main() {
int number;
clrscr(); // Clear the screen
// Input a number
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is even or odd
if (number % 2 == 0) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);
}
getch(); // Wait for a key press
}
Sample Input/Output
Input 1:
Enter a number: 8
Output 1:
8 is even.
Input 2:
Enter a number: 15
Output 2:
15 is odd.
B)
#include <stdio.h>
#include <conio.h>
void main() {
int number;
clrscr(); // Clear the screen
// Input a number
printf("Enter a number: ");
scanf("%d", &number);
// Check divisibility by 5 or 7
if (number % 5 == 0 && number % 7 == 0) {
printf("%d is divisible by both 5 and 7.\n",
number);
} else if (number % 5 == 0) {
printf("%d is divisible by 5.\n", number);
} else if (number % 7 == 0) {
printf("%d is divisible by 7.\n", number);
} else {
printf("%d is not divisible by 5 or 7.\n", number);
}
getch(); // Wait for a key press
}
Sample Input/Output
Input 1:
Enter a number: 35
Output 1:
35 is divisible by both 5 and 7.
Input 2:
Enter a number: 10
Output 2:
10 is divisible by 5.
Input 3:
Enter a number: 14
Output 3:
14 is divisible by 7.
Input 4:
Enter a number: 13
Output 4:
13 is not divisible by 5 or 7.
2. Write C programs to print
a) even numbers from 1 to 10.
b) check if the given number is positive, negative or zero
A)
#include <stdio.h>
#include <conio.h>
void main() {
int i;
clrscr(); // Clear the screen
printf("Even numbers from 1 to 10 are:\n");
for (i = 1; i <= 10; i++) {
if (i % 2 == 0) {
printf("%d ", i);
}
}
getch(); // Wait for a key press
}
Example Output
Even numbers from 1 to 10 are : 2 4 6 8 10
B)
#include <stdio.h>
#include <conio.h>
void main() {
int number;
clrscr(); // Clear the screen
// Input a number
printf("Enter a number: ");
scanf("%d", &number);
// Check if the number is positive, negative, or zero
if (number > 0) {
printf("%d is positive.\n", number);
} else if (number < 0) {
printf("%d is negative.\n", number);
} else {
printf("The number is zero.\n");
}
getch(); // Wait for a key press
}
Sample Input/Output
Input 1:
Enter a number: 10
Output 1:
10 is positive.
Input 2:
Enter a number: -5
Output 2:
-5 is negative.
Input 3:
Enter a number: 0
Output 3:
The number is zero.
3. Write a C program to get the sum of numbers from 1 to 50.
Using For Loop:
#include <stdio.h>
#include <conio.h>
void main() {
int i, sum = 0;
clrscr(); // Clear the screen
// Calculate the sum of numbers from 1 to 50
for (i = 1; i <= 50; i++) {
sum += i;
}
// Display the result
printf("The sum of numbers from 1 to 50 is: %d\n", sum);
getch(); // Wait for a key press
}
Using While Loop
#include <stdio.h>
#include <conio.h>
void main() {
int i = 1, sum = 0;
clrscr(); // Clear the screen
// Calculate the sum of numbers from 1 to 50 using while loop
while (i <= 50) {
sum += i; // Add the current number to sum
i++; // Increment the counter
}
// Display the result
printf("The sum of numbers from 1 to 50 is: %d\n", sum);
getch(); // Wait for a key press
}
Output:
The sum of numbers from 1 to 50 is: 1275
4. Write a C program to input a single digit and print it in word using
switch..case
#include <stdio.h>
#include <conio.h>
void main() {
int digit;
clrscr(); // Clear the screen
// Input a single digit
printf("Enter a single digit (0-9): ");
scanf("%d", &digit);
// Use switch-case to print the digit in words
switch (digit) {
case 0:
printf("Zero\n");
break;
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
case 4:
printf("Four\n");
break;
case 5:
printf("Five\n");
break;
case 6:
printf("Six\n");
break;
case 7:
printf("Seven\n");
break;
case 8:
printf("Eight\n");
break;
case 9:
printf("Nine\n");
break;
default:
printf("Invalid input! Please enter a digit between 0 and
9.\n");
}
getch(); // Wait for a key press
}
Example Outputs:
Valid Input:
Input:
Enter a single digit (0-9): 5
Output:
Five
Invalid Input:
Input:
Enter a single digit (0-9): 12
Output:
Invalid input! Please enter a digit between 0 and 9.
5. Write a C program to input day of week from 1 to 7 and print it in
words. Eg if the number entered is 2, it should display “Tuesday”
using switch...case
#include <stdio.h>
#include <conio.h>
void main() {
int day;
clrscr(); // Clear the screen
// Input the day of the week
printf("Enter the day of the week (1-7): ");
scanf("%d", &day);
// Use switch-case to print the day in words
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid input! Please enter a number between 1
and 7.\n");
}
getch(); // Wait for a key press
}
Example Outputs:
Valid Input:
Input:
Enter the day of the week (1-7): 2
Output:
Tuesday
Invalid Input:
Input:
Enter the day of the week (1-7): 9
Output:
Invalid input! Please enter a number between 1 and 7.
6. Write a C program to input product no, product name, rate and
quantity. Calculate the cost and if cost is more than 1000 give a
discount of 10% and print the final cost with other details.
#include <stdio.h>
#include <conio.h>
void main() {
int productNo, quantity;
char productName[50];
float rate, cost, discount = 0.0, finalCost;
clrscr(); // Clear the screen
// Input product details
printf("Enter product number: ");
scanf("%d", &productNo);
printf("Enter product name: ");
scanf("%s", productName);
printf("Enter rate of the product: ");
scanf("%f", &rate);
printf("Enter quantity: ");
scanf("%d", &quantity);
// Calculate the cost
cost = rate * quantity;
// Apply discount if cost exceeds 1000
if (cost > 1000) {
discount = cost * 0.10; // 10% discount
}
finalCost = cost - discount;
// Print the details
printf("\nProduct Details:\n");
printf("Product Number: %d\n", productNo);
printf("Product Name: %s\n", productName);
printf("Rate: %.2f\n", rate);
printf("Quantity: %d\n", quantity);
printf("Total Cost: %.2f\n", cost);
printf("Discount: %.2f\n", discount);
printf("Final Cost: %.2f\n", finalCost);
getch(); // Wait for a key press
}
Example Output:
Case 1: Cost exceeds 1000
Input:
Enter product number: 101
Enter product name: Laptop
Enter rate of the product: 600
Enter quantity: 2
Output:
Product Details:
Product Number: 101
Product Name: Laptop
Rate: 600.00
Quantity: 2
Total Cost: 1200.00
Discount: 120.00
Final Cost: 1080.00
Case 2: Cost does not exceed 1000
Input:
Enter product number: 202
Enter product name: Mouse
Enter rate of the product: 200
Enter quantity: 4
Output:
Product Details:
Product Number: 202
Product Name: Mouse
Rate: 200.00
Quantity: 4
Total Cost: 800.00
Discount: 0.00
Final Cost: 800.00
7. Write a C program to input rollno, name and marks of two subjects.
Calculate and display percentage and grade. Grades can be given as if
percentage > 75 grade is “Distinction”, if percentage > 60 and less
than 75 is “First” , percentage >50 and less than 60 is “second”
otherwise “Fail”.
#include <stdio.h>
#include <conio.h>
void main() {
int rollNo, marks1, marks2, totalMarks = 200;
char name[50];
float percentage;
char grade[15];
clrscr(); // Clear the screen
// Input student details
printf("Enter Roll Number: ");
scanf("%d", &rollNo);
printf("Enter Name: ");
scanf("%s", name);
printf("Enter marks of Subject 1: ");
scanf("%d", &marks1);
printf("Enter marks of Subject 2: ");
scanf("%d", &marks2);
// Calculate percentage
percentage = ((marks1 + marks2) / (float)totalMarks) * 100;
// Determine grade based on percentage
if (percentage > 75) {
strcpy(grade, "Distinction");
} else if (percentage > 60) {
strcpy(grade, "First");
} else if (percentage > 50) {
strcpy(grade, "Second");
} else {
strcpy(grade, "Fail");
}
// Display results
printf("\nStudent Details:\n");
printf("Roll Number: %d\n", rollNo);
printf("Name: %s\n", name);
printf("Marks in Subject 1: %d\n", marks1);
printf("Marks in Subject 2: %d\n", marks2);
printf("Percentage: %.2f%%\n", percentage);
printf("Grade: %s\n", grade);
getch(); // Wait for a key press
}
Example Outputs:
Case 1: Distinction
Input:
Enter Roll Number: 1
Enter Name: John
Enter marks of Subject 1: 85
Enter marks of Subject 2: 80
Output:
Student Details:
Roll Number: 1
Name: John
Marks in Subject 1: 85
Marks in Subject 2: 80
Percentage: 82.50%
Grade: Distinction
Case 2: First Class
Input:
Enter Roll Number: 2
Enter Name: Jane
Enter marks of Subject 1: 70
Enter marks of Subject 2: 65
Output:
Student Details:
Roll Number: 2
Name: Jane
Marks in Subject 1: 70
Marks in Subject 2: 65
Percentage: 67.50%
Grade: First
Case 3: Fail
Input:
Enter Roll Number: 3
Enter Name: Max
Enter marks of Subject 1: 40
Enter marks of Subject 2: 35
Output:
Student Details:
Roll Number: 3
Name: Max
Marks in Subject 1: 40
Marks in Subject 2: 35
Percentage: 37.50
%Grade: Fail
8. Write a C program to add 10 integers to an array and display odd
numbers among them .
#include <stdio.h>
#include <conio.h>
void main() {
int arr[10], i;
clrscr(); // Clear the screen
// Input 10 integers into the array
printf("Enter 10 integers:\n");
for (i = 0; i < 10; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Display the odd numbers
printf("\nOdd numbers in the array are:\n");
for (i = 0; i < 10; i++) {
if (arr[i] % 2 != 0) {
printf("%d ", arr[i]);
}
}
getch(); // Wait for a key press
}
Example Output:
Input:
Enter 10 integers:
Enter element 1: 12
Enter element 2: 5
Enter element 3: 8
Enter element 4: 9
Enter element 5: 15
Enter element 6: 20
Enter element 7: 3
Enter element 8: 6
Enter element 9: 10
Enter element 10: 7
Output:
Odd numbers in the array are: 5 9 15 3 7
9. Write a C program to input 10 integers to an array and display sum
of all the numbers
#include <stdio.h>
#include <conio.h>
void main() {
int arr[10], sum = 0, i;
clrscr(); // Clear the screen
printf("Enter 10 integers:\n");
for (i = 0; i < 10; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &arr[i]); // Input each integer
sum += arr[i]; // Add the integer to the sum
}
printf("\nThe sum of the entered numbers is: %d", sum);
getch(); // Wait for user input before closing
}
Sample Input/Output
Input:
Enter 10 integers:
Enter number 1: 5
Enter number 2: 10
Enter number 3: 15
Enter number 4: 20
Enter number 5: 25
Enter number 6: 30
Enter number 7: 35
Enter number 8: 40
Enter number 9: 45
Enter number 10: 50
Output:
The sum of the entered numbers is: 275
10. Write a C program to print transpose of a 3x3 matrix using 2D
array of integers
#include <stdio.h>
#include <conio.h>
void main() {
int matrix[3][3], transpose[3][3];
int i, j;
clrscr(); // Clear the screen
// Input the matrix
printf("Enter the elements of a 3x3 matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("Element [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
// Compute the transpose
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
transpose[j][i] = matrix[i][j];
}
}
// Print the original matrix
printf("\nOriginal Matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Print the transpose of the matrix
printf("\nTranspose of the Matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d ", transpose[i][j]);
}
printf("\n");
}
getch(); // Wait for user input before exiting
}
Sample Input/Output:
Input:
Enter the elements of a 3x3 matrix:
Element [1][1]: 1
Element [1][2]: 2
Element [1][3]: 3
Element [2][1]: 4
Element [2][2]: 5
Element [2][3]: 6
Element [3][1]: 7
Element [3][2]: 8
Element [3][3]: 9
Output:
Original Matrix: 1 2 3
4 5 6
7 8 9
Transpose of the Matrix: 1 4 7
2 5 8
3 6 9
11. Write a C program to input a string and check if it has number of
characters more than 6 or less than 6 or equal to 6.
#include <stdio.h>
#include <string.h>
void main() {
char str[100];
int length;
clrscr(); // Clear the screen
// Input the string
printf("Enter a string: ");
gets(str); // Use gets() for input (suitable for Turbo C)
// Calculate the length of the string
length = strlen(str);
// Check the length
if (length > 6) {
printf("The string has more than 6 characters.\n");
} else if (length < 6) {
printf("The string has less than 6 characters.\n");
} else {
printf("The string has exactly 6 characters.\n");
}
getch(); // Wait for user input before exiting
}
Sample Input/Output:
Input 1:
Enter a string: Hello
Output 1:
The string has less than 6 characters.
Input 2:
Enter a string: Welcome
Output 2:
The string has exactly 6 characters.
Input 3:
Enter a string: Programming
Output 3:
The string has more than 6 characters.
12. Write a C program to input two strings and check if they are equal
or not.
#include <stdio.h>
#include <string.h>
void main() {
char str1[100], str2[100];
int result;
clrscr(); // Clear the screen
// Input the first string
printf("Enter the first string: ");
gets(str1); // Use gets() for string input (suitable for Turbo C)
// Input the second string
printf("Enter the second string: ");
gets(str2);
// Compare the strings
result = strcmp(str1, str2);
if (result == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
getch(); // Wait for user input before exiting
}
Sample Input/Output:
Input 1:
Enter the first string: Hello
Enter the second string: Hello
Output 1:
The strings are equal.
Input 2:
Enter the first string: Welcome
Enter the second string: Goodbye
Output 2:
The strings are not equal.
13. Write a C program to print first 5 numbers of Fibonacci series.
#include <stdio.h>
#include <conio.h>
void main() {
int n1 = 0, n2 = 1, n3, count = 2; // count starts at 2 because n1
and n2 are already printed
clrscr(); // Clear the screen
printf("The first 5 numbers of the Fibonacci series are:\n");
printf("%d %d ", n1, n2); // Print the first two numbers
while (count < 5) {
n3 = n1 + n2; // Calculate the next number in the series
printf("%d ", n3); // Print the next number
n1 = n2; // Update n1 to the next number
n2 = n3; // Update n2 to the next number
count++; // Increment the count
}
getch(); // Wait for user input before exiting
}
Sample Output:
The first 5 numbers of the Fibonacci series are:0 1 1 2 3
14. Write a C program to calculate sum of factorial of 3 numbers
using user defined function for finding factorial.
#include <stdio.h>
#include <conio.h>
// Function to calculate factorial
int factorial(int n) {
int fact = 1, i;
for (i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
void main() {
int num1, num2, num3, sum;
clrscr(); // Clear the screen
// Input three numbers
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
// Calculate the sum of factorials
sum = factorial(num1) + factorial(num2) + factorial(num3);
// Print the result
printf("\nThe sum of factorials of %d, %d, and %d is: %d",
num1, num2, num3, sum);
getch(); // Wait for user input before exiting
}
Sample Input/Output:
Input:
Enter the first number: 3
Enter the second number: 4
Enter the third number: 5
Output:
The sum of factorials of 3, 4, and 5 is: 150
15. Write a C program to define a structure Book with members as
bookid, bookname, author and price. Accept and display data for 1
book.
#include <stdio.h>
#include <conio.h>
// Define the structure
struct Book {
int bookid;
char bookname[50];
char author[50];
float price;
};
void main() {
struct Book b; // Declare a variable of type Book
clrscr(); // Clear the screen
// Accept data for the book
printf("Enter Book ID: ");
scanf("%d", &b.bookid);
fflush(stdin); // Clear input buffer
printf("Enter Book Name: ");
gets(b.bookname); // Read string with spaces
printf("Enter Author Name: ");
gets(b.author);
printf("Enter Book Price: ");
scanf("%f", &b.price);
// Display data for the book
printf("\n--- Book Details ---\n");
printf("Book ID: %d\n", b.bookid);
printf("Book Name: %s\n", b.bookname);
printf("Author Name: %s\n", b.author);
printf("Price: %.2f\n", b.price);
getch(); // Wait for user input before exiting
}
Sample Input/Output:
Input:
Enter Book ID: 101
Enter Book Name: The C Programming Language
Enter Author Name: Brian Kernighan
Enter Book Price: 499.99
Output:
--- Book Details ---
Book ID: 101
Book Name: The C Programming Language
Author Name: Brian Kernighan
Price: 499.99