Solving Some Programming Problems | Grade 12 | Computer | File Handling | Structure | JavaScripts functions | Simple Form validation | PhP with Database
Rohan Koirala - Thu Mar 21 2024
0
Some Structure Questions answer
Create a structure student with members: sid, name, marks in math, science and computer. Write a program that takes records of 20 students and display them.
#include <stdio.h>
int main() {
int i;
struct student {
int sid, m, sc, c;
char name[40];
}s[20];
for(i=0;i<20;i++) {
printf("Enter records of student %d\n",i+1);
printf("Enter ID: ");
scanf("%d",&s[i].sid);
printf("Enter a name: ");
scanf("%s",s[i].name);
printf("Enter marks of math: ");
scanf("%d",&s[i].m);
printf("Enter marks of science: ");
scanf("%d",&s[i].sc);
printf("Enter marks of Computer: ");
scanf("%d",&s[i].c);
}
printf("Records of students are:\n");
printf("SID\t\tName\t\tMath\t\tScience\t\tComputer\n");
for(i=0;i<20;i++) {
printf("%d\t\t%s\t\t%d\t\t%d\t\t%d\n",s[i].sid,s[i].name,s[i].m,s[i].sc,s[i].c);
}
}
Create a structure student with members: sid, name, marks in math, science and computer. Write a program that takes records of 20 students and display only those records who are pass in all subject. (Pass marks = 32).
#include <stdio.h>
int main() {
int i;
struct student {
int sid, m, sc, c;
char name[40];
}s[20];
for(i=0;i<20;i++) {
printf("Enter records of student %d\n",i+1);
printf("Enter ID: ");
scanf("%d",&s[i].sid);
printf("Enter a name: ");
scanf("%s",s[i].name);
printf("Enter marks of math: ");
scanf("%d",&s[i].m);
printf("Enter marks of science: ");
scanf("%d",&s[i].sc);
printf("Enter marks of Computer: ");
scanf("%d",&s[i].c);
}
printf("Records of students are:\n");
printf("SID\t\tName\t\tMath\t\tScience\t\tComputer\n");
for(i=0;i<20;i++) {
if(s[i].m>=32 && s[i].sc>=32 && s[i].c>=32)
printf("%d\t\t%s\t\t%d\t\t%d\t\t%d\n",s[i].sid,s[i].name,s[i].m,s[i].sc,s[i].c);
}
}
Write a structure to store the names, salary and hours of work per day of 10 employees in a company. Write a program to increase the salary depending on the number of hours of work per day as follows and then print the name of all the employees along with their final salaries.
Hours of work per day: 8 10 >=12
Increase in salary: $50 $100 $150
#include <stdio.h>
int main() {
int i;
struct employee {
float sal;
int hrs;
char name[40];
}e[10];
for(i=0;i<10;i++) {
printf("Enter records of Employee %d\n",i+1);
printf("Enter a name: ");
scanf("%s",e[i].name);
printf("Enter hours of work: ");
scanf("%d",&e[i].hrs);
printf("Enter Salary: ");
scanf("%f",&e[i].sal);
}
printf("Records of employees after Salary increment:\n");
printf("Name\t\tWork hour\t\tSalary\n");
for(i=0;i<10;i++) {
if(e[i].hrs>=12)
e[i].sal+=150;
else if(e[i].hrs>=10)
e[i].sal+=100;
else if(e[i].hrs>=8)
e[i].sal+=50;
else
e[i].sal+=0;
printf("%s\t\t%d\t\t\t%.2f\n",e[i].name,e[i].hrs,e[i].sal);
}
}
Write a C program to input emp_id, emp_name, emp_salary of the employees using structure and display emp_id, emp_name and emp_salary of the employee receiving highest salary.
#include <stdio.h>
int main() {
int i;
struct employee {
float emp_salary;
int emp_id;
char emp_name[40];
}e[10],temp;
for(i=0;i<10;i++) {
printf("Enter records of Employee %d\n",i+1);
printf("Enter ID: ");
scanf("%d",&e[i].emp_id);
printf("Enter a name: ");
scanf("%s",e[i].emp_name);
printf("Enter Salary: ");
scanf("%f ",&e[i].emp_salary);
}
printf("Employee with highest Salary is:\n");
temp=e[0];
for(i=1;i<10;i++) {
if(e[i].emp_salary>temp.emp_salary)
temp=e[i];
}
printf("EmpID:%d\tName:%s\tEmpSalary:%.2f",temp.emp_id,temp.emp_name, temp.emp_salary);
}
Write a C program using structure to input rollno, name, and percentage of several students and arrange them in descending order by percentage.
#include <stdio.h>
int main() {
int i,j;
struct student {
int rollno;
float percent;
char name[40];
}s[20],temp;
for(i=0;i<10;i++) {
printf("Enter records of student %d\n",i+1);
printf("Enter rollno: ");
scanf("%d",&s[i].rollno);
printf("Enter a name: ");
scanf("%s",s[i].name);
printf("Enter percentage: ");
scanf("%f",&s[i].percent);
}
for(i=0;i<10;i++) {
for(j=i+1;j<10;j++) {
if(s[i].percent<s[j].percent) {
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
}
printf("Records of students based on highest Percernt are:\n");
printf("RollNo\t\tName\t\tPercent\n");
for(i=0;i<3;i++){
printf("%d\t\t%s\t\t%.2f\n",s[i].rollno,s[i].name,s[i].percent);
}
}
Some file handling Question’s Solution
Write a C program to enter ID, employee_name, and post of 10 employees and store them in a data file named “emp.txt”.
#include <stdio.h>
int main() {
FILE *fp;
int id;
char name[50], post[50];
fp = fopen("emp.txt", "w");
printf("Enter details of 10 employees:\n");
for (int i = 1; i <= 10; i++) {
printf("Employee %d:\n", i);
printf("ID: ");
scanf("%d", &id);
printf("Name: ");
scanf("%s", name);
printf("Post: ");
scanf("%s", post);
fprintf(fp, "%d %s %s\n", id, name, post);
}
fclose(fp);
fp = fopen("emp.txt", "r");
printf("\nEmployee records:\n");
printf("ID\t\tName\t\tPost\n");
while (fscanf(fp,"%d %s %s",&id,name,post) != EOF) {
printf("%d\t\t%s\t\t%s\n", id, name, post);
}
fclose(fp);
}
Write a C program to open a new file and read roll_no, name, address and phone number of students until the user says “no”. after reading the data, write it to the file named “Std.txt”.
#include <stdio.h>
#include <conio.h>
int main() {
FILE *file;
char ch='y';
int roll_no;
char name[40];
char address[40];
char ph_no[12];
file = fopen("Std.txt", "w");
while(ch!='n') {
printf("\nEnter roll number: ");
scanf("%d", &roll_no);
printf("Enter name: ");
scanf(" %[^\n]s", name);
printf("Enter address: ");
scanf(" %s", address);
printf("Enter phone number: ");
scanf(" %s", ph_no);
fprintf(file, "%d\t%s\t%s\t%s\t\n", roll_no,name,address,ph_no);
printf("Do you want to enter another student (yes/no)? ");
ch=getch();
}
fclose(file);
}
Write a C program to open a new file and read roll_no, name, address and phone number of students until the user says “no”. after reading the data, write it to the file named “Std.txt” then display each record.
#include <stdio.h>
#include <conio.h>
int main() {
FILE *file;
char ch='y';
int roll_no;
char name[40];
char address[40];
char ph_no[12];
file = fopen("Std.txt", "w");
while(ch!='n') {
printf("\nEnter roll number: ");
scanf("%d",&roll_no);
printf("Enter name: ");
scanf(" %s", name);
printf("Enter address: ");
scanf(" %s", address);
printf("Enter phone number: ");
scanf(" %s", ph_no);
fprintf(file, "%d\t%s\t%s\t%s\t\n", roll_no,name,address,ph_no);
printf("Do you want to enter another student (yes/no)? ");
ch=getch();
}
fclose(file);
file = fopen("Std.txt", "r");
printf("\nRoll\t\tName\t\t\tAddress\t\t Phn_no\n");
while (fscanf(file, "%d %s %s %s", &roll_no, name, address, ph_no) != EOF) {
printf("%d\t\t%s\t\t\t%s\t\t%s\n", roll_no, name, address, ph_no);
}
fclose(file);
}
A data file “employee.dat” contains emp_id, emp_name, emp_salary of some employees. Write a C program that reads each record in the file and display only those records whose salary is greater and equal to 50000.
#include <stdio.h>
int main() {
FILE *file;
int emp_id;
char emp_name[40];
float emp_salary;
file = fopen("employee.dat","r");
printf("Records with salary greater than or equal to 50000:\n");
while (fscanf(file,"%d%s%f", &emp_id, emp_name, &emp_salary) != EOF) {
if (emp_salary >= 50000) {
printf("%d\t\t%s\t\t%.2f\n", emp_id, emp_name, emp_salary);
}
}
fclose(file);
}
Web Technology:
Check even or odd in javascript using function.
<!-- Your html code here -->
<script>
let input = prompt('Enter a no: ')
input_no_2_int = parseInt(input)
if ((input_no_2_int % 2) === 0) {
let check = 'Even No'
}
else {
let check = 'Odd No'
}
document.write('The given no is '+check)
</script>
<!-- Your html code here -->
Write a javascript code for form validation.
<!-- Your html code here -->
<script>
function validate() {
if (document.myForm.Name.value == "") {
alert("Please provide your name!");
}
if (document.myForm.EMail.value == "") {
alert("Please provide your Email!");
}
return (true);
}
</script>
<form name="myForm" onsubmit="validate();">
Name: <input type="text" name="Name">
EMail: <input type="text" name="EMail">
<input type="submit" value="Submit">
</form>
<!-- Your html code here -->
How do you connect to the database with PHP
<?php
$server="localhost";
$username="root";
$password="";
$dbname="clz";
// Create connection
$conn= mysqli_connect($server, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
else{
echo "Connected successfully";
}
?>
Insert data in database through html form with PHP.
<form method="POST">
Name:<input type="text" name="name">
Email:<input type="text" name="email">
<button type="submit" name="submit">Submit</button>
</form>
<?php
$conn= mysqli_connect("localhost", "root", "", "clz");
if(isset($_POST['submit'])){
if(!empty($_POST['name']) && !empty($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$query= "INSERT INTO emp(name,email) values('$name','$email')";
$run= mysqli_query($conn,$query);
if($run){
echo "Form Submitted Successfully";
}
else{
echo "Form not Submitted";
}
}
else{
echo "All field are required";
}
}
?>
Fetch data in html from database with PHP.
<?php
$conn=mysqli_connect("localhost", "root", "", "clz");
$query="SELECT * FROM emp";
$result=mysqli_query($conn,$query);
?>
<!DOCTYPE html>
<html>
<head>
<title>fetch data</title>
</head>
<body>
<?php
while($rows=mysqli_fetch_assoc($result)) {
echo "ID : ".$rows['id']."<br>";
echo "Name : ".$rows['name']."<br>";
echo "E-mail :", $rows['email']."<br><br>";
}
?>
</body>
</html>