Q - Write program to read the student’s name, gender, three scores and find the average of the scores then print “pass” when the average is greater than 50, otherwise print “failed”.
C#
sol//
using System;
class Program
{
static void Main(string[] args)
{
// Reading student's information
Console.WriteLine("Enter student's name:");
string name = Console.ReadLine();
Console.WriteLine("Enter student's gender (M/F):");
char gender = char.ToUpper(Console.ReadLine()[0]);
Console.WriteLine("Enter score 1:");
double score1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter score 2:");
double score2 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter score 3:");
double score3 = Convert.ToDouble(Console.ReadLine());
// Calculating average score
double average = (score1 + score2 + score3) / 3;
// Determining pass or fail
string result = (average > 50) ? "pass" : "failed";
// Printing result
Console.WriteLine($"Student: {name}");
Console.WriteLine($"Gender: {gender}");
Console.WriteLine($"Average score: {average:F2}");
Console.WriteLine($"Result: {result}");
}
}
Output:
Enter student's name:
Ahmed
Enter student's gender (M/F):
M
Enter score 1:
75
Enter score 2:
60
Enter score 3:
85
Student: Ahmed
Gender: M
Average score: 73.33
Result: pass
C++
sol//
#include <iostream>
#include <string>
using namespace std;
int main() {
// Declare variables
string name;
char gender;
double score1, score2, score3, average;
// Read student's information
cout << "Enter student's name: ";
getline(cin, name);
cout << "Enter student's gender (M/F): ";
cin >> gender;
cout << "Enter score 1: ";
cin >> score1;
cout << "Enter score 2: ";
cin >> score2;
cout << "Enter score 3: ";
cin >> score3;
// Calculate average score
average = (score1 + score2 + score3) / 3.0;
// Determine pass or fail
string result = (average > 50) ? "pass" : "failed";
// Print result
cout << "Student: " << name << endl;
cout << "Gender: " << gender << endl;
cout << "Average score: " << average << endl;
cout << "Result: " << result << endl;
return 0;
}
output:
Enter student's name: John Doe
Enter student's gender (M/F): M
Enter score 1: 85
Enter score 2: 70
Enter score 3: 65
Student: John Doe
Gender: M
Average score: 73.3333
Result: pass
C
#include <stdio.h>
int main() {
// Declare variables
char name[50];
char gender;
float score1, score2, score3, average;
// Read student's information
printf("Enter student's name: ");
scanf("%s", name);
printf("Enter student's gender (M/F): ");
scanf(" %c", &gender);
printf("Enter score 1: ");
scanf("%f", &score1);
printf("Enter score 2: ");
scanf("%f", &score2);
printf("Enter score 3: ");
scanf("%f", &score3);
// Calculate average score
average = (score1 + score2 + score3) / 3.0;
// Determine pass or fail
char* result = (average > 50) ? "pass" : "failed";
// Print result
printf("Student: %s\n", name);
printf("Gender: %c\n", gender);
printf("Average score: %.2f\n", average);
printf("Result: %s\n", result);
return 0;
}
output:
Enter student's name: Alice
Enter student's gender (M/F): F
Enter score 1: 70
Enter score 2: 65
Enter score 3: 80
Student: Alice
Gender: F
Average score: 71.67
Result: pass
Python
def main():
# Read student's information
name = input("Enter student's name: ")
gender = input("Enter student's gender (M/F): ")
score1 = float(input("Enter score 1: "))
score2 = float(input("Enter score 2: "))
score3 = float(input("Enter score 3: "))
# Calculate average score
average = (score1 + score2 + score3) / 3
# Determine pass or fail
result = "pass" if average > 50 else "failed"
# Print result
print("Student:", name)
print("Gender:", gender)
print("Average score:", "{:.2f}".format(average))
print("Result:", result)
if __name__ == "__main__":
main()
output:
Enter student's name: John
Enter student's gender (M/F): M
Enter score 1: 75
Enter score 2: 60
Enter score 3: 85
Student: John
Gender: M
Average score: 73.33
Result: pass
Java
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Create Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Read student's information
System.out.print("Enter student's name: ");
String name = scanner.nextLine();
System.out.print("Enter student's gender (M/F): ");
char gender = scanner.next().charAt(0);
System.out.print("Enter score 1: ");
double score1 = scanner.nextDouble();
System.out.print("Enter score 2: ");
double score2 = scanner.nextDouble();
System.out.print("Enter score 3: ");
double score3 = scanner.nextDouble();
// Calculate average score
double average = (score1 + score2 + score3) / 3;
// Determine pass or fail
String result = (average > 50) ? "pass" : "failed";
// Print result
System.out.println("Student: " + name);
System.out.println("Gender: " + gender);
System.out.println("Average score: " + String.format("%.2f", average));
System.out.println("Result: " + result);
// Close the Scanner
scanner.close();
}
}
output:
Enter student's name: Alice
Enter student's gender (M/F): F
Enter score 1: 70
Enter score 2: 65
Enter score 3: 80
Student: Alice
Gender: F
Average score: 71.67
Result: pass
PHP
<?php
// Read student's information
$name = readline("Enter student's name: ");
$gender = readline("Enter student's gender (M/F): ");
$score1 = floatval(readline("Enter score 1: "));
$score2 = floatval(readline("Enter score 2: "));
$score3 = floatval(readline("Enter score 3: "));
// Calculate average score
$average = ($score1 + $score2 + $score3) / 3;
// Determine pass or fail
$result = ($average > 50) ? "pass" : "failed";
// Print result
echo "Student: $name\n";
echo "Gender: $gender\n";
echo "Average score: " . number_format($average, 2) . "\n";
echo "Result: $result\n";
?>
output:
Enter student's name: John
Enter student's gender (M/F): M
Enter score 1: 75
Enter score 2: 60
Enter score 3: 85
Student: John
Gender: M
Average score: 73.33
Result: pass
Swift
import Foundation
// Function to read input from user
func readInput() -> String {
guard let input = readLine() else {
fatalError("Unexpected end of input")
}
return input
}
// Function to read a float input from user
func readFloatInput() -> Float {
guard let input = readLine(), let floatInput = Float(input) else {
fatalError("Invalid input")
}
return floatInput
}
// Read student's information
print("Enter student's name:")
let name = readInput()
print("Enter student's gender (M/F):")
let gender = readInput()
print("Enter score 1:")
let score1 = readFloatInput()
print("Enter score 2:")
let score2 = readFloatInput()
print("Enter score 3:")
let score3 = readFloatInput()
// Calculate average score
let average = (score1 + score2 + score3) / 3
// Determine pass or fail
let result = average > 50 ? "pass" : "failed"
// Print result
print("Student: \(name)")
print("Gender: \(gender)")
print("Average score: \(String(format: "%.2f", average))")
print("Result: \(result)")
output :
Enter student's name:
John
Enter student's gender (M/F):
M
Enter score 1:
75
Enter score 2:
60
Enter score 3:
85
Student: John
Gender: M
Average score: 73.33
Result: pass
VB.net
Imports System
Module Program
Sub Main()
' Read student's information
Console.Write("Enter student's name: ")
Dim name As String = Console.ReadLine()
Console.Write("Enter student's gender (M/F): ")
Dim gender As Char = Console.ReadLine()(0)
Console.Write("Enter score 1: ")
Dim score1 As Double = Convert.ToDouble(Console.ReadLine())
Console.Write("Enter score 2: ")
Dim score2 As Double = Convert.ToDouble(Console.ReadLine())
Console.Write("Enter score 3: ")
Dim score3 As Double = Convert.ToDouble(Console.ReadLine())
' Calculate average score
Dim average As Double = (score1 + score2 + score3) / 3
' Determine pass or fail
Dim result As String = If(average > 50, "pass", "failed")
' Print result
Console.WriteLine("Student: " & name)
Console.WriteLine("Gender: " & gender)
Console.WriteLine("Average score: " & average.ToString("F2"))
Console.WriteLine("Result: " & result)
End Sub
End Module
output:
Enter student's name:
John
Enter student's gender (M/F):
M
Enter score 1:
75
Enter score 2:
60
Enter score 3:
85
Student: John
Gender: M
Average score: 73.33
Result: pass