0% found this document useful (0 votes)
2 views

Project Report on C Programming and HTML Basics

The project report covers fundamental topics in C programming and HTML, including arithmetic operations, matrix manipulations, and interactive HTML forms. It provides 40 questions and answers with code examples and explanations for each topic. The report aims to help readers understand basic programming operations and design simple web applications.

Uploaded by

timsinakomal300
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Project Report on C Programming and HTML Basics

The project report covers fundamental topics in C programming and HTML, including arithmetic operations, matrix manipulations, and interactive HTML forms. It provides 40 questions and answers with code examples and explanations for each topic. The report aims to help readers understand basic programming operations and design simple web applications.

Uploaded by

timsinakomal300
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Project Report on C Programming and HTML Basics

1. Sum of Two Numbers:

 Q1: How do you calculate the sum of two numbers in C?


 Answer:

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:

 Q2: How do you calculate the square of a number in C?


 Answer:

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;
}

Explanation: This program calculates the square of a given number.

3. Product of Two Numbers:

 Q3: How do you calculate the product of two numbers in C?


 Answer:

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;
}

Explanation: The program multiplies two numbers to find the product.

4. Division of Two Numbers:

 Q4: How do you divide two numbers in C?


 Answer:

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.

5. Checking Odd or Even:

 Q5: How do you check if a number is odd or even in C?


 Answer:

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;
}

Explanation: This program checks whether a number is odd or even.

6. Matrix (2x2) Addition:

 Q6: How do you add 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 matrix A (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 matrix B (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("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;
}

Explanation: This program adds two 2x2 matrices.

7. Matrix (4x4) Addition:

 Q7: How do you add two 4x4 matrices in C?


 Answer:
c
CopyEdit
#include <stdio.h>

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("Enter elements of matrix B (4x4):\n");


for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
scanf("%d", &B[i][j]);
}
}

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


for (int j = 0; j < 4; j++) {
sum[i][j] = A[i][j] + B[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;
}

Explanation: This program adds two 4x4 matrices.

8. Matrix (2x3) Multiplication:

 Q8: How do you multiply a 2x3 matrix with a 3x2 matrix in C?


 Answer:

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]);
}
}

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


for (int j = 0; j < 2; j++) {
product[i][j] = 0;
for (int k = 0; k < 3; k++) {
product[i][j] += A[i][k] * B[k][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.

Part 2: HTML Basics


9. HTML Program for Sum of Two Numbers:

 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.

10. HTML Program for Square of a Number:

 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:

 Q11: How do you calculate the factorial of a number in C?


 Answer:

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;
}

Explanation: This program calculates the factorial of a number using a loop.

12. Prime Number Check:

 Q12: How do you check if a number is prime in C?


 Answer:

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;
}

Explanation: This program checks if a number is prime by dividing it by all numbers


from 2 to half of the number.

13. Fibonacci Series:

 Q13: How do you print the Fibonacci series in C?


 Answer:

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.

14. Reverse of a Number:

 Q14: How do you reverse a number in C?


 Answer:

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.

15. Swapping Two Numbers:

 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.

16. Sum of Digits of a Number:

 Q16: How do you find the sum of digits of a number in C?


 Answer:

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:

 Q17: How do you check if a number is a palindrome in C?


 Answer:

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;
}

Explanation: This program checks if the number is a palindrome by reversing the


number and comparing it with the original number.

18. Count Digits in a Number:

 Q18: How do you count the number of digits in a number in C?


 Answer:

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:

 Q19: How do you find the largest of three numbers in C?


 Answer:

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.

20. Armstrong Number Check:

 Q20: How do you check if a number is an Armstrong number in C?


 Answer:

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;
}

Explanation: This program checks if a number is an Armstrong number by calculating


the sum of each digit raised to the power of the number of digits in the number.

21. HTML Program for Multiplication:

 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.

22. HTML Program for Division:

 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:

 Q23: How do you calculate the sum of elements in an array in C?


 Answer:

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;
}

Explanation: This program calculates the sum of elements in an array by looping


through each element and adding it to the sum.

24. Largest Element in an Array:

 Q24: How do you find the largest element in an array in C?


 Answer:

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.

25. Matrix Addition (2x2):

 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.

26. Matrix Multiplication (2x3 and 3x2):

 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.

27. Sum of First N Natural Numbers:

 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.

28. Count Odd and Even Numbers in an Array:

 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.

29. Check Perfect Number:

 Q29: How do you check if a number is a perfect number in C?


 Answer:

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.

30. Reverse an Array:

 Q30: How do you reverse an array in C?


 Answer:
c
CopyEdit
#include <stdio.h>

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.

31. HTML Table Creation:

 Q31: How do you create a table in HTML?


 Answer:

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.

32. HTML Form to Get User Input:

 Q32: How do you create an HTML form to get user input?


 Answer:

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.

33. Check Prime Number:

 Q33: How do you check if a number is prime in C?


 Answer:

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.

34. Fibonacci Sequence (Up to N Terms):

 Q34: How do you generate a Fibonacci sequence up to N terms in C?


 Answer:

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.

35. Find Factorial of a Number:

 Q35: How do you find the factorial of a number in C?


 Answer:

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.

36. Reverse a Number:

 Q36: How do you reverse a number in C?


 Answer:

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.

37. Sum of Digits of a Number:

 Q37: How do you find the sum of digits of a number in C?


 Answer:

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.

38. Convert Binary to Decimal:

 Q38: How do you convert a binary number to a decimal number in C?


 Answer:

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;
}

Explanation: This program converts a binary number to a decimal by processing each


binary digit and using powers of 2.

39. Sum of Even Numbers from 1 to N:

 Q39: How do you calculate the sum of even numbers from 1 to N in C?


 Answer:

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.

40. Sum of Odd Numbers from 1 to N:

 Q40: How do you calculate the sum of odd numbers from 1 to N in C?


 Answer:

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.

You might also like