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

coding (1) (1)

The document contains multiple programming examples in C and PHP, demonstrating various concepts such as swapping numbers using pointers, calculating areas, finding the largest element in arrays, file handling, and basic arithmetic operations. Each example includes code snippets with explanations for tasks like creating files, appending data, and performing calculations. The document serves as a practical guide for learning programming techniques and file operations.

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)
6 views

coding (1) (1)

The document contains multiple programming examples in C and PHP, demonstrating various concepts such as swapping numbers using pointers, calculating areas, finding the largest element in arrays, file handling, and basic arithmetic operations. Each example includes code snippets with explanations for tasks like creating files, appending data, and performing calculations. The document serves as a practical guide for learning programming techniques and file operations.

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/ 29

1.

Swapping Two Numbers Using Pointer


#include <stdio.h>

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


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

int main() {
int x = 1, y = 12;
printf("input: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("output: x = %d, y = %d\n", x, y);
return 0;
}
2.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 is: %.2f square units\n", area);

return 0;
}
3. 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;
}
4. 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;
}
5. 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;
}
6. 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;
}
7. 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;
}
8. 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;
}
9. 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;
}
10.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;
}
11. 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>
#include <stdlib.h>
struct Student {
int rollNo;
char name[50];
float marks;
};
int main() {
FILE *file;
struct Student s;
int n;
file = fopen("students.txt", "w");
if (file == NULL) {
printf("Error opening file for writing!\n");
return 1;
}
printf("Enter the number of students: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
printf("\nEnter details of student %d:\n", i + 1);
printf("Roll Number: ");
scanf("%d", &s.rollNo);
printf("Name: ");
scanf(" %[^\n]", s.name);
printf("Marks: ");
scanf("%f", &s.marks);
fprintf(file, "%d %s %.2f\n", s.rollNo, s.name, s.marks);
}
fclose(file);
printf("\nData successfully written to the file.\n");
file = fopen("students.txt", "r");
if (file == NULL) {
printf("Error opening file for reading!\n");
return 1;
}
printf("\nContents of the file:\n");
printf("------------------------------------\n");
printf("Roll No\tName\t\tMarks\n");
printf("------------------------------------\n");
while (fscanf(file, "%d %s %f", &s.rollNo, s.name, &s.marks) != EOF) {
printf("%d\t%s\t\t%.2f\n", s.rollNo, s.name, s.marks);
}
fclose(file);
return 0;
}
12. 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;
}
13. 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 or cannot be opened!\n");
return 1;
}
printf("Enter the text to append: ");
getchar();
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 for reading!\n");
return 1;
}
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
return 0;
}
14. 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;
}
15. 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;
}
16. 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;
?>
17. 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.";
}
?>
18. 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;


?>
19. 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";


?>
20. 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.";
}
?>
21. 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>
22. 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>
23. 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>
24. 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>
25. 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