pf questions
pf questions
"
c
CopyEdit
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Explanation:
Question: Write a C program to take two numbers as input and print their sum.
c
CopyEdit
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
Explanation:
c
CopyEdit
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("Even\n");
else
printf("Odd\n");
return 0;
}
Explanation:
c
CopyEdit
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
return 0;
}
Explanation:
Question: Write a C program to swap two numbers without using a third variable.
c
CopyEdit
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
Explanation:
c
CopyEdit
#include <stdio.h>
int main() {
int num, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
Explanation:
7. Reverse a Number
c
CopyEdit
#include <stdio.h>
int main() {
int num, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
Explanation:
c
CopyEdit
#include <stdio.h>
int main() {
int num, i, flag = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1)
flag = 0;
if (flag)
printf("Prime\n");
else
printf("Not Prime\n");
return 0;
}
Explanation:
Loops through numbers from 2 to num/2 to check divisibility.
9. Reverse a String
c
CopyEdit
#include <stdio.h>
#include <string.h>
int main() {
char str[100], temp;
int i, len;
len = strlen(str);
for (i = 0; i < len / 2; i++) {
temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
Explanation:
c
CopyEdit
#include <stdio.h>
int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
Explanation:
These questions cover basic C concepts such as input/output, loops, conditionals, arrays, and
functions. Let me know if you need more! 🚀
4o
Here are 10 questions with explanations focused on the while loop and do-while loop in C.
#include <stdio.h>
int main() {
int n, i = 1;
printf("Enter a number: ");
scanf("%d", &n);
while (i <= n) {
printf("%d ", i);
i++;
}
return 0;
}
Explanation:
Question: Write a C program to print even numbers from 1 to N using a while loop.
#include <stdio.h>
int main() {
int n, i = 2;
printf("Enter a number: ");
scanf("%d", &n);
while (i <= n) {
printf("%d ", i);
i += 2;
}
return 0;
}
Explanation:
Question: Write a C program to find the sum of first N natural numbers using a while loop.
#include <stdio.h>
int main() {
int n, sum = 0, i = 1;
printf("Enter a number: ");
scanf("%d", &n);
while (i <= n) {
sum += i;
i++;
}
Explanation:
#include <stdio.h>
int main() {
int num, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
Explanation:
Question: Write a C program to find the factorial of a number using a while loop.
#include <stdio.h>
int main() {
int num, fact = 1, i = 1;
printf("Enter a number: ");
scanf("%d", &num);
Explanation:
Question: Write a C program to find the sum of digits of a number using a while loop.
#include <stdio.h>
int main() {
int num, sum = 0, rem;
printf("Enter a number: ");
scanf("%d", &num);
while (num != 0) {
rem = num % 10;
sum += rem;
num /= 10;
}
Explanation:
Question: Write a C program to print the multiplication table of a number using a do-while loop.
#include <stdio.h>
int main() {
int num, i = 1;
printf("Enter a number: ");
scanf("%d", &num);
do {
printf("%d x %d = %d\n", num, i, num * i);
i++;
} while (i <= 10);
return 0;
}
Explanation:
#include <stdio.h>
int main() {
int num, original, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
do {
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
} while (num != 0);
if (original == rev)
printf("Palindrome\n");
else
printf("Not a palindrome\n");
return 0;
}
Explanation:
Uses do-while to reverse the number and compare with the original.
Question: Write a C program to count the number of digits in a number using a do-while loop.
#include <stdio.h>
int main() {
int num, count = 0;
printf("Enter a number: ");
scanf("%d", &num);
do {
num /= 10;
count++;
} while (num != 0);
Removes digits one by one using / 10, ensuring at least one iteration.
10. Keep Taking Input Until Negative Number is Entered (do-while loop)
Question: Write a C program to keep taking numbers as input until the user enters a negative
number using a do-while loop.
#include <stdio.h>
int main() {
int num;
do {
printf("Enter a number (negative to stop): ");
scanf("%d", &num);
if (num >= 0)
printf("You entered: %d\n", num);
printf("Program terminated.\n");
return 0;
}
Explanation:
These questions focus on the while and do-while loops, covering common use cases like
iteration, input handling, and number manipulation. Let me know if you need more! 🚀
Question: Write a C program that takes two numbers and an operator (+, -, *, /) and performs
the operation using a switch statement.
#include <stdio.h>
int main() {
char op;
double num1, num2;
switch (op) {
case '+':
printf("Result: %.2lf\n", num1 + num2);
break;
case '-':
printf("Result: %.2lf\n", num1 - num2);
break;
case '*':
printf("Result: %.2lf\n", num1 * num2);
break;
case '/':
if (num2 != 0)
printf("Result: %.2lf\n", num1 / num2);
else
printf("Error: Division by zero\n");
break;
default:
printf("Invalid operator\n");
}
return 0;
}
Explanation:
Question: Write a C program to check if a character is a vowel or consonant using switch case.
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
switch (ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("Vowel\n");
break;
default:
printf("Consonant\n");
}
return 0;
}
Explanation:
Question: Write a C program to print the weekday name based on a number (1-7).
#include <stdio.h>
int main() {
int day;
printf("Enter a number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
case 4: printf("Thursday\n"); break;
case 5: printf("Friday\n"); break;
case 6: printf("Saturday\n"); break;
case 7: printf("Sunday\n"); break;
default: printf("Invalid input\n");
}
return 0;
}
Explanation:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
switch (num % 2) {
case 0:
printf("Even\n");
break;
case 1:
printf("Odd\n");
break;
}
return 0;
}
Explanation:
Question: Write a C program to input a month number (1-12) and display the number of days in
that month.
#include <stdio.h>
int main() {
int month;
printf("Enter month number (1-12): ");
scanf("%d", &month);
switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
printf("31 days\n");
break;
case 4: case 6: case 9: case 11:
printf("30 days\n");
break;
case 2:
printf("28 or 29 days (leap year dependent)\n");
break;
default:
printf("Invalid month number\n");
}
return 0;
}
Explanation:
Groups months based on the number of days.
#include <stdio.h>
int main() {
int num;
printf("Enter a number (1-5): ");
scanf("%d", &num);
switch (num) {
case 1: printf("I\n"); break;
case 2: printf("II\n"); break;
case 3: printf("III\n"); break;
case 4: printf("IV\n"); break;
case 5: printf("V\n"); break;
default: printf("Invalid input\n");
}
return 0;
}
Explanation:
Question: Write a C program that displays a menu and performs operations accordingly.
#include <stdio.h>
int main() {
int choice;
printf("1. Hello\n2. Goodbye\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Hello, User!\n");
break;
case 2:
printf("Goodbye, User!\n");
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
return 0;
}
Explanation:
#include <stdio.h>
int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);
return 0;
}
Explanation:
Question: Write a C program to convert temperature based on user input (C for Celsius to
Fahrenheit, F for Fahrenheit to Celsius).
#include <stdio.h>
int main() {
char scale;
float temp;
switch (scale) {
case 'C':
case 'c':
printf("Fahrenheit: %.2f\n", (temp * 9 / 5) + 32);
break;
case 'F':
case 'f':
printf("Celsius: %.2f\n", (temp - 32) * 5 / 9);
break;
default:
printf("Invalid input\n");
}
return 0;
}
Explanation:
Question: Write a C program that simulates a traffic light system (R for Red, Y for Yellow, G for
Green).
#include <stdio.h>
int main() {
char light;
printf("Enter traffic light color (R/Y/G): ");
scanf(" %c", &light);
switch (light) {
case 'R': case 'r': printf("STOP\n"); break;
case 'Y': case 'y': printf("WAIT\n"); break;
case 'G': case 'g': printf("GO\n"); break;
default: printf("Invalid input\n");
}
return 0;
}
Explanation:
These switch case questions cover basic decision-making scenarios in C. Let me know if you
need more! 🚀
Here are 10 questions on the for loop in C, along with explanations and sample code.
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
return 0;
}
Explanation:
Question: Write a C program to print even numbers from 1 to N using a for loop.
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
return 0;
}
Explanation:
Question: Write a C program to calculate the sum of the first N natural numbers using a for
loop.
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a number: ");
scanf("%d", &n);
Explanation:
4. Factorial of a Number
Question: Write a C program to find the factorial of a number using a for loop.
#include <stdio.h>
int main() {
int num, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
Explanation:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
return 0;
}
Explanation:
Question: Write a C program to print the multiplication table of a number using a for loop.
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
return 0;
}
Explanation:
Question: Write a C program to find the sum of digits of a number using a for loop.
#include <stdio.h>
int main() {
int num, sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
Explanation:
Question: Write a C program to print the first N terms of the Fibonacci series using a for loop.
#include <stdio.h>
int main() {
int n, a = 0, b = 1, next;
printf("Enter number of terms: ");
scanf("%d", &n);
Explanation:
Question: Write a C program to check whether a number is prime using a for loop.
#include <stdio.h>
int main() {
int num, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 2) {
isPrime = 0;
} else {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime)
printf("Prime\n");
else
printf("Not Prime\n");
return 0;
}
Explanation:
#include <stdio.h>
int main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
return 0;
}
Explanation:
Summary
Here are 10 pattern printing questions using C, along with their expected outputs.
*
**
***
****
*****
C Code:
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Explanation:
*****
****
***
**
*
C Code:
#include <stdio.h>
int main() {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Explanation:
*
***
*****
*******
*********
C Code:
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++)
printf(" ");
for (int j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}
Explanation:
4. Inverted Pyramid
*********
*******
*****
***
*
C Code:
#include <stdio.h>
int main() {
int rows = 5;
for (int i = rows; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++)
printf(" ");
for (int j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}
Explanation:
5. Hollow Square
*****
* *
* *
* *
*****
C Code:
#include <stdio.h>
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == 1 || i == n || j == 1 || j == n)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Explanation:
Prints stars for the first/last row and first/last column, spaces elsewhere.
6. Number Pyramid
C Code:
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++)
printf(" ");
for (int j = 1; j <= 2 * i - 1; j++)
printf("%d", i);
printf("\n");
}
return 0;
}
Explanation:
7. Floyd’s Triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
C Code:
#include <stdio.h>
int main() {
int num = 1;
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
printf("%d ", num++);
}
printf("\n");
}
return 0;
}
Explanation:
8. Pascal’s Triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
C Code:
#include <stdio.h>
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
int main() {
int rows = 5;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows - i; j++)
printf(" ");
for (int j = 0; j <= i; j++)
printf("%d ", factorial(i) / (factorial(j) * factorial(i - j)));
printf("\n");
}
return 0;
}
Explanation:
9. Diamond Pattern
*
***
*****
*******
*********
*******
*****
***
*
C Code:
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++)
printf(" ");
for (int j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++)
printf(" ");
for (int j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}
Explanation:
A
BBB
CCCCC
DDDDDDD
EEEEEEEEE
C Code:
#include <stdio.h>
int main() {
int rows = 5;
char ch = 'A';
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++)
printf(" ");
for (int j = 1; j <= 2 * i - 1; j++)
printf("%c", ch);
ch++;
printf("\n");
}
return 0;
}
Explanation:
These 10 pattern programs cover stars, numbers, alphabets, and shapes using nested loops
in C. Let me know if you need more! 🚀
Here are 10 matrix-related questions with C++ code for simple and multidimensional
matrices.
Question: Write a C++ program to take a 2x2 matrix as input and print it.
#include <iostream>
using namespace std;
int main() {
int matrix[2][2];
return 0;
}
2. Sum of All Elements in a Matrix
Question: Write a C++ program to find the sum of all elements in a 3x3 matrix.
#include <iostream>
using namespace std;
int main() {
int matrix[3][3], sum = 0;
cout << "Sum of all elements: " << sum << endl;
return 0;
}
3. Transpose of a Matrix
#include <iostream>
using namespace std;
int main() {
int matrix[3][3], transpose[3][3];
// Compute transpose
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
transpose[j][i] = matrix[i][j];
return 0;
}
4. Addition of Two Matrices
#include <iostream>
using namespace std;
int main() {
int A[3][3], B[3][3], sum[3][3];
// Add matrices
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
sum[i][j] = A[i][j] + B[i][j];
return 0;
}
#include <iostream>
using namespace std;
int main() {
int A[2][2], B[2][2], product[2][2] = {0};
// Matrix multiplication
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 2; k++) {
product[i][j] += A[i][k] * B[k][j];
}
}
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
int matrix[3][3], maxElement;
maxElement = matrix[0][0];
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (matrix[i][j] > maxElement)
maxElement = matrix[i][j];
Question: Write a C++ program to find the sum of each row in a matrix.
#include <iostream>
using namespace std;
int main() {
int matrix[3][3];
return 0;
}
Question: Write a C++ program to find the sum of each column in a matrix.
Question: Write a C++ program to find the sum of the main diagonal elements of a square
matrix.
These 10 questions cover basic matrix operations in C++ using loops and arrays. Let me
know if you need full solutions for the remaining ones! 🚀
Here are 10 questions on arrays in C++, along with explanations and C++ code.
#include <iostream>
using namespace std;
int main() {
int arr[5], maxElement;
maxElement = arr[0];
for (int i = 1; i < 5; i++)
if (arr[i] > maxElement)
maxElement = arr[i];
#include <iostream>
using namespace std;
int main() {
int arr[5], minElement;
minElement = arr[0];
for (int i = 1; i < 5; i++)
if (arr[i] < minElement)
minElement = arr[i];
3. Reverse an Array
#include <iostream>
using namespace std;
int main() {
int arr[5];
return 0;
}
Question: Write a C++ program to calculate the sum of all elements in an array.
#include <iostream>
using namespace std;
int main() {
int arr[5], sum = 0;
Question: Write a C++ program to count even and odd numbers in an array.
#include <iostream>
using namespace std;
int main() {
int arr[5], evenCount = 0, oddCount = 0;
cout << "Even numbers: " << evenCount << ", Odd numbers: " << oddCount <<
endl;
return 0;
}
Question: Write a C++ program to copy all elements from one array to another.
#include <iostream>
using namespace std;
int main() {
int arr1[5], arr2[5];
return 0;
}
#include <iostream>
using namespace std;
int main() {
int arr[5], key, found = 0;
if (!found)
cout << "Element not found" << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int arr[5], temp;
// Bubble Sort
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return 0;
}
Question: Write a C++ program to merge two arrays into a third array.
#include <iostream>
using namespace std;
int main() {
int arr1[3], arr2[3], merged[6];
return 0;
}
Question: Write a C++ program to find the second largest element in an array.
#include <iostream>
#include <limits.h>
using namespace std;
int main() {
int arr[5], firstMax = INT_MIN, secondMax = INT_MIN;
cout << "Second largest element: " << secondMax << endl;
return 0;
}
These 10 C++ programs cover basic and intermediate operations on arrays. Let me know if
you need modifications or explanations! 🚀
Here are 10 questions on functions in C++, covering both basic and advanced topics.
Question: Write a C++ function that takes two integers as input and returns their sum.
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Sum: " << add(num1, num2) << endl;
return 0;
}
Question: Write a C++ function that takes two numbers as input and returns the maximum.
#include <iostream>
using namespace std;
int main() {
int x, y;
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Maximum: " << maxNumber(x, y) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter a number: ";
cin >> n;
checkEvenOdd(n);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factorial: " << factorial(num) << endl;
return 0;
}
#include <iostream>
using namespace std;
bool isPrime(int n) {
if (n < 2)
return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
if (isPrime(num))
cout << num << " is Prime\n";
else
cout << num << " is Not Prime\n";
return 0;
}
Question: Write a C++ program that demonstrates function overloading for adding two and
three numbers.
#include <iostream>
using namespace std;
int main() {
cout << "Sum of 2 numbers: " << add(5, 10) << endl;
cout << "Sum of 3 numbers: " << add(5, 10, 15) << endl;
return 0;
}
Question: Write a C++ recursive function to print the Fibonacci series up to a given term.
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int terms;
cout << "Enter number of terms: ";
cin >> terms;
for (int i = 0; i < terms; i++)
cout << fibonacci(i) << " ";
return 0;
}
Question: Write a C++ function to swap two numbers using call by reference.
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 10;
cout << "Before Swap: x = " << x << ", y = " << y << endl;
swap(x, y);
cout << "After Swap: x = " << x << ", y = " << y << endl;
return 0;
}
Question: Write a C++ function to compute the GCD of two numbers using recursion.
#include <iostream>
using namespace std;
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD: " << gcd(num1, num2) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int base, exp;
cout << "Enter base and exponent: ";
cin >> base >> exp;
cout << base << "^" << exp << " = " << power(base, exp) << endl;
return 0;
}
1. Basic Functions:
o Adding two numbers
o Finding maximum
o Checking even/odd
o Computing factorial
o Checking prime numbers
2. Advanced Functions:
o Function overloading
o Recursion (Fibonacci, GCD, Power)
o Call by reference (Swapping)
o Using recursion for calculations
These questions and solutions help you understand function concepts in C++, from basic
function definitions to recursion and function overloading. Let me know if you need more
explanations! 🚀
Here are 10 basic questions on pointers in C++, along with explanations and example
programs.
1. Declare and Initialize a Pointer
Question: Write a C++ program to declare an integer pointer, initialize it with a variable's
address, and print the variable using the pointer.
#include <iostream>
using namespace std;
int main() {
int num = 10;
int *ptr = # // Pointer storing the address of num
return 0;
}
2. Pointer Arithmetic
Question: Write a C++ program to demonstrate pointer arithmetic (increment and decrement
operations).
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30};
int *ptr = arr;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int num = 42;
int *ptr = #
int **ptr2 = &ptr; // Pointer to pointer
cout << "Value of num: " << num << endl;
cout << "Value using ptr: " << *ptr << endl;
cout << "Value using ptr2: " << **ptr2 << endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int x = 5, y = 10;
cout << "Before Swap: x = " << x << ", y = " << y << endl;
swap(&x, &y);
cout << "After Swap: x = " << x << ", y = " << y << endl;
return 0;
}
Question: Write a C++ program to allocate memory dynamically using new and free it using
delete.
#include <iostream>
using namespace std;
int main() {
int *ptr = new int; // Dynamically allocate memory
*ptr = 25; // Assign value
cout << "Dynamically allocated value: " << *ptr << endl;
#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to first element
return 0;
}
#include <iostream>
using namespace std;
int* getPointer() {
int *ptr = new int(100); // Dynamically allocated integer
return ptr;
}
int main() {
int *p = getPointer();
cout << "Value returned by function: " << *p << endl;
delete p; // Free memory
return 0;
}
8. Pointer to a Structure
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
};
int main() {
Student s1 = {"John", 21};
Student *ptr = &s1;
return 0;
}
#include <iostream>
using namespace std;
int main() {
char str[] = "Hello";
char *ptr = str;
return 0;
}
#include <iostream>
using namespace std;
void displayMessage() {
cout << "Hello, this is a function pointer example!" << endl;
}
int main() {
void (*funcPtr)(); // Function pointer
funcPtr = displayMessage;
funcPtr(); // Calling function using pointer
return 0;
}
These questions help understand basic pointer operations and their advanced uses in C++. Let
me know if you need further explanations! 🚀