Tvarana Programs
Tvarana Programs
Submitted by:
SANAM HARI PRASAD
204C1A05F1
CSE
C PROGRAMS
1.Write a C Program to convert Octal Number to Hexadecimal Number.
SOURCE CODE:
#include <stdio.h>
#include<string.h>
int main()
{
int octaltobinary[]={0,1,10,11,100,101,110,111};
char hexadecimal[10];
char hex[10];
long int binary=0;
int octal;
int rem=0;
int position=1;
int len=0;
int k=0;
printf("Enter a Octal Number: ");
scanf("%d",&octal);
// Converting octal number into a binary number.
while(octal!=0)
{
rem=octal%10;
binary=octaltobinary[rem]*position+binary;
octal=octal/10;
position=position*1000;
}
printf("The Binary Number is : %ld",binary);
return 0;
}
OUTPUT:
Enter a Octal Number: 123
The Binary Number is: 1010011
The Hexadecimal Number is: 53
2.Write a C Program to find biggest of 3 Numbers.
SOURCE CODE:
#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("The Biggest Number is: %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The Biggest Number is: %d\n", num2);
} else {
printf("The Biggest Number is: %d\n", num3);
}
return 0;
}
OUTPUT:
Enter Three Numbers:
18
27
45
The Biggest Number is: 45
3. Write a C Program to check whether the given number is Armstrong
Number or not.
SOURCE CODE:
#include <stdio.h>
#include <math.h>
if (result == num) {
return 1;
} else {
return 0;
}
}
OUTPUT:
Enter a number: 153
153 is an Armstrong number.
4. Write a C Program to find if current year is Leap Year.
SOURCE CODE:
#include <stdio.h>
int main() {
int year;
printf("Enter the current year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
OUTPUT:
Enter the current year: 2023
2023 is not a leap year.
5. Write a C Program to find square root of a number.
SOURCE CODE:
#include <stdio.h>
#include <math.h>
int main() {
double number, squareRoot;
printf("Enter a number: ");
scanf("%lf", &number);
if (number >= 0) {
squareRoot = sqrt(number);
printf("The square root of %.2lf is %.2lf.\n", number, squareRoot);
} else {
printf("Cannot calculate the square root of a negative number.\n");
}
return 0;
}
OUTPUT:
Enter a number: 9
The square root of 9.00 is 3.00
6. Write a C Program for multiplication of two matrices.
SORCE CODE:
#include <stdio.h>
void matrixMultiplication(int A[][100], int B[][100], int result[][100], int rowsA, int
colsA, int colsB) {
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = 0;
for (int k = 0; k < colsA; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
}
int main() {
int rowsA, colsA, rowsB, colsB;
printf("Enter the number of rows and columns for matrix A: ");
scanf("%d %d", &rowsA, &colsA);
printf("Enter the number of rows and columns for matrix B: ");
scanf("%d %d", &rowsB, &colsB);
if (colsA != rowsB) {
printf("Matrix multiplication is not possible due to incompatible dimensions.\n");
return 1;
}
int matrixA[100][100], matrixB[100][100], resultMatrix[100][100];
printf("Enter the elements of matrix A:\n");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
scanf("%d", &matrixA[i][j]);
}
}
printf("Enter the elements of matrix B:\n");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
scanf("%d", &matrixB[i][j]);
}
}
matrixMultiplication(matrixA, matrixB, resultMatrix, rowsA, colsA, colsB);
return 0;
}
OUTPUT:
Enter the number of rows and columns for matrix A: 2 2
Enter the number of rows and columns for matrix B: 2 3
Enter the elements of matrix A:
12
34
Enter the elements of matrix B:
567
8 9 10
Result of matrix multiplication:
21 24 27
47 54 61
7. Write a C Program to read a string and print the length of each word.
SOURCE CODE:
#include <stdio.h>
#include <string.h>
int main() {
char inputString[1000];
printf("Enter a string: ");
fgets(inputString, sizeof(inputString), stdin);
int length = strlen(inputString);
if (inputString[length - 1] == '\n') {
inputString[length - 1] = '\0';
}
printf("Length of each word in the string:\n");
int wordLength = 0;
for (int i = 0; i < length; i++) {
if (inputString[i] != ' ' && inputString[i] != '\0') {
wordLength++;
} else {
if (wordLength > 0) {
printf("%d ", wordLength);
wordLength = 0;
}
}
}
printf("\n");
return 0;
}
OUTPUT:
Enter a string: Hello
Length of each word in the string: 5
8. Write a C Program to change the given String to Uppercase.
SOURCE CODE:
#include <stdio.h>
#include <string.h>
int main() {
char inputString[1000];
printf("Enter a string: ");
fgets(inputString, sizeof(inputString), stdin);
return 0;
}
OUTPUT:
Enter a string: C Programming
Uppercase string: C PROGRAMMING
9. Write a C Program to check if the Substring is present in the given
String.
SOURCE CODE:
#include <stdio.h>
#include <string.h>
int isSubstringPresent(const char *string, const char *substring) {
int stringLength = strlen(string);
int subLength = strlen(substring);
for (int i = 0; i <= stringLength - subLength; i++) {
int j;
for (j = 0; j < subLength; j++) {
if (string[i + j] != substring[j]) {
break;
}
}
if (j == subLength) {
return 1; // Substring found
}
}
return 0; // Substring not found
}
int main() {
char inputString[1000];
char substring[1000];
printf("Enter a string: ");
fgets(inputString, sizeof(inputString), stdin);
printf("Enter a substring to search for: ");
fgets(substring, sizeof(substring), stdin);
int lengthInput = strlen(inputString);
int lengthSub = strlen(substring);
if (inputString[lengthInput - 1] == '\n') {
inputString[lengthInput - 1] = '\0';
}
if (substring[lengthSub - 1] == '\n') {
substring[lengthSub - 1] = '\0';
}
if (isSubstringPresent(inputString, substring)) {
printf("Substring is present in the given string.\n");
} else {
printf("Substring is not present in the given string.\n");
}
return 0;
}
OUTPUT:
Enter a string: C Programming
Enter a substring to search for: Pro
Substring is present in the given string.
10. Write a C Program to compare two strings without predefined function.
SOURCE CODE:
#include <stdio.h>
int compareStrings(const char *str1, const char *str2) {
while (*str1 != '\0' && *str2 != '\0') {
if (*str1 != *str2) {
return 0;
}
str1++;
str2++;
}
if (*str1 == '\0' && *str2 == '\0') {
return 1;
} else {
return 0;
}
}
int main() {
char string1[1000];
char string2[1000];
printf("Enter the first string: ");
fgets(string1, sizeof(string1), stdin);
printf("Enter the second string: ");
fgets(string2, sizeof(string2), stdin);
int length1 = 0;
while (string1[length1] != '\0' && string1[length1] != '\n') {
length1++;
}
string1[length1] = '\0';
int length2 = 0;
while (string2[length2] != '\0' && string2[length2] != '\n') {
length2++;
}
string2[length2] = '\0';
if (compareStrings(string1, string2)) {
printf("Both strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
return 0;
}
OUTPUT:
Enter the first string: Hello C
Enter the second string: Hello C
Both strings are equal.
11. Write a C Program to check number is Positive, Negative or Zero using
C program until users says exit.
SOURCE CODE:
#include <stdio.h>
#include <string.h>
int main() {
char input[10];
int number;
while (1) {
printf("Enter a number: ");
fgets(input, sizeof(input), stdin);
int length = strlen(input);
if (input[length - 1] == '\n') {
input[length - 1] = '\0';
}
if (strcmp(input, "exit") == 0) {
printf("Exiting the program.\n");
break;
}
sscanf(input, "%d", &number);
if (number > 0) {
printf("Positive number.\n");
} else if (number < 0) {
printf("Negative number.\n");
} else {
printf("Zero.\n");
}
}
return 0;
}
OUTPUT:
Enter a number: 27
Positive number.
Enter a number: -78
Negative number.
Enter a number: 0
Zero.
12. Write a C Program to print Fibonacci series up to 100.
SOURCE CODE:
#include <stdio.h>
int main() {
int num1 = 0, num2 = 1, nextTerm = 0;
printf("Fibonacci Series up to 100:\n");
OUTPUT:
Fibonacci Series up to 100:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89.
JAVA PROGRAMS
1. Write a Java Program to implement Bubble Sort.
SOURCE CODE:
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of elements: ");
int n = scanner.nextInt();
int[] arr = new int[n];
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
bubbleSort(arr);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
scanner.close();
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}
OUTPUT:
Enter the number of elements: 6
Enter the elements:
12 56 3 1 67 78
Sorted array:
1 3 12 56 67 78
2. Write a Java Program to find sum of N natural numbers.
SOURCE CODE:
import java.util.Scanner;
public class SumOfNaturalNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer N: ");
int N = scanner.nextInt();
if (N <= 0) {
System.out.println("N should be a positive integer.");
} else {
int sum = calculateSum(N);
System.out.println("Sum of first " + N + " natural numbers is: " + sum);
}
scanner.close();
}
public static int calculateSum(int N) {
return N * (N + 1) / 2;
}
}
OUTPUT:
Enter a positive integer N: 10
Sum of first 10 natural numbers is: 55
HTML PROGRAMS
1.Write a HTML Program which takes input in 2 text fields and display the
sum of 2 values in alert box.
SOURCE CODE:
<!DOCTYPE html>
<html>
<head>
<title>Sum Calculator</title>
<script type="text/javascript">
function calculateSum() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
if (isNaN(num1) || isNaN(num2)) {
alert("Please enter valid numbers.");
} else {
var sum = num1 + num2;
alert("Sum: " + sum);
}
}
</script>
</head>
<body>
<h2>Sum Calculator</h2>
<label for="num1">Enter number 1: </label>
<input type="text" id="num1"><br><br>
<label for="num2">Enter number 2: </label>
<input type="text" id="num2"><br><br>
<button onclick="calculateSum()">Calculate Sum</button>
</body>
</html>
OUTPUT:
2. Print a Paragraph using HTML/CSS with 4-5 sentences. Each sentence
should be different font.
SOURCE CODE:
<!DOCTYPE html>
<html>
<head>
<title>Styled Paragraph</title>
<style>
p{
font-family: Arial, sans-serif;
font-size: 16px;
}
.sentence1 {
font-family: "Times New Roman", serif;
font-size: 18px;
font-style: italic;
}
.sentence2 {
font-family: "Courier New", monospace;
font-size: 20px;
font-weight: bold;
}
.sentence3 {
font-family: "Verdana", sans-serif;
font-size: 16px;
color: #008CBA;
}
.sentence4 {
font-family: "Georgia", serif;
font-size: 18px;
text-decoration: underline;
}
.sentence5 {
font-family: "Impact", sans-serif;
font-size: 22px;
color: #FF4500;
}
</style>
</head>
<body>
<p>
<span class="sentence1">Lorem ipsum dolor sit amet</span>, consectetur
adipiscing elit.
<span class="sentence2">Nulla facilisi</span>. Aenean vel <span
class="sentence3">massa ac ante tincidunt fringilla</span>.
Suspendisse potenti. <span class="sentence4">Cras auctor vehicula arcu</span>,
at tempus nibh tincidunt vel.
<span class="sentence5">Vestibulum euismod nunc non metus volutpat</span>.
</p>
</body>
</html>
OUTPUT: