Name – DEEPAK KUMAR
Branch – CSE ( CYBER SECURITY)
Roll no – W22554
Q1. Write a C++ program to print Hello World.
Ans:-
Q2. Write a C++ program to input marks of five subjects of a student and
calculate total marks and percentage
Ans:-
Output:-
#include <iostream>
int main() {
std::cout << "Hello World!";
return 0;
}
#include <iostream>
using namespace std;
int main() {
float m, p, c, e, h, total, percentage;
cout << "Enter the marks of five subjects: ";
cin >> m >> p >> c >> e >> h;
total = m + p + c + e + h;
percentage = (total / 500) * 100;
cout << "Total Marks: " << total << endl;
cout << "Percentage: " << percentage << "%" << endl;
return 0;
}
Enter the marks of five subjects: 87 80 89 90 90
Total Marks: 436
Percentage: 87.2%
Q3. Write a C++ program to check the number is Armstrong or not. (For any
digits armstrong number)
Ans:-
Output:-
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int num, originalNum, remainder, n = 0, result = 0;
cout << "Enter an integer: ";
cin >> num;
originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
++n;
}
originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}
if (result == num)
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";
return 0;
}
Enter an integer: 153
153 is an Armstrong number.
Q4. Write the basic structure of C++ and principle of OOPs.
Ans:-
Q5. Explain the token in C++.
Ans:-
I. The basic structure of a C++ program consists of a few key components:
• Header files: These files contain the declarations of functions and variables that are used
in the program. They are included at the beginning of the program using
the #include directive.
• Main function: This is the entry point of the program. It is where the program starts
executing. The main function is defined using the int main() statement.
• Statements: These are the instructions that the program executes. They can be simple
statements like cout << "Hello World!"; or more complex statements like loops and
conditional statements.
• Comments: These are notes that are added to the program to explain what the code is
doing. They are ignored by the compiler and are used to make the code more readable.
II. Object-oriented programming (OOP) is a programming paradigm that is based on the
concept of “objects”. In OOP, objects are instances of classes, which are user-defined
data types that encapsulate data and functions that operate on that data. The four basic
principles of OOP are:
• Encapsulation: This is the practice of hiding the implementation details of a class from
the outside world. It allows the class to change its implementation without affecting the
code that uses it.
• Inheritance: This is the process of creating a new class from an existing class. The new
class inherits the properties and methods of the existing class and can add new properties
and methods of its own.
• Polymorphism: This is the ability of objects of different classes to be treated as if they
were objects of the same class. It allows for more flexible and modular code.
• Abstraction: This is the process of identifying the essential features of an object and
ignoring the details that are not relevant to the current context. It allows for more efficient
and effective code.
In C++, a token is the smallest building block of a program that the compiler understands. Every
word in a C++ source code can be considered a token. There are several types of tokens in C++,
each of which serves a specific purpose in the syntax and semantics of the language. Here are
the main types of tokens in C++:
1. Identifiers: These are unique names given to entities like variables, functions, classes,
or structs within a program so that they can be uniquely identified. An identifier can only
begin with a letter or an underscore (), and can consist of letters (A-Z or a-z), digits (0-
9), and underscores ().
2. Keywords: These are the reserved words in programming languages that have their
specific meaning and functionalities within a program. Keywords cannot be used as an
identifier to name any variables.
3. Constants: These are the tokens in C++ that are used to define variables at the time of
initialization and the assigned value cannot be changed after that.
4. Strings: These are a sequence of characters enclosed in double quotes.
5. Special Symbols: These include special characters like brackets, braces, semicolons,
commas, etc.
6. Operators: These are used to perform operations on variables and values.

Deepakkumarassignmentnumberdiles1 01.pdf

  • 1.
    Name – DEEPAKKUMAR Branch – CSE ( CYBER SECURITY) Roll no – W22554 Q1. Write a C++ program to print Hello World. Ans:- Q2. Write a C++ program to input marks of five subjects of a student and calculate total marks and percentage Ans:- Output:- #include <iostream> int main() { std::cout << "Hello World!"; return 0; } #include <iostream> using namespace std; int main() { float m, p, c, e, h, total, percentage; cout << "Enter the marks of five subjects: "; cin >> m >> p >> c >> e >> h; total = m + p + c + e + h; percentage = (total / 500) * 100; cout << "Total Marks: " << total << endl; cout << "Percentage: " << percentage << "%" << endl; return 0; } Enter the marks of five subjects: 87 80 89 90 90 Total Marks: 436 Percentage: 87.2%
  • 2.
    Q3. Write aC++ program to check the number is Armstrong or not. (For any digits armstrong number) Ans:- Output:- #include <iostream> #include <cmath> using namespace std; int main() { int num, originalNum, remainder, n = 0, result = 0; cout << "Enter an integer: "; cin >> num; originalNum = num; while (originalNum != 0) { originalNum /= 10; ++n; } originalNum = num; while (originalNum != 0) { remainder = originalNum % 10; result += pow(remainder, n); originalNum /= 10; } if (result == num) cout << num << " is an Armstrong number."; else cout << num << " is not an Armstrong number."; return 0; } Enter an integer: 153 153 is an Armstrong number.
  • 3.
    Q4. Write thebasic structure of C++ and principle of OOPs. Ans:- Q5. Explain the token in C++. Ans:- I. The basic structure of a C++ program consists of a few key components: • Header files: These files contain the declarations of functions and variables that are used in the program. They are included at the beginning of the program using the #include directive. • Main function: This is the entry point of the program. It is where the program starts executing. The main function is defined using the int main() statement. • Statements: These are the instructions that the program executes. They can be simple statements like cout << "Hello World!"; or more complex statements like loops and conditional statements. • Comments: These are notes that are added to the program to explain what the code is doing. They are ignored by the compiler and are used to make the code more readable. II. Object-oriented programming (OOP) is a programming paradigm that is based on the concept of “objects”. In OOP, objects are instances of classes, which are user-defined data types that encapsulate data and functions that operate on that data. The four basic principles of OOP are: • Encapsulation: This is the practice of hiding the implementation details of a class from the outside world. It allows the class to change its implementation without affecting the code that uses it. • Inheritance: This is the process of creating a new class from an existing class. The new class inherits the properties and methods of the existing class and can add new properties and methods of its own. • Polymorphism: This is the ability of objects of different classes to be treated as if they were objects of the same class. It allows for more flexible and modular code. • Abstraction: This is the process of identifying the essential features of an object and ignoring the details that are not relevant to the current context. It allows for more efficient and effective code. In C++, a token is the smallest building block of a program that the compiler understands. Every word in a C++ source code can be considered a token. There are several types of tokens in C++, each of which serves a specific purpose in the syntax and semantics of the language. Here are the main types of tokens in C++: 1. Identifiers: These are unique names given to entities like variables, functions, classes, or structs within a program so that they can be uniquely identified. An identifier can only begin with a letter or an underscore (), and can consist of letters (A-Z or a-z), digits (0- 9), and underscores (). 2. Keywords: These are the reserved words in programming languages that have their specific meaning and functionalities within a program. Keywords cannot be used as an identifier to name any variables. 3. Constants: These are the tokens in C++ that are used to define variables at the time of initialization and the assigned value cannot be changed after that. 4. Strings: These are a sequence of characters enclosed in double quotes. 5. Special Symbols: These include special characters like brackets, braces, semicolons, commas, etc. 6. Operators: These are used to perform operations on variables and values.