0% found this document useful (0 votes)
5 views30 pages

coding (1)

The document contains a series of C and PHP programs demonstrating various programming concepts such as swapping numbers using pointers, calculating areas, finding the largest element in arrays, file operations, and basic arithmetic operations in PHP. Each program includes code snippets, input prompts, and output descriptions. The examples illustrate the use of structures, pointers, and file handling in C, as well as basic control structures in PHP.

Uploaded by

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

coding (1)

The document contains a series of C and PHP programs demonstrating various programming concepts such as swapping numbers using pointers, calculating areas, finding the largest element in arrays, file operations, and basic arithmetic operations in PHP. Each program includes code snippets, input prompts, and output descriptions. The examples illustrate the use of structures, pointers, and file handling in C, as well as basic control structures in PHP.

Uploaded by

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

26.

Swapping Two Numbers Using Pointers

#include <stdio.h>

void swap(int *a, int *b) {


*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}

int main() {
int x = 5, y = 10;
printf("Before swapping: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d\n", x, y);
return 0;
}
27. WAP in c to find area of room using pointer

#include <stdio.h>

void calculateArea(float *length, float *width, float *area) {


*area = (*length) * (*width);
}

int main() {
float length, width, area;

printf("Enter the length of the room: ");


scanf("%f", &length);

printf("Enter the width of the room: ");


scanf("%f", &width);

calculateArea(&length, &width, &area);

printf("The area of the room is: %.2f square units\n", area);

return 0;
}
28. Find Largest element using pointer

#include <stdio.h>

void findLargest(int *arr, int size, int *largest) {


*largest = *arr;
for (int i = 1; i < size; i++) {
if (*(arr + i) > *largest) {
*largest = *(arr + i);
}
}
}

int main() {
int n;

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


scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);


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

int largest;
findLargest(arr, n, &largest);

printf("The largest element is: %d\n", largest);

return 0;
}
29. Find largest element in array using pointer

#include <stdio.h>

int main() {
int n, *ptr, largest;

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


scanf("%d", &n);

int arr[n];
ptr = arr;

printf("Enter %d elements:\n", n);


for (int i = 0; i < n; i++) {
scanf("%d", (ptr + i));
}

largest = *ptr;
for (int i = 1; i < n; i++) {
if (*(ptr + i) > largest) {
largest = *(ptr + i);
}
}

printf("The largest element is: %d\n", largest);

return 0;
}
30. WAP to input a number and display its multiplication
table using pointer

#include <stdio.h>

void displayTable(int *num) {


for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", *num, i, (*num) * i);
}
}

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

displayTable(&number);

return 0;
}
31. Area of a Room Using Structure

#include <stdio.h>

typedef struct {
float length;
float width;
} Room;

void calculateArea(Room *r, float *area) {


*area = r->length * r->width;
}

int main() {
Room room;
float area;

printf("Enter the length of the room: ");


scanf("%f", &room.length);

printf("Enter the width of the room: ");


scanf("%f", &room.width);

calculateArea(&room, &area);

printf("The area of the room is: %.2f square units\n", area);

return 0;
}
32. Find Largest Element in Array Using Structure

#include <stdio.h>

typedef struct {
int *arr;
int size;
} Array;

void findLargest(Array *a, int *largest) {


*largest = *(a->arr);
for (int i = 1; i < a->size; i++) {
if (*(a->arr + i) > *largest) {
*largest = *(a->arr + i);
}
}
}

int main() {
Array array;
int largest;

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


scanf("%d", &array.size);

int arr[array.size];
array.arr = arr;

printf("Enter %d elements:\n", array.size);


for (int i = 0; i < array.size; i++) {
scanf("%d", &arr[i]);
}

findLargest(&array, &largest);

printf("The largest element is: %d\n", largest);

return 0;
}
33. Find Largest Element in Array Using Pointer and
Structure
#include <stdio.h>

typedef struct {
int *arr;
int size;
} Array;

void findLargest(Array *a, int *largest) {


*largest = *a->arr;
for (int i = 1; i < a->size; i++) {
if (*(a->arr + i) > *largest) {
*largest = *(a->arr + i);
}
}
}

int main() {
Array array;
int largest;

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


scanf("%d", &array.size);

int arr[array.size];
array.arr = arr;

printf("Enter %d elements:\n", array.size);


for (int i = 0; i < array.size; i++) {
scanf("%d", &arr[i]);
}

findLargest(&array, &largest);

printf("The largest element is: %d\n", largest);

return 0;
}
34. Multiplication Table Using Structure
#include <stdio.h>

typedef struct {
int number;
} Table;

void displayTable(Table *t) {


for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", t->number, i, t->number * i);
}
}

int main() {
Table t;

printf("Enter a number: ");


scanf("%d", &t.number);

displayTable(&t);

return 0;
}
35.Largest Element and Multiplication Table in One
Program Using Structure
#include <stdio.h>

typedef struct {
int *arr;
int size;
} Array;

typedef struct {
int number;
} Table;

void findLargest(Array *a, int *largest) {


*largest = *a->arr;
for (int i = 1; i < a->size; i++) {
if (*(a->arr + i) > *largest) {
*largest = *(a->arr + i);
}
}
}

void displayTable(Table *t) {


for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", t->number, i, t->number * i);
}
}

int main() {
Array array;
Table table;
int largest;

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


scanf("%d", &array.size);

int arr[array.size];
array.arr = arr;
printf("Enter %d elements:\n", array.size);
for (int i = 0; i < array.size; i++) {
scanf("%d", &arr[i]);
}

findLargest(&array, &largest);
printf("The largest element is: %d\n", largest);

printf("Enter a number for its multiplication table: ");


scanf("%d", &table.number);

displayTable(&table);

return 0;
}
36. Write a program to create a file and store student
information (roll number, name, and marks). Then display
the contents of the file.
#include <stdio.h>

typedef struct {
int roll;
char name[50];
float marks;
} Student;

int main() {
FILE *file;
char filename[50];
Student s;
int n;

printf("Enter the filename to create: ");


scanf("%s", filename);

file = fopen(filename, "w");


if (file == NULL) {
printf("Error: Cannot create file!\n");
return 1;
}

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


scanf("%d", &n);

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


printf("Enter roll number, name, and marks of student %d:\n", i + 1);
scanf("%d", &s.roll);
getchar(); // To clear the newline character from the buffer
fgets(s.name, 50, stdin);
s.name[strcspn(s.name, "\n")] = '\0'; // Remove newline character from the name
scanf("%f", &s.marks);

fwrite(&s, sizeof(Student), 1, file);


}
fclose(file);

printf("\nFile created successfully. Displaying contents:\n");


file = fopen(filename, "r");
while (fread(&s, sizeof(Student), 1, file)) {
printf("Roll: %d, Name: %s, Marks: %.2f\n", s.roll, s.name, s.marks);
}

fclose(file);
return 0;
}
37. Write a program to copy the contents of one file to
another.
#include <stdio.h>

int main() {
FILE *source, *destination;
char srcFilename[50], destFilename[50], ch;

printf("Enter the source filename: ");


scanf("%s", srcFilename);
printf("Enter the destination filename: ");
scanf("%s", destFilename);

source = fopen(srcFilename, "r");


if (source == NULL) {
printf("Error: Source file not found!\n");
return 1;
}

destination = fopen(destFilename, "w");


if (destination == NULL) {
printf("Error: Cannot create destination file!\n");
fclose(source);
return 1;
}

while ((ch = fgetc(source)) != EOF) {


fputc(ch, destination);
}

fclose(source);
fclose(destination);

printf("File copied successfully!\n");


return 0;
}
38. Write a program to copy the contents of one file to
another.
#include <stdio.h>

int main() {
FILE *source, *destination;
char srcFilename[50], destFilename[50], ch;

printf("Enter the source filename: ");


scanf("%s", srcFilename);
printf("Enter the destination filename: ");
scanf("%s", destFilename);

source = fopen(srcFilename, "r");


if (source == NULL) {
printf("Error: Source file not found!\n");
return 1;
}

destination = fopen(destFilename, "w");


if (destination == NULL) {
printf("Error: Cannot create destination file!\n");
fclose(source);
return 1;
}

while ((ch = fgetc(source)) != EOF) {


fputc(ch, destination);
}

fclose(source);
fclose(destination);

printf("File copied successfully!\n");


return 0;
}
38. Write a program to append data to an existing file and
display its new contents.
#include <stdio.h>

int main() {
FILE *file;
char filename[50], text[200], ch;

printf("Enter the filename to append data: ");


scanf("%s", filename);

file = fopen(filename, "a");


if (file == NULL) {
printf("Error: File not found!\n");
return 1;
}

printf("Enter the text to append: ");


getchar(); // To clear the newline character from the buffer
fgets(text, 200, stdin);
fputs(text, file);

fclose(file);

printf("\nData appended successfully. Displaying new contents:\n");


file = fopen(filename, "r");
if (file == NULL) {
printf("Error: Cannot open file!\n");
return 1;
}

while ((ch = fgetc(file)) != EOF) {


putchar(ch);
}

fclose(file);
return 0;
}
39. Write a program to create a file and store employee
information (ID, name, and salary). Then display the
contents of the file.
#include <stdio.h>

typedef struct {
int id;
char name[50];
float salary;
} Employee;

int main() {
FILE *file;
char filename[50];
Employee emp;
int n;

printf("Enter the filename to create: ");


scanf("%s", filename);

file = fopen(filename, "w");


if (file == NULL) {
printf("Error: Cannot create file!\n");
return 1;
}

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


scanf("%d", &n);

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


printf("Enter ID, name, and salary of employee %d:\n", i + 1);
scanf("%d", &emp.id);
getchar(); // To clear the newline character from the buffer
fgets(emp.name, 50, stdin);
emp.name[strcspn(emp.name, "\n")] = '\0'; // Remove newline character
scanf("%f", &emp.salary);

fwrite(&emp, sizeof(Employee), 1, file);


}
fclose(file);

printf("\nFile created successfully. Displaying contents:\n");


file = fopen(filename, "r");
while (fread(&emp, sizeof(Employee), 1, file)) {
printf("ID: %d, Name: %s, Salary: %.2f\n", emp.id, emp.name, emp.salary);
}

fclose(file);
return 0;
}
40. Write a program to create a file and store vehicle
details (registration number, model name, and price).
Then display the contents of the file.
#include <stdio.h>

typedef struct {
char regNumber[20];
char modelName[50];
float price;
} Vehicle;

int main() {
FILE *file;
char filename[50];
Vehicle veh;
int n;

printf("Enter the filename to create: ");


scanf("%s", filename);

file = fopen(filename, "w");


if (file == NULL) {
printf("Error: Cannot create file!\n");
return 1;
}

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


scanf("%d", &n);

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


printf("Enter registration number, model name, and price of vehicle %d:\n", i + 1);
scanf("%s", veh.regNumber);
getchar(); // To clear the newline character from the buffer
fgets(veh.modelName, 50, stdin);
veh.modelName[strcspn(veh.modelName, "\n")] = '\0'; // Remove newline character
scanf("%f", &veh.price);

fwrite(&veh, sizeof(Vehicle), 1, file);


}
fclose(file);

printf("\nFile created successfully. Displaying contents:\n");


file = fopen(filename, "r");
while (fread(&veh, sizeof(Vehicle), 1, file)) {
printf("Registration Number: %s, Model Name: %s, Price: %.2f\n", veh.regNumber,
veh.modelName, veh.price);
}

fclose(file);
return 0;
}
41. Write a PHP program to display the sum of the first 10
natural numbers.
<?php
$sum = 0;

for ($i = 1; $i <= 10; $i++) {


$sum += $i;
}

echo "The sum of the first 10 natural numbers is: " . $sum;
?>
42. Write a PHP program to check whether a given
number is even or odd.
<?php
$number = 25;

if ($number % 2 == 0) {
echo $number . " is an even number.";
} else {
echo $number . " is an odd number.";
}
?>
43. Write a PHP program to find the factorial of a number.
<?php
$number = 5;
$factorial = 1;

for ($i = 1; $i <= $number; $i++) {


$factorial *= $i;
}

echo "The factorial of " . $number . " is: " . $factorial;


?>
44. Write a PHP program to display the sum of two
numbers.
<?php
$num1 = 12;
$num2 = 8;

$sum = $num1 + $num2;

echo "The sum of $num1 and $num2 is: $sum";


?>
45. Write a PHP program to check whether a number is
positive, negative, or zero.
<?php
$number = -15;

if ($number > 0) {
echo "$number is positive.";
} elseif ($number < 0) {
echo "$number is negative.";
} else {
echo "$number is zero.";
}
?>
46. Find the largest of three numbers.
<html>
<head>
<title>Largest of Three Numbers</title>
</head>
<body>
<h2>Find the Largest of Three Numbers</h2>
<script>
let num1 = 10, num2 = 25, num3 = 15;

if (num1 >= num2 && num1 >= num3) {


document.write(`${num1} is the largest number.`);
} else if (num2 >= num1 && num2 >= num3) {
document.write(`${num2} is the largest number.`);
} else {
document.write(`${num3} is the largest number.`);
}
</script>
</body>
</html>
47. Calculate the area of a rectangle.
<html>
<head>
<title>Rectangle Area</title>
</head>
<body>
<h2>Calculate the Area of a Rectangle</h2>
<script>
let length = 10, width = 5;
let area = length * width;
document.write(`The area of the rectangle is: ${area} square units.`);
</script>
</body>
</html>
48. Calculate the square of a number
<html>
<head>
<title>Square of a Number</title>
</head>
<body>
<h2>Find the Square of a Number</h2>
<script>
let number = 7;
let square = number * number;
document.write(`The square of ${number} is: ${square}`);
</script>
</body>
</html>
49. Check whether a number is positive, negative, or zero

<!DOCTYPE html>
<html>
<head>
<title>Check Number</title>
</head>
<body>
<h2>Check if a Number is Positive, Negative, or Zero</h2>
<script>
let number = -5;
if (number > 0) {
document.write(`${number} is a positive number.`);
} else if (number < 0) {
document.write(`${number} is a negative number.`);
} else {
document.write(`${number} is zero.`);
}
</script>
</body>
</html>
50. Print the multiplication table of a number

<!DOCTYPE html>
<html>
<head>
<title>Multiplication Table</title>
</head>
<body>
<h2>Multiplication Table of a Number</h2>
<script>
let number = 5;
document.write(`<h3>Multiplication table of ${number}:</h3>`);
for (let i = 1; i <= 10; i++) {
document.write(`${number} x ${i} = ${number * i}<br>`);
}
</script>
</body>
</html>

You might also like