Summary

This document includes C++ programs for calculating the sum of a series, the area of a circle, and the factorial of a number. It also provides explanations and examples for various concepts like control statements, bitwise operators and pointers.

Full Transcript

# C++ Programs ## D. Write a program in C++ to find the sum of series 1+3+5+... till the value of n should be accepted. ```c++ #include <iostream> using namespace std; int main() { int number; cout << "Enter a positive number: "; cin >> number; int sum = 0; for (int i = 1; i <= number;...

# C++ Programs ## D. Write a program in C++ to find the sum of series 1+3+5+... till the value of n should be accepted. ```c++ #include <iostream> using namespace std; int main() { int number; cout << "Enter a positive number: "; cin >> number; int sum = 0; for (int i = 1; i <= number; i += 2) { sum += i; } cout << "Required sum is: " << sum; return 0; } ``` **Output** ``` Enter a positive number: 8 Required sum is: 16 ``` ## 2). Write a program in C++ to find area of circle using return type function ```c++ #include <iostream.h> void area(double); // function prototype void main() { double r, a; cout << "Enter radius: "; cin >> r; area(r); // function call } // function definition void area(double r) { double a; a = 3.14 * r * r; cout << "Area of circle is : " << a; } ``` ## 3). Enlist different control statements with syntax. Later ## 4). Explain 2's complement method of subtraction with suitable examples. 2's Complement is the no that results when we add 1 to the 1's complement. It is denoted by A'. 2's Complement = (1's Complement) + 1. A' = A +1 ex. A = 111010 A = 000101 (1's complement A' = 000101 + 1 = 000110 (2's complement ## 5) Write a program in C++, to display odd Numbers between 1-100 using for loop... ```c++ #include <iostream> using namespace std; int main () { for (int i=3; i< 100; i+=2) cout <<i<<" "; return 0; } ``` **Out put** 3,5,7,9,...95,97,99 ## 6.) Explain bitwise operators in C++ Some special C++ operators can also work on the bits, So they may be called to as bit manipulation operators. They are used to manipulate or modify the individual bits of the piece of data. They can be wed with the integral built-in data | Operator | Form | Meaning | |---|---|---| | ~ | a~ | One's complement | | >> | a>>b | Right shift a by b | | << | a<<b | Left shift a by b | | & | a&b | Bitwise AND| | | | | | \| | a\|b | Bit wise OR | | ^ | a^b | Bitwise XOR | pg 49 of chem 013- ## 7) What is constant? Explain the difference between declaration and initialization of, variable. Constants are declared with the const keyword, a type, their name, and an assignment of their constant value. This value must match the declared type, and is fixed for the duration of the program. Declaration of a variable involved name of the variable & its data type. Initialization, on the other hand, involves assigning an initial value a declared variable. In summary, declaration established the existence of a variable, while initialization sets its initial value ## 8) Explain prefix and postfix operator. The incrementing and decrementing can be of two type. - **Pre Fix:** First increment/decrement the value of the variable and then take this new value for processing. eg ++9,--6 - **Postfix:** First take the value of the variable, increment or decrement the variable. Eg. att, b ## 9) Use of Prefix and Pastfix. Suppose initial values of a and bare 5 and 3 respectively. Suppose, I do post fix Operation on it ise att and b-- and print them. Here value of a and b remain unaltered while after postfix operation is done, value of a and b are printed them will be changed to 6 and 4 respectively. ## 10. Write C++ program for finding factorial of a number. The value of number should be accepted from user. ```c++ #include <iostream> using namespace std; int main() { int i, fact = 1, number; cout << "Enter any Number "; cin >> number; for (i = 1; i <= number; i++) { fact = fact * i; } cout << "Factorial of" << number << "is " << fact << endl; return 0; } ``` **Output** Enter ang Number: 5. Factorial of 5 is: 20 ## 11) What is built-in function in VB? In Visual Basic (VB), built-in functions are predefined functions provided by the language to perform common tasks. Ex include, functions for mathematical operations. (Math, sort for square root), string manipulation (Len for length), and data/time operations ('Now' for current date and time). These functions are part of the VB language and can be used without explicitly defining them. ## 12) Draw flowchart for calculation of Fibonacci series. ```mermaid graph LR A[Start] --> B{Ctr=1; a=1, b=1} B --> C{Print a} C --> D{C = a+b} D --> E{a = b, b = C} E --> F{Ctr = Ctr +1} F --> G{Ctr<=N} G -- Yes --> B G -- No --> H(Stop) ``` ## 13 Explain the difference between prefix and the postfix operator In programming, a prefix operator is placed before the operand, while a postfix operator is placed after the operand. For ex in the expression '++x' '++' is prefix operator and in 'x++' "++' is a postfix operator. Both increment have the value, but the order of the operator to the variable differs. ## 14 What are bas data type available in to Visual basic? In Visual Basic, common data types include Integer, Double, String, Boolean, B.Date, and object, among others. Each data type serves a specific purpose, allowing developers to work with different kinds of data efficiently. 1. **Integer:** Used for whole numbers, both positive and negative, without decimals. 2. **Double:** Ideal for floating-point numbers, allowing decimals and a wider range of value compared to Integer. 3. **String:** Used to store text or a combination of Characters 4. **Boolean:** Represents logical values, either True Or False 5. **Date:** Specifically designed to handle dates and times. 6. **Object:** A versatile data type that can store any type of data, including custom objects. ## 16. What are different type of data types in C++ **Data type in C++** - **Built in** - Intergal, void - Float, double - **Derived** - Pointer - Array - **User defined** - Structure - Class - Function - **Enumerations** ## 17. Explain different bitwise operators: Same as Q6 ## 18 Explain if then statement with suitable example. In Visual Basic, the If…Then statement is a powerful tool for controlling the flow of your program. It allows you to execute specific code blocks conditionally based on whether a certain condition is true or false. **Here's a basic example:** ```vb Dim number As Integer = 10 If number > 5 Then Console.WriteLine("The number is greater than 5.") Else Console.WriteLine("The number is not greater than 5.") End If ``` In this example, the `If` statement checks if the variable `number` is greater than 5. If it is, the first `Console.WriteLine` statement is executed. Otherwise, the `Else` block executes and prints a different message. ## 19) What is Pointer write its advantages A pointer is a variable that stores the memory address of another variable. Advantages of using pointers include efficient memory allocation, facilitating the implementation of data structure like linked lists and trees. Pointers also allow for direct manipulation of data in memory enhancing flexibility in programming. ## 20 What is array? Explain multidimensional array... An array is a data structure that stores elements of the same data type in a contiguous memory block. It provides a way to organize and efficiently access a collection of values using an index or a key. An array of arrays is refered to as multidimensional array. An array may have more than one dimension (i.e. two, three, or higher). The organization of the array in memory is still the same (a contiguous sequence of elements), but the programmer's perceived organization of the elements is different. eg. int two Dim [3][2] = {{1,11}, {2,12}, {3,93}}; The above declared is 2'd array that is viewed as a one-dimensional array of one-dimensional array.

Use Quizgecko on...
Browser
Browser