C Programming Solutions
C Programming Solutions
A)
#include <stdio.h>
#include <conio.h>
void main() {
int number;
// Input a number
printf("Enter a number: ");
scanf("%d", &number);
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;
// 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);
}
Input 1:
Enter a number: 35
Output 1:
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;
#include <stdio.h>
#include <conio.h>
void main() {
int number;
// Input a number
printf("Enter a number: ");
scanf("%d", &number);
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:
#include <stdio.h>
#include <conio.h>
void main() {
int i, sum = 0;
#include <stdio.h>
#include <conio.h>
void main() {
int i = 1, sum = 0;
#include <stdio.h>
#include <conio.h>
void main() {
int digit;
Valid Input:
Input:
Five
Invalid Input:
Input:
#include <stdio.h>
#include <conio.h>
void main() {
int day;
Valid Input:
Input:
Tuesday
Invalid Input:
Input:
#include <stdio.h>
#include <conio.h>
void main() {
int productNo, quantity;
char productName[50];
float rate, cost, discount = 0.0, finalCost;
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
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];
// 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);
Case 1: Distinction
Input:
Output:
Student Details:
Roll Number: 1
Name: John
Marks in Subject 1: 85
Marks in Subject 2: 80
Percentage: 82.50%
Grade: Distinction
Input:
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:
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;
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:
#include <stdio.h>
#include <conio.h>
void main() {
int arr[10], sum = 0, i;
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
}
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:
#include <stdio.h>
#include <conio.h>
void main() {
int matrix[3][3], transpose[3][3];
int i, j;
Input:
Output:
Original Matrix: 1 2 3
4 5 6
7 8 9
#include <stdio.h>
#include <string.h>
void main() {
char str[100];
int length;
Sample Input/Output:
Input 1:
Output 1:
Input 2:
Output 2:
Input 3:
Output 3:
#include <stdio.h>
#include <string.h>
void main() {
char str1[100], str2[100];
int result;
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:
Output 1:
Input 2:
Output 2:
#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
#include <stdio.h>
#include <conio.h>
void main() {
int num1, num2, num3, sum;
Sample Input/Output:
Input:
Output:
#include <stdio.h>
#include <conio.h>
void main() {
struct Book b; // Declare a variable of type Book
Sample Input/Output:
Input:
Output: