Given marks of a student, print on the screen:
For printing use:-
Please point to new line after you print the output.
Input: marks = 95
Output: Grade A
Explanation: marks are greater than or equal to 90.
Input: marks = 14
Output: Fail
Explanation: marks are less than 35.
Input: marks = 70
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to print the grades based on marks
void studentGrade(int marks) {
// If else ladder
if (marks >= 90) {
cout << "Grade A";
} else if (marks >= 70) {
cout << "Grade B";
} else if (marks >= 50) {
cout << "Grade C";
} else if (marks >= 35) {
cout << "Grade D";
} else {
cout << "Fail";
}
}
};
int main() {
// Creating an instance of Solution class
Solution solution;
int marks;
// Taking marks as input from user
cout << "Enter your marks: ";
cin >> marks;
// Function call to print the grades based on marks
solution.studentGrade(marks);
return 0;
}import java.util.Scanner;
class Solution {
// Function to print the grades based on marks
public void studentGrade(int marks) {
// If else ladder
if (marks >= 90) {
System.out.print("Grade A");
} else if (marks >= 70) {
System.out.print("Grade B");
} else if (marks >= 50) {
System.out.print("Grade C");
} else if (marks >= 35) {
System.out.print("Grade D");
} else {
System.out.print("Fail");
}
}
}
class Main {
public static void main(String[] args) {
// Creating an instance of Solution class
Solution solution = new Solution();
int marks;
// Taking marks as input from user
Scanner sc = new Scanner(System.in);
System.out.print("Enter your marks: ");
marks = sc.nextInt();
// Function call to print the grades based on marks
solution.studentGrade(marks);
}
}class Solution:
# Function to print the grades based on marks
def studentGrade(self, marks):
# If else ladder
if marks >= 90:
print("Grade A")
elif marks >= 70:
print("Grade B")
elif marks >= 50:
print("Grade C")
elif marks >= 35:
print("Grade D")
else:
print("Fail")
if __name__ == "__main__":
# Creating an instance of Solution class
solution = Solution()
# Taking marks as input from user
marks = int(input("Enter your marks: "))
# Function call to print the grades based on marks
solution.studentGrade(marks)class Solution {
// Function to print the grades based on marks
studentGrade(marks) {
// If else ladder
if (marks >= 90) {
console.log("Grade A");
} else if (marks >= 70) {
console.log("Grade B");
} else if (marks >= 50) {
console.log("Grade C");
} else if (marks >= 35) {
console.log("Grade D");
} else {
console.log("Fail");
}
}
}
// Creating an instance of Solution class
const solution = new Solution();
// Taking marks as input from user
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter your marks: ', marks => {
// Function call to print the grades based on marks
solution.studentGrade(parseInt(marks));
readline.close();
});
Time Complexity: O(1), The operations of receiving input and printing output are executed once regardless of the size of the input.
Space Complexity: O(1), Using only a single variable to store marks, and no additional space is used that grows with the input size.
Your notes are automatically saved in your browser's local storage and will persist across sessions on this device.