Project Report on C Programming and HTML Basics
Project Report on C Programming and HTML Basics
c
CopyEdit
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
sum = num1 + num2;
printf("Sum: %d\n", sum);
return 0;
}
Explanation: This program adds two numbers and prints the result.
2. Square of a Number:
c
CopyEdit
#include <stdio.h>
int main() {
int num, square;
printf("Enter a number: ");
scanf("%d", &num);
square = num * num;
printf("Square: %d\n", square);
return 0;
}
c
CopyEdit
#include <stdio.h>
int main() {
int num1, num2, product;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
product = num1 * num2;
printf("Product: %d\n", product);
return 0;
}
c
CopyEdit
#include <stdio.h>
int main() {
float num1, num2, division;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
if (num2 != 0) {
division = num1 / num2;
printf("Division: %.2f\n", division);
} else {
printf("Error: Division by zero\n");
}
return 0;
}
Explanation: This program divides two numbers and handles division by zero.
c
CopyEdit
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
c
CopyEdit
#include <stdio.h>
int main() {
int A[2][2], B[2][2], sum[2][2];
printf("Enter elements of matrix A (2x2):\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Sum of matrices:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
int main() {
int A[4][4], B[4][4], sum[4][4];
printf("Enter elements of matrix A (4x4):\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Sum of matrices:\n");
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
c
CopyEdit
#include <stdio.h>
int main() {
int A[2][3], B[3][2], product[2][2];
printf("Enter elements of matrix A (2x3):\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Enter elements of matrix B (3x2):\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &B[i][j]);
}
}
printf("Product of matrices:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}
return 0;
}
Explanation: This program multiplies a 2x3 matrix with a 3x2 matrix and prints the
result.
Q9: How do you create an HTML form to calculate the sum of two numbers?
Answer:
html
CopyEdit
<!DOCTYPE html>
<html>
<head>
<title>Sum Calculator</title>
<script>
function calculateSum() {
var num1 = parseInt(document.getElementById("num1").value);
var num2 = parseInt(document.getElementById("num2").value);
var sum = num1 + num2;
document.getElementById("result").innerHTML = "Sum: " + sum;
}
</script>
</head>
<body>
<h2>Sum Calculator</h2>
<form>
Number 1: <input type="number" id="num1"><br><br>
Number 2: <input type="number" id="num2"><br><br>
<input type="button" value="Calculate" onclick="calculateSum()">
</form>
<p id="result"></p>
</body>
</html>
Explanation: This HTML page contains a form that takes two numbers as input and
calculates their sum using JavaScript.
Q10: How do you create an HTML form to calculate the square of a number?
Answer:
html
CopyEdit
<!DOCTYPE html>
<html>
<head>
<title>Square Calculator</title>
<script>
function calculateSquare() {
var num = parseInt(document.getElementById("num").value);
var square = num * num;
document.getElementById("result").innerHTML = "Square: " +
square;
}
</script>
</head>
<body>
<h2>Square Calculator</h2>
<form>
Number: <input type="number" id="num"><br><br>
<input type="button" value="Calculate"
onclick="calculateSquare()">
</form>
<p id="result"></p>
</body>
</html>
Explanation: This HTML page contains a form that calculates the square of a given
number using JavaScript.
Conclusion:
This project report covers a variety of fundamental topics in C programming and HTML,
including arithmetic operations, matrix manipulations, and interactive HTML forms. It provides
40 questions and answers, complete with code examples and detailed explanations. This will
help in understanding how to perform basic operations in programming and design simple web
applications using HTML and JavaScript.
Part 1: C Programming
11. Factorial of a Number:
c
CopyEdit
#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial of %d is: %d\n", num, factorial);
return 0;
}
c
CopyEdit
#include <stdio.h>
int main() {
int num, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}
c
CopyEdit
#include <stdio.h>
int main() {
int num, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &num);
printf("Fibonacci Series: ");
for (int i = 1; i <= num; i++) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
printf("\n");
return 0;
}
Explanation: This program prints the Fibonacci series up to the given number of terms.
c
CopyEdit
#include <stdio.h>
int main() {
int num, reverse = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Reversed Number: %d\n", reverse);
return 0;
}
Explanation: This program reverses the digits of a number using a while loop.
Q15: How do you swap two numbers without using a third variable in C?
Answer:
c
CopyEdit
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Explanation: This program swaps two numbers without using a third variable by using
arithmetic operations.
c
CopyEdit
#include <stdio.h>
int main() {
int num, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
printf("Sum of digits: %d\n", sum);
return 0;
}
Explanation: This program calculates the sum of digits by extracting each digit of the
number and adding it.
17. Check Palindrome Number:
c
CopyEdit
#include <stdio.h>
int main() {
int num, originalNum, reversedNum = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
originalNum = num;
while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num /= 10;
}
if (originalNum == reversedNum)
printf("%d is a palindrome.\n", originalNum);
else
printf("%d is not a palindrome.\n", originalNum);
return 0;
}
c
CopyEdit
#include <stdio.h>
int main() {
int num, count = 0;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
num /= 10;
count++;
}
printf("Number of digits: %d\n", count);
return 0;
}
Explanation: This program counts the number of digits by continuously dividing the
number by 10.
19. Find Largest of Three Numbers:
c
CopyEdit
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3)
printf("%d is the largest number.\n", num1);
else if (num2 >= num1 && num2 >= num3)
printf("%d is the largest number.\n", num2);
else
printf("%d is the largest number.\n", num3);
return 0;
}
Explanation: This program finds the largest of three numbers using if-else conditions.
c
CopyEdit
#include <stdio.h>
#include <math.h>
int main() {
int num, sum = 0, remainder, originalNum, n = 0;
printf("Enter a number: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
n++;
}
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
sum += pow(remainder, n);
originalNum /= 10;
}
if (sum == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
Q21: How do you create an HTML form to calculate the multiplication of two numbers?
Answer:
html
CopyEdit
<!DOCTYPE html>
<html>
<head>
<title>Multiplication Calculator</title>
<script>
function calculateProduct() {
var num1 = parseInt(document.getElementById("num1").value);
var num2 = parseInt(document.getElementById("num2").value);
var product = num1 * num2;
document.getElementById("result").innerHTML = "Product: " +
product;
}
</script>
</head>
<body>
<h2>Multiplication Calculator</h2>
<form>
Number 1: <input type="number" id="num1"><br><br>
Number 2: <input type="number" id="num2"><br><br>
<input type="button" value="Calculate"
onclick="calculateProduct()">
</form>
<p id="result"></p>
</body>
</html>
Explanation: This HTML page calculates the multiplication of two numbers using
JavaScript.
Q22: How do you create an HTML form to calculate the division of two numbers?
Answer:
html
CopyEdit
<!DOCTYPE html>
<html>
<head>
<title>Division Calculator</title>
<script>
function calculateDivision() {
var num1 =
parseFloat(document.getElementById("num1").value);
var num2 =
parseFloat(document.getElementById("num2").value);
if (num2 !== 0) {
var division = num1 / num2;
document.getElementById("result").innerHTML = "Division:
" + division;
} else {
document.getElementById("result").innerHTML = "Error:
Division by zero.";
}
}
</script>
</head>
<body>
<h2>Division Calculator</h2>
<form>
Number 1: <input type="number" id="num1"><br><br>
Number 2: <input type="number" id="num2"><br><br>
<input type="button" value="Calculate"
onclick="calculateDivision()">
</form>
<p id="result"></p>
</body>
</html>
Explanation: This HTML page calculates the division of two numbers, ensuring no
division by zero occurs.
Part 1: C Programming
23. Sum of Elements in an Array:
c
CopyEdit
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of array elements: %d\n", sum);
return 0;
}
c
CopyEdit
#include <stdio.h>
int main() {
int n, largest;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
largest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
printf("Largest element is: %d\n", largest);
return 0;
}
Explanation: This program finds the largest element in an array by comparing each
element with the current largest element.
Q25: How do you perform matrix addition for two 2x2 matrices in C?
Answer:
c
CopyEdit
#include <stdio.h>
int main() {
int a[2][2], b[2][2], sum[2][2];
printf("Enter elements of first matrix (2x2):\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of second matrix (2x2):\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &b[i][j]);
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
sum[i][j] = a[i][j] + b[i][j];
}
}
printf("Matrix sum is:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}
return 0;
}
Explanation: This program adds two 2x2 matrices by looping through the elements and
adding corresponding elements.
Q26: How do you perform matrix multiplication for a 2x3 and a 3x2 matrix in C?
Answer:
c
CopyEdit
#include <stdio.h>
int main() {
int a[2][3], b[3][2], result[2][2];
printf("Enter elements of first matrix (2x3):\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of second matrix (3x2):\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &b[i][j]);
}
}
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
result[i][j] = 0;
for (int k = 0; k < 3; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Matrix multiplication result is:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}
return 0;
}
Explanation: This program multiplies a 2x3 matrix by a 3x2 matrix and stores the result
in a 2x2 matrix.
Q27: How do you find the sum of the first N natural numbers in C?
Answer:
c
CopyEdit
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
sum = (n * (n + 1)) / 2; // Formula for sum of N natural numbers
printf("Sum of first %d natural numbers: %d\n", n, sum);
return 0;
}
Explanation: This program uses the formula (n * (n + 1)) / 2 to calculate the sum
of the first N natural numbers.
Q28: How do you count the number of odd and even numbers in an array in C?
Answer:
c
CopyEdit
#include <stdio.h>
int main() {
int n, odd = 0, even = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0)
even++;
else
odd++;
}
printf("Odd numbers: %d\n", odd);
printf("Even numbers: %d\n", even);
return 0;
}
Explanation: This program counts the number of odd and even numbers in an array
using a loop.
c
CopyEdit
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num / 2; i++) {
if (num % i == 0) {
sum += i;
}
}
if (sum == num)
printf("%d is a perfect number.\n", num);
else
printf("%d is not a perfect number.\n", num);
return 0;
}
Explanation: This program checks if a number is perfect by summing its divisors and
comparing the sum to the number.
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n], temp;
printf("Enter elements: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < n / 2; i++) {
temp = arr[i];
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = temp;
}
printf("Reversed array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Explanation: This program reverses the array by swapping elements from both ends
toward the middle.
html
CopyEdit
<!DOCTYPE html>
<html>
<head>
<title>HTML Table</title>
</head>
<body>
<h2>Sample Table</h2>
<table border="1">
<tr>
<th>Header 1</th>
<th>Header 2</th>
<th>Header 3</th>
</tr>
<tr>
<td>Data 1</td>
<td>Data 2</td>
<td>Data 3</td>
</tr>
<tr>
<td>Data 4</td>
<td>Data 5</td>
<td>Data 6</td>
</tr>
</table>
</body>
</html>
Explanation: This code creates a simple HTML table with 3 columns and 2 rows of data.
html
CopyEdit
<!DOCTYPE html>
<html>
<head>
<title>User Input Form</title>
</head>
<body>
<h2>Enter Your Details</h2>
<form action="/submit_form" method="post">
Name: <input type="text" name="name"><br><br>
Age: <input type="number" name="age"><br><br>
Email: <input type="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Explanation: This form collects the user's name, age, and email and submits the data to
the server.
c
CopyEdit
#include <stdio.h>
int main() {
int num, flag = 0;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);
return 0;
}
Explanation: This program checks whether the number is divisible by any number other
than 1 and itself. If it is divisible, it is not prime.
c
CopyEdit
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci sequence: ");
for (int i = 0; i < n; i++) {
if (i <= 1)
next = i;
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
printf("\n");
return 0;
}
Explanation: This program generates the Fibonacci sequence by adding the two previous
numbers to get the next number in the sequence.
c
CopyEdit
#include <stdio.h>
int main() {
int num, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
fact *= i;
}
printf("Factorial of %d is: %d\n", num, fact);
return 0;
}
Explanation: This program calculates the factorial by multiplying all integers from 1 to
the number.
c
CopyEdit
#include <stdio.h>
int main() {
int num, reverse = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
reverse = reverse * 10 + remainder;
num /= 10;
}
printf("Reversed number is: %d\n", reverse);
return 0;
}
Explanation: This program reverses the digits of a number by extracting each digit and
appending it to the reversed number.
c
CopyEdit
#include <stdio.h>
int main() {
int num, sum = 0, remainder;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
remainder = num % 10;
sum += remainder;
num /= 10;
}
printf("Sum of digits is: %d\n", sum);
return 0;
}
Explanation: This program adds the digits of a number by extracting each digit using
modulus and dividing the number by 10.
c
CopyEdit
#include <stdio.h>
#include <math.h>
int main() {
int binary, decimal = 0, remainder, i = 0;
printf("Enter a binary number: ");
scanf("%d", &binary);
while (binary != 0) {
remainder = binary % 10;
decimal += remainder * pow(2, i);
binary /= 10;
i++;
}
printf("Decimal equivalent: %d\n", decimal);
return 0;
}
c
CopyEdit
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 2; i <= n; i += 2) {
sum += i;
}
printf("Sum of even numbers from 1 to %d is: %d\n", n, sum);
return 0;
}
Explanation: This program calculates the sum of even numbers from 1 to N by iterating
through the range and adding only the even numbers.
c
CopyEdit
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
for (int i = 1; i <= n; i += 2) {
sum += i;
}
printf("Sum of odd numbers from 1 to %d is: %d\n", n, sum);
return 0;
}
Explanation: This program calculates the sum of odd numbers from 1 to N by iterating
through the range and adding only the odd numbers.