0% found this document useful (0 votes)
10 views18 pages

12th Codeing

Uploaded by

njv2504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views18 pages

12th Codeing

Uploaded by

njv2504
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Practical 01

#include <stdio.h>

int main() {

char character = 'A';

int integerValue = 42;

float floatValue = 3.14;

printf("Line 1: Character: %c\n", character);

printf("Line 2: Integer Value: %d\n", integerValue);

printf("Line 3: Floating Point Value: %.2f\n", floatValue);

printf("Line 4: Combined: %c, %d, %.2f\n", character, integerValue, floatValue);

return 0;

}
Practical 02
#include <stdio.h>

int main() {
char name[50];
int age;
float height;
char gender;

// Prompt user for input


printf("Enter your name: ");
scanf("%49s", name); // Read up to 49 characters to prevent overflow

printf("Enter your age: ");


scanf("%d", &age);

printf("Enter your height (in meters): ");


scanf("%f", &height);

printf("Enter your gender (M/F): ");


scanf(" %c", &gender); // Notice the space before %c to consume any leftover newline

// Print the collected data


printf("\n--- Student Information ---\n");
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Height: %.2f meters\n", height);
printf("Gender: %c\n", gender);

return 0;
}
Practical 03
#include <stdio.h>
#include <math.h>

int main() {
float base, height, radius;
float area, volume;

// Input for triangle


printf("Enter the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);

// Calculate the area of the triangle


area = 0.5 * base * height;

// Input for sphere


printf("Enter the radius of the sphere: ");
scanf("%f", &radius);

// Calculate the volume of the sphere


volume = (4.0 / 3.0) * M_PI * pow(radius, 3);

// Print the results


printf("\nArea of the triangle: %.2f\n", area);
printf("Volume of the sphere: %.2f\n", volume);

// Arrange the results in ascending order


if (area < volume) {
printf("Ascending order: %.2f (Area of Triangle), %.2f (Volume of Sphere)\n", area, volume);
} else {
printf("Ascending order: %.2f (Volume of Sphere), %.2f (Area of Triangle)\n", volume, area);
}

return 0;
}
Practical 04
#include <stdio.h>

int main() {

int i, j;

// Generate multiplication tables from 2 to 20

for (i = 2; i <= 20; i++) {

printf("Multiplication Table of %d:\n", i);

for (j = 1; j <= 10; j++) {

printf("%d x %d = %d\n", i, j, i * j);

printf("\n"); // Print a new line for better readability

return 0;

}
Practical 05 (a)
#include <stdio.h>

int main() {

int numSubjects, marks;

int totalMarks = 0;

int count = 0;

char continueInput;

do {

printf("Enter the number of subjects: ");

scanf("%d", &numSubjects);

// Reset totalMarks and count for each new input of subjects

totalMarks = 0;

count = 0;

int i = 1; // Initialize subject counter

while (i <= numSubjects) {

printf("Enter marks for subject %d: ", i);

scanf("%d", &marks);

// Ensure the marks are non-negative

if (marks < 0) {

printf("Marks cannot be negative. Please enter a valid mark.\n");

continue; // Skip the rest of the loop and ask for marks again

totalMarks += marks;

count++;
i++; // Increment subject counter

// Calculate average

if (count > 0) {

float average = (float)totalMarks / count;

printf("Average marks: %.2f\n", average);

} else {

printf("No valid marks entered.\n");

// Ask if the user wants to continue

printf("Do you want to calculate average for another student? (y/n): ");

scanf(" %c", &continueInput); // Space before %c to consume any newline character

} while (continueInput == 'y' || continueInput == 'Y');

printf("Thank you!\n");

return 0;

}
Practical 05 (b)
#include <stdio.h>

int main() {

int numSubjects;

char continueInput;

do {

printf("Enter the number of subjects: ");

scanf("%d", &numSubjects);

// Initialize totalMarks and count

int totalMarks = 0;

int count = 0;

for (int i = 1; i <= numSubjects; i++) {

int marks;

int validInput = 0; // To track valid marks entry

while (validInput == 0) {

printf("Enter marks for subject %d: ", i);

scanf("%d", &marks);

// Ensure the marks are non-negative

if (marks < 0) {

printf("Marks cannot be negative. Please enter a valid mark.\n");

} else {

totalMarks += marks;

validInput = 1; // Mark entry is valid

count++; // Increment count of valid marks


}

// Calculate average

if (count > 0) {

float average = (float)totalMarks / count;

printf("Average marks: %.2f\n", average);

} else {

printf("No valid marks entered.\n");

// Ask if the user wants to continue

printf("Do you want to calculate average for another student? (y/n): ");

scanf(" %c", &continueInput); // Space before %c to consume any newline character

} while (continueInput == 'y' || continueInput == 'Y');

printf("Thank you!\n");

return 0;

}
Practical 06
#include <stdio.h>

int main() {

int n;

long long factorial = 1; // Using long long to handle larger factorials

// Read the value of n

printf("Enter a non-negative integer: ");

scanf("%d", &n);

// Check for negative input

if (n < 0) {

printf("Factorial is not defined for negative integers.\n");

return 1; // Exit the program with an error code

// Calculate factorial using a while loop

int i = 1;

while (i <= n) {

factorial *= i; // Multiply current factorial by i

i++; // Increment i

// Print the result

printf("Factorial of %d is %lld\n", n, factorial);

return 0;

}
Practical 07 (a)
#include <stdio.h>

int main() {

int rows, cols;

// Get the dimensions of the checkboard

printf("Enter the number of rows: ");

scanf("%d", &rows);

printf("Enter the number of columns: ");

scanf("%d", &cols);

printf("\nCheckboard Pattern:\n");

// Generate the checkboard pattern

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

if ((i + j) % 2 == 0) {

printf("X "); // Print 'X' for black squares

} else {

printf("O "); // Print 'O' for white squares

printf("\n"); // New line after each row

return 0;

}
Practical 07 (b)
#include <stdio.h>

int main() {

int rows, cols, squareSize;

char char1, char2;

// Get the dimensions and characters for the checkboard

printf("Enter the number of rows: ");

scanf("%d", &rows);

printf("Enter the number of columns: ");

scanf("%d", &cols);

printf("Enter the character for black squares: ");

scanf(" %c", &char1); // Space before %c to consume any newline character

printf("Enter the character for white squares: ");

scanf(" %c", &char2);

printf("Enter the size of each square: ");

scanf("%d", &squareSize);

printf("\nCheckboard Pattern:\n");

// Generate the checkboard pattern

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

// Determine if the current square is black or white

if ((i + j) % 2 == 0) {

// Nested if-else for square size

for (int k = 0; k < squareSize; k++) {

for (int l = 0; l < squareSize; l++) {

if (k == 0 || k == squareSize - 1 || l == 0 || l == squareSize - 1) {
printf("%c ", char1); // Print border character

} else {

printf(" "); // Space inside the square

printf("\n"); // New line after each inner square row

} else {

// Nested if-else for square size

for (int k = 0; k < squareSize; k++) {

for (int l = 0; l < squareSize; l++) {

if (k == 0 || k == squareSize - 1 || l == 0 || l == squareSize - 1) {

printf("%c ", char2); // Print border character

} else {

printf(" "); // Space inside the square

printf("\n"); // New line after each inner square row

printf("\n"); // New line after each row of squares

return 0;

}
Practical 08
#include <stdio.h>

int main() {

int choice;

float temperature, convertedTemperature;

do {

// Display menu options

printf("\nTemperature Conversion Menu:\n");

printf("1. Celsius to Fahrenheit\n");

printf("2. Celsius to Kelvin\n");

printf("3. Fahrenheit to Celsius\n");

printf("4. Fahrenheit to Kelvin\n");

printf("5. Kelvin to Celsius\n");

printf("6. Kelvin to Fahrenheit\n");

printf("7. Exit\n");

printf("Enter your choice (1-7): ");

scanf("%d", &choice);

switch (choice) {

case 1: // Celsius to Fahrenheit

printf("Enter temperature in Celsius: ");

scanf("%f", &temperature);

convertedTemperature = (temperature * 9/5) + 32;

printf("Temperature in Fahrenheit: %.2f\n", convertedTemperature);

break;

case 2: // Celsius to Kelvin

printf("Enter temperature in Celsius: ");


scanf("%f", &temperature);

convertedTemperature = temperature + 273.15;

printf("Temperature in Kelvin: %.2f\n", convertedTemperature);

break;

case 3: // Fahrenheit to Celsius

printf("Enter temperature in Fahrenheit: ");

scanf("%f", &temperature);

convertedTemperature = (temperature - 32) * 5/9;

printf("Temperature in Celsius: %.2f\n", convertedTemperature);

break;

case 4: // Fahrenheit to Kelvin

printf("Enter temperature in Fahrenheit: ");

scanf("%f", &temperature);

convertedTemperature = (temperature - 32) * 5/9 + 273.15;

printf("Temperature in Kelvin: %.2f\n", convertedTemperature);

break;

case 5: // Kelvin to Celsius

printf("Enter temperature in Kelvin: ");

scanf("%f", &temperature);

convertedTemperature = temperature - 273.15;

printf("Temperature in Celsius: %.2f\n", convertedTemperature);

break;

case 6: // Kelvin to Fahrenheit

printf("Enter temperature in Kelvin: ");

scanf("%f", &temperature);

convertedTemperature = (temperature - 273.15) * 9/5 + 32;


printf("Temperature in Fahrenheit: %.2f\n", convertedTemperature);

break;

case 7: // Exit

printf("Exiting the program.\n");

break;

default: // Invalid option

printf("Invalid choice! Please select a valid option.\n");

break;

} while (choice != 7); // Continue until user chooses to exit

return 0;

}
Practical 09
#include <stdio.h>

// Function to calculate factorial

long long factorial(int n) {

long long result = 1; // Using long long to handle large factorials

for (int i = 1; i <= n; i++) {

result *= i; // Multiply result by i

return result; // Return the factorial

int main() {

int n;

// Read the value of n

printf("Enter a non-negative integer: ");

scanf("%d", &n);

// Check for negative input

if (n < 0) {

printf("Factorial is not defined for negative integers.\n");

} else {

// Call the factorial function and display the result

long long result = factorial(n);

printf("Factorial of %d is %lld\n", n, result);

return 0;

}
Practical 10
#include <stdio.h>

void draw_rectangle(int width, int height) {

// Draw a rectangle with the given width and height

for (int i = 0; i < height; i++) {

for (int j = 0; j < width; j++) {

// Print '*' to represent the rectangle

printf("*");

printf("\n"); // New line after each row

void draw_square(int side_length) {

// Call draw_rectangle to draw a square

draw_rectangle(side_length, side_length);

int main() {

printf("Drawing Rectangles:\n");

// Draw different rectangles

printf("Rectangle 1 (Width: 5, Height: 3):\n");

draw_rectangle(5, 3);

printf("\nRectangle 2 (Width: 10, Height: 4):\n");

draw_rectangle(10, 4);

printf("\nDrawing Squares:\n");
// Draw different squares

printf("Square 1 (Side Length: 5):\n");

draw_square(5);

printf("\nSquare 2 (Side Length: 8):\n");

draw_square(8);

return 0;

You might also like