0% found this document useful (0 votes)
14 views13 pages

C++ Algorithm Ans

The document contains a series of algorithms for various programming tasks, including checking if a number is even or odd, using string functions, handling structures and unions, reading from files, and manipulating arrays. Each algorithm is structured with clear steps from start to end, detailing input handling, processing, and output. The tasks cover fundamental programming concepts such as pointers, dynamic memory allocation, and command line arguments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views13 pages

C++ Algorithm Ans

The document contains a series of algorithms for various programming tasks, including checking if a number is even or odd, using string functions, handling structures and unions, reading from files, and manipulating arrays. Each algorithm is structured with clear steps from start to end, detailing input handling, processing, and output. The tasks cover fundamental programming concepts such as pointers, dynamic memory allocation, and command line arguments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

slip 1)

Q-Write an algorithm to accept an integer using pointer and check whether it is


even or odd
ans =

Algorithm:

1. Start
2. Declare an integer variable num to store the input number.
3. Declare a pointer ptr to point to the integer variable num.
4. Accept input from the user and store it in num using the pointer ptr.
5. Check if the value pointed by ptr is divisible by 2:
o If *ptr % 2 == 0, go to step 6.
o Else, go to step 7.
6. Display "Even" as the number is even.
7. Display "Odd" as the number is odd.
8. End.

slip 2)
Q-Write an algorithm to use library function of string strlen ()

Ans=

Algorithm:

1. Start
2. Declare a string variable to store the input string.
3. Input the string from the user.
4. Call the library function strlen() to calculate the length of the string and store the
result in an integer variable length.
5. Display the length of the string.
6. End

slip 3)
Q-Write an algorithm to use library function of string Concatenation of two string.

Ans=

Algorithm:

1. Start
2. Declare two string variables: str1 and str2 to store the input strings.
3. Input the first string (str1) from the user.
4. Input the second string (str2) from the user.
5. Call the library function strcat() to concatenate str2 to str1.
o Use the syntax: strcat(str1, str2);
6. Display the concatenated result (the content of str1 after concatenation).
7. End

slip 4)
Q-Write an algorithm to declared and access the member of structure.
Ans =

Algorithm:

1. Start
2. Declare a structure with relevant members.
o Define the structure with the necessary data types and member names.
3. Declare a variable of the structure type.
o Create an instance of the structure to store data.
4. Access and assign values to the members of the structure using the dot operator (.).
o Assign values to the structure members individually.
5. Access and display the values of the structure members using the dot operator (.).
6. End

slip 5)
Q-Write an algorithm to find the sum of N integer numbers using command
line arguments
Ans=

Algorithm:

1. Start
2. Declare an integer variable sum and initialize it to 0 to store the sum of the numbers.
3. Accept the number of integers N from the command-line arguments.
o The first argument argv[0] is the program name, so the numbers begin from
argv[1] to argv[N].
4. Loop through the command-line arguments starting from index 1 to N:
o Convert each command-line argument from string to integer using atoi() or
strtol().
o Add each integer to the sum.
5. Display the sum.
6. End

slip 6)
Q-Write an algorithm to display vowels from given string.
Ans =

Algorithm:

1. Start
2. Declare a string variable to store the input string.
3. Input the string from the user.
4. Loop through each character of the string:
o For each character, check if it is a vowel ('a', 'e', 'i', 'o', 'u') or its uppercase
versions ('A', 'E', 'I', 'O', 'U').
5. If the character is a vowel, display it.
6. End

slip 7)
Q - an algorithm toread the number from a text file

Ans=

Algorithm:

1. Start
2. Declare a file pointer variable to handle the file.
3. Open the text file in read mode using fopen().
o If the file cannot be opened, display an error message and terminate the program.
4. Check if the file is open:
o If the file is open successfully, proceed to the next step.
5. Read the numbers from the file one by one using fscanf() or fgets() depending on
the format of the file.
o Store the read numbers in an array or process them as required.
6. Display the numbers or process them as needed.
7. Close the file using fclose() after reading the numbers.
8. End

slip 8)
Q-Write an algorithm to display addition of 2 numbers using commandline
Arguments.
Ans=
Algorithm:

1. Start
2. Check the number of command-line arguments (argc):
o Ensure there are at least 3 arguments: the program name and the two numbers to
be added.
3. Convert the command-line arguments (argv[1] and argv[2]) from string to integer
using atoi() or strtol().
4. Add the two numbers.
5. Display the sum.
6. End

slip 9)
Q-Write an algorithm to compare 2 strings using built in function.
Ans=

Algorithm:

1. Start
2. Declare two string variables to store the input strings.
3. Input the two strings from the user.
4. Use the built-in function strcmp() to compare the two strings:
o If strcmp(str1, str2) == 0, the strings are equal.
o If strcmp(str1, str2) > 0, str1 is greater than str2.
o If strcmp(str1, str2) < 0, str1 is smaller than str2.
5. Display the result of the comparison.
6. End

slip 10)
Q-Write an algorithm to accept radius and display area and perimeter using pointer
Ans=

Algorithm:

1. Start
2. Declare the necessary variables:
o Declare a float variable radius to store the radius of the circle.
o Declare a pointer ptr to store the address of the radius.
3. Accept the value of radius from the user.
4. Store the address of radius in the pointer ptr.
5. Calculate the area using the formula:
o Area = π×radius2\pi \times \text{radius}^2π×radius2
6. Calculate the perimeter (circumference) using the formula:
o Perimeter (Circumference) = 2×π×radius2 \times \pi \times \
text{radius}2×π×radius
7. Display the area and perimeter using the pointer to access the radius.
8. End

slip 11)
Q-Write an algorithm toaccept n numbers and display the array in the reverse order.

Ans =

Algorithm:

1. Start
2. Declare an integer array arr[] of size n to store the numbers.
3. Declare an integer variable n to store the number of elements.
4. Accept the value of n (number of elements in the array) from the user.
5. Accept n numbers from the user and store them in the array arr[].
6. Loop through the array in reverse order:
o Start from the last element and move towards the first element.
7. Display the numbers in reverse order as you loop through the array.
8. End

slip 12)
Q-Write an algorithm to read and display array elements using dynamic memory allocation
Ans=

Algorithm:

1. Start
2. Declare a pointer variable to hold the base address of the dynamically allocated
memory for the array.
3. Accept the number of elements n (size of the array) from the user.
4. Dynamically allocate memory for the array using malloc() or calloc().
o Ensure the memory is successfully allocated; if not, display an error message and
terminate.
5. Accept the array elements from the user and store them in the dynamically allocated
memory.
6. Display the array elements.
7. Free the dynamically allocated memory using free() to prevent memory leaks.
8. End

slip 13)
Q-Write an algorithm todisplay the string using pointer
Ans=

Algorithm:

1. Start
2. Declare a string (character array) to store the input string.
3. Declare a pointer that will point to the first character of the string.
4. Input the string from the user.
5. Assign the address of the string to the pointer.
6. Use a loop to traverse the string using the pointer, and display each character until the
null character (\0) is encountered.
7. End

slip 14)
Q-Write an algorithm tocreate a structure hospital ,use the typedef to display the details.
Ans=

Algorithm:

1. Start
2. Define the structure:
o Use typedef to define a structure called Hospital.
o Inside the structure, define the following members:
 name: To store the hospital's name.
 address: To store the hospital's address.
 num_doctors: To store the number of doctors in the hospital.
 num_patients: To store the number of patients.
3. Declare a variable of type Hospital to store the hospital's details.
4. Accept input for the structure members from the user (hospital name, address, number
of doctors, number of patients).
5. Display the details of the hospital stored in the structure.
6. End

slip 15)
Q-Write an algorithm to display book information using union.
Ans=

Algorithm to Display Book Information Using Union:

1. Start
2. Define a union called Book:
o Member 1: title (character array) to store the book's title.
o Member 2: author (character array) to store the author's name.
o Member 3: price (float) to store the price of the book.
3. Declare a variable of type Book to store the book information.
4. Accept the book details from the user:
o Accept the book title and store it in the title member.
o Accept the author's name and store it in the author member.
o Accept the book price and store it in the price member.
5. Display the book details:
o Display the value stored in the title member.
o Display the value stored in the author member.
o Display the value stored in the price member.
6. End

slip 16)
Q-Write an algorithm tocheck whether given string is palindrome or Not
Ans=

Algorithm to Check Whether a Given String is a Palindrome or Not:

1. Start
2. Declare a string to store the input string.
3. Accept the string from the user.
4. Initialize two pointers:
o One pointer i at the start of the string (index 0).
o Another pointer j at the end of the string (index length - 1).
5. Loop while i < j:
o If string[i] is not equal to string[j], then the string is not a palindrome.
Print "Not a palindrome" and exit.
o If string[i] is equal to string[j], increment i and decrement j to check the
next pair of characters.
6. If the loop completes without finding unequal characters, print "Palindrome".
7. End

slip 17)
Q-Write an algorithm toprint given string in reverse order using user defined function
.
Ans=

Algorithm to Print a Given String in Reverse Order Using a User-Defined


Function:

1. Start
2. Define a user-defined function reverse_string(char str[]):
o This function will take a string as an argument and print it in reverse order.
o Inside the function, use a loop to print the string characters from the end to the
start.
3. Declare a string to store the input string.
4. Accept the string from the user.
5. Call the function reverse_string(str) to print the string in reverse order.
6. End

slip 18)
Q-Write an algorithm tocount the total number of words in the sentence using
Command line argument.
.
Ans=

Algorithm to Count the Total Number of Words in a Sentence Using Command


Line Arguments:

1. Start
2. Accept command line arguments:
o The first command line argument will be the total number of arguments
(including the program name).
o The remaining command line arguments will be the words in the sentence.
3. Initialize a counter to zero (to count the words).
4. Loop through the command line arguments (starting from the second argument):
o For each argument, check if it is a non-empty string (i.e., it is a word).
o Increment the word counter for each valid word.
5. Display the word count.
6. End

slip 19)
Q-Write an algorithm to sum of ‘n’ numbers using pointers.
Ans=

Algorithm to Calculate the Sum of 'n' Numbers Using Pointers:

1. Start
2. Declare necessary variables:
o n: To store the number of elements.
o sum: To store the sum of the numbers.
o A pointer to store the address of the array.
3. Accept the value of n from the user, which represents how many numbers will be added.
4. Dynamically allocate memory for an array of n integers using malloc() or calloc().
5. Accept the n numbers from the user and store them in the dynamically allocated array.
6. Initialize a pointer to the array's first element.
7. Use a loop to iterate through the array using the pointer and sum the numbers.
o In each iteration, dereference the pointer to get the value at that position and add it
to sum.
8. Display the sum.
9. Free the dynamically allocated memory.
10. End

slip 20)
Q-Write an algorithm to display the reverse of the number using pointer.
Ans=

Algorithm to Display the Reverse of a Number Using a Pointer:

1. Start
2. Declare the necessary variables:
o num: An integer to store the number entered by the user.
o reversed: An integer to store the reversed number.
o A pointer ptr to store the address of num.
3. Accept the number from the user.
4. Initialize the pointer ptr to point to the number num.
5. Reverse the number:
o Set reversed to 0 initially.
o While the number is not zero:
 Get the last digit of the number using num % 10.
 Multiply reversed by 10 and add the last digit.
 Remove the last digit from num by dividing it by 10.
6. Display the reversed number.
7. End

slip 21)
Q-Write an algorithm to read and display array elements using dynamic memory
Allocation
Ans=

Algorithm to Read and Display Array Elements Using Dynamic Memory


Allocation:

1. Start
2. Declare necessary variables:
o n: An integer to store the number of elements.
o A pointer arr to store the dynamically allocated array.
3. Accept the value of n from the user, which represents how many elements will be in the
array.
4. Dynamically allocate memory for the array of n integers using malloc() or calloc().
5. Check if the memory allocation is successful:
o If the memory allocation fails (i.e., arr is NULL), display an error message and
terminate the program.
6. Accept the elements of the array from the user.
7. Display the array elements using a loop and pointer dereferencing.
8. Free the dynamically allocated memory.
9. End

slip 22)
Q-Write an algorithm to find the sum of N integer numbers using command
line arguments
Ans=

Algorithm to Find the Sum of N Integer Numbers Using Command Line


Arguments:

1. Start
2. Accept the number of integers (N) from the command line as arguments.
o The first argument (argv[0]) will be the program name.
o The remaining arguments (argv[1] to argv[N]) will be the integers to sum.
3. Initialize a variable sum to 0.
4. Loop through the command line arguments (from argv[1] to argv[N]):
o Convert each argument to an integer using atoi() or strtol().
o Add the integer to the sum variable.
5. Display the sum.
6. End

slip 23)
Q-Write an algorithm to use library function of string Concatenation of two string.

Ans=

Algorithm to Concatenate Two Strings Using Library Function:

1. Start
2. Declare two strings str1 and str2 to store the input strings.
3. Accept the input for str1 and str2 from the user.
4. Use the library function strcat() to concatenate str2 to the end of str1.
o strcat(str1, str2) appends the content of str2 to str1.
5. Display the concatenated string.
6. End

slip 24)
Q-Write an algorithm to accept an integer using pointer and check whether it is

even or odd

Ans=

Algorithm to Accept an Integer Using Pointer and Check Whether it is Even or


Odd:

1. Start
2. Declare an integer variable num to store the input number.
3. Declare a pointer ptr to store the address of num.
4. Accept the value of num from the user.
5. Initialize the pointer ptr to point to num.
6. Check the value using the pointer:
o If the value pointed to by ptr is divisible by 2 (i.e., *ptr % 2 == 0), then the
number is even.
o Otherwise, the number is odd.
7. Display whether the number is even or odd based on the result from step 6.
8. End

slip 25)
Q-Write an algorithm toaccept radius and display area and perimeter using pointer.
Ans=

Algorithm to Accept Radius and Display Area and Perimeter Using Pointer:

1. Start
2. Declare necessary variables:
o radius: A float variable to store the radius of the circle.
o area: A float variable to store the area of the circle.
o perimeter: A float variable to store the perimeter of the circle.
o A pointer ptr_radius to store the address of radius.
3. Accept the value of radius from the user.
4. Initialize the pointer ptr_radius to point to radius.
5. Calculate the area and perimeter:
o Area of circle: area = π * (*ptr_radius) * (*ptr_radius) where π ≈
3.14159.
o Perimeter (Circumference) of circle: perimeter = 2 * π * (*ptr_radius).
6. Display the area and perimeter using the calculated values.
7. End

You might also like