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

Computer

The document contains various C programming examples, including calculations for area and perimeter of a rectangle, checking if a number is even or odd, calculating factorial using recursion, and managing student and teacher records. It also includes a project certificate, acknowledgment, and a declaration of originality for a project titled 'C and SQL'. Additionally, it covers file operations in C and basic SQL commands for managing a student database.

Uploaded by

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

Computer

The document contains various C programming examples, including calculations for area and perimeter of a rectangle, checking if a number is even or odd, calculating factorial using recursion, and managing student and teacher records. It also includes a project certificate, acknowledgment, and a declaration of originality for a project titled 'C and SQL'. Additionally, it covers file operations in C and basic SQL commands for managing a student database.

Uploaded by

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

1 AREA AND PERIMETER

#include <stdio.h>

// Function to calculate area and perimeter

void area_perimeter(float length, float breadth, float *area, float *perimeter) {


*area = length * breadth;

*perimeter = 2 * (length + breadth);

int main() {

float length, breadth, area, perimeter;

printf("Enter the length and breadth of the rectangle: ");

scanf("%f %f", &length, &breadth);

area_perimeter(length, breadth, &area, &perimeter);

printf("Area: %.2f\n", area);

printf("Perimeter: %.2f\n", perimeter);

return 0;

OUTPUT
2 NUMBER IS EVEN OR ODD
#include <stdio.h>

// Function to check even or odd

int evenodd(int num) {

return num % 2 == 0; // Returns 1 for even, 0 for odd

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (evenodd(num))

printf("The number %d is even.\n", num);

else

printf("The number %d is odd.\n", num);

return 0;

OUTPUT
3 FACTORIAL OF A NUMBER USING RECURSIVE FUNCTION
#include <stdio.h>

// Recursive function for factorial

unsigned long long factorial(int n) {

if (n == 0 || n == 1)

return 1;

else

return n * factorial(n - 1);

int main() {

int num;

printf("Enter a number to find its factorial: ");

scanf("%d", &num);

if (num < 0)

printf("Factorial is not defined for negative numbers.\n");

else

printf("Factorial of %d is %llu\n", num, factorial(num)) ;


OUTPUT
1 Input ID, Name, and Address of 20 Students and Sort Them by
Name
#include <stdio.h>

#include <string.h>

// Define structure

struct Student {

int id;

char name[50];

char address[100];

};

// Function to sort students by name

void sortByName(struct Student students[], int n) {

struct Student temp;

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

for (int j = i + 1; j < n; j++) {

if (strcmp(students[i].name, students[j].name) > 0) {

temp = students[i];

students[i] = students[j];

students[j] = temp;

}
}

int main() {

struct Student students[3];

int n;

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

scanf("%d", &n);

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

printf("\nEnter details for student %d\n", i + 1);

printf("ID: ");

scanf("%d", &students[i].id);

printf("Name: ");

scanf(" %[^\n]", students[i].name);

printf("Address: ");

scanf(" %[^\n]", students[i].address);

// Sort students by name

sortByName(students, n);

printf("\nStudents sorted by name:\n");

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

printf("ID: %d, Name: %s, Address: %s\n", students[i].id, students[i].name,


students[i].address);
}

return 0;

OUTPUT

2. Input Teacher's ID, Name, Address, and Subject for 10


Records and Sort Them by ID
#include <stdio.h>

#include <string.h>

// Define structure

struct Teacher {

int id;

char name[50];
char address[100];

char subject[50];

};

// Function to sort teachers by ID

void sortByID(struct Teacher teachers[], int n) {

struct Teacher temp;

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

for (int j = i + 1; j < n; j++) {

if (teachers[i].id > teachers[j].id) {

temp = teachers[i];

teachers[i] = teachers[j];

teachers[j] = temp;

int main() {

struct Teacher teachers[10];

int n;

printf("Enter the number of teachers (max 10): ");

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

printf("\nEnter details for teacher %d\n", i + 1);

printf("ID: ");

scanf("%d", &teachers[i].id);

printf("Name: ");

scanf(" %[^\n]", teachers[i].name);

printf("Address: ");

scanf(" %[^\n]", teachers[i].address);

printf("Subject: ");

scanf(" %[^\n]", teachers[i].subject);

// Sort teachers by ID

sortByID(teachers, n);

printf("\nTeachers sorted by ID:\n");

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

printf("ID: %d, Name: %s, Address: %s, Subject: %s\n",

teachers[i].id, teachers[i].name, teachers[i].address, teachers[i].subject);

return 0;

}
OUTPUT
3. Input 10 Students' Data (Name, Grade, Gender, Marks) and Print
Records with Total and Percentage
#include <stdio.h>

// Define structure

struct Student {

char name[50];

int grade;

char gender;

int marks[5];

int total;

float percentage;

};

int main() {

struct Student students[10];

int n, sum;

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

scanf("%d", &n);

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

printf("\nEnter details for student %d\n", i + 1);

printf("Name: ");

scanf(" %[^\n]", students[i].name);

printf("Grade: ");
scanf("%d", &students[i].grade);

printf("Gender (M/F): ");

scanf(" %c", &students[i].gender);

sum = 0;

printf("Enter marks for 5 subjects: ");

for (int j = 0; j < 5; j++) {

scanf("%d", &students[i].marks[j]);

sum += students[i].marks[j];

students[i].total = sum;

students[i].percentage = (float)sum / 5;

printf("\nStudent Records:\n");

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

printf("Name: %s, Grade: %d, Gender: %c, Total: %d, Percentage: %.2f%%\n",
students[i].name, students[i].grade, students[i].gender,

students[i].total, students[i].percentage);

return 0;

}
OUTPUT
Certificate of Approval
This is to certify that the project work entitled C and SQL has been successfully completed and
submitted by Nikas Paudel, a student of Class 12, Section Faraday, Roll Number 24, of
Gorkha International Public Secondary School, Dang.

This project was carried out under the guidance of AI Bhadur Pun in partial fulfillment of the
requirements for Computer Science. The work embodied in this project is original and has been
approved as satisfactory for submission.

Approved By:

__________________________
Subject Teacher
AI Bhadur Pun

__________________________
Science Coordinator
Swotantra Hamal

__________________________
External Examiner
Date: 2025/01/06

Acknowledgment

I would like to express my heartfelt gratitude to my subject teacher, AI Bhadur


Pun, for their invaluable guidance, support, and encouragement throughout the
completion of this project, titled "C and SQL". Their expertise and insights have
been instrumental in the successful completion of this work.

I am also thankful to Gorkha International Public Secondary School, Dang, for


providing the necessary resources and a conducive environment for learning and
research.

Lastly, I extend my appreciation to my classmates, friends, and family for their


constant support and encouragement during this project.

Thank you all for your valuable contributions to this endeavor.

Nikas Paudel
POINTERS 1. Perform Arithmetic Calculations (Sum, Difference,
Multiplication, Division)
#include <stdio.h>

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

printf("Sum: %d\n", (*a + *b));

printf("Difference: %d\n", (*a - *b));

printf("Multiplication: %d\n", (*a * *b));

if (*b != 0) {

printf("Division: %.2f\n", (float)(*a) / (*b));

} else {

printf("Division: Not possible (division by zero).\n");

}}

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

arithmeticOperations(&num1, &num2);

return 0;

}
OUTPUT
2. Check if a Number is Even or Odd Using Pointers
#include <stdio.h>

void checkEvenOdd(int *num) {

if (*num % 2 == 0) {

printf("%d is Even.\n", *num);

} else {

printf("%d is Odd.\n", *num);

int main() {

int number;

printf("Enter a number: ");

scanf("%d", &number);

checkEvenOdd(&number);

return 0;

OUPUT
3. Swap Two Values Using Call by Reference and Call by Value

(a) Using Call by Reference


#include <stdio.h>

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

int temp = *a;

*a = *b;

*b = temp;

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

swapByReference(&num1, &num2);

printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);

return 0;

}
OUTPUT

(b) Using Call by Value


#include <stdio.h>

void swapByValue(int a, int b) {

int temp = a;

a = b;

b = temp;

printf("Inside function (after swapping): a = %d, b = %d\n", a, b);

int main() {

int num1, num2;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

swapByValue(num1, num2);

printf("After function call: num1 = %d, num2 = %d (values remain


unchanged)\n", num1, num2);
return 0;

OUTPUT
GORKHA INTERNATIONAL PUBLIC SECONDARY SCHOOL DANG

A project work on
C AND SQL

SUBMITTED TO SUBMITTED BY

AI BHADHUR PUN NIKAS PAUDEL

Sign- Sign-
Declaration

I hereby declare that the project work C AND SQl


entitled submitted to Gorkha International Public
Secondary School, Dang, is a record of my original
work. This project was completed under the guidance of
AI Bhadur Pun and is submitted in partial fulfillment of
the requirements for Computer Science, Class 12.
I further declare that this project has not been previously
submitted to any other institution or organization for any
examination or qualification.

Name: Nikas Paudel


Class: 12
Section: Faraday
Roll Number: 24
Date: 2025/01/06
DATA FILE 1. Create and Write Data to a File

#include <stdio.h>

int main() {

FILE *file;

char name[50];

int roll, n;

file = fopen("data.txt", "w"); // Open file in write mode

if (file == NULL) {

printf("Error opening file!\n");

return 1;

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

scanf("%d", &n);

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

printf("Enter name of student %d: ", i + 1);

scanf(" %[^\n]", name);

printf("Enter roll number of student %d: ", i + 1);

scanf("%d", &roll);

fprintf(file, "Name: %s, Roll Number: %d\n", name, roll);

fclose(file);

printf("Data successfully written to 'data.txt'.\n");

return 0;}
2. Read Data from a File
#include <stdio.h>

int main() {

FILE *file;

char ch;

file = fopen("data.txt", "r"); // Open file in read mode

if (file == NULL) {

printf("Error opening file! Make sure 'data.txt' exists.\n");

return 1;

printf("Contents of the file:\n");

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

putchar(ch); // Display each character

fclose(file);

return 0;

}
3. Append Data to a File
#include <stdio.h>

int main() {

FILE *file;

char name[50];

int roll, n;

file = fopen("data.txt", "a"); // Open file in append mode

if (file == NULL) {

printf("Error opening file!\n");

return 1;

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

scanf("%d", &n);

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

printf("Enter name of student %d: ", i + 1);

scanf(" %[^\n]", name);

printf("Enter roll number of student %d: ", i + 1);

scanf("%d", &roll);

fprintf(file, "Name: %s, Roll Number: %d\n", name, roll);

fclose(file);

printf("Data successfully appended to 'data.txt'.\n");

return 0;

}
Structured Query Language
*CREATE DATABASE student;

USE student;

*CREATE TABLE student (

student_id INT AUTO_INCREMENT PRIMARY KEY,

student_name VARCHAR(100),

student_address VARCHAR(100),

student_grade INT );

*INSERT INTO student (student_name, student_address, student_grade) VALUES

('Ram', 'Kathmandu', 12),

('Sita', 'Lalitpur', 10),

('Hari', 'Pokhara', 11),

('Gita', 'Kathmandu', 12),

('Krishna', 'Bhaktapur', 9);

SELECT * FROM student;

SELECT student_id, student_name FROM student;

SELECT * FROM student WHERE student_name LIKE 'R%';

SELECT * FROM student WHERE student_name LIKE '%a';

SELECT * FROM student WHERE student_grade = 12;

SELECT * FROM student WHERE student_grade = 12 AND student_address = 'Kathmandu';

UPDATE student SET student_name = 'Rajesh' WHERE student_id = 1;

DELETE FROM student WHERE student_id = 3;

You might also like