0% found this document useful (0 votes)
23 views50 pages

pf questions

The document contains a series of C programming exercises that cover basic concepts such as input/output, loops, conditionals, and functions. Each exercise includes a question, code implementation, and an explanation of key components. The exercises range from printing messages to calculating sums, checking conditions, and using control structures like switch-case and loops.

Uploaded by

k242016
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)
23 views50 pages

pf questions

The document contains a series of C programming exercises that cover basic concepts such as input/output, loops, conditionals, and functions. Each exercise includes a question, code implementation, and an explanation of key components. The exercises range from printing messages to calculating sums, checking conditions, and using control structures like switch-case and loops.

Uploaded by

k242016
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/ 50

1. Print "Hello, World!

"

Question: Write a C program to print "Hello, World!" to the console.

c
CopyEdit
#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

Explanation:

 #include <stdio.h> includes the standard input-output library.


 printf() prints text to the console.
 \n moves the cursor to a new line.

2. Add Two Numbers

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:

 scanf("%d %d", &a, &b); takes two integer inputs.


 sum = a + b; performs addition.
 printf("Sum = %d\n", sum); prints the result.

3. Check Even or Odd

Question: Write a C program to check whether a number is even or odd.

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:

 % operator returns the remainder.


 If num % 2 == 0, it's even; otherwise, it's odd.

4. Find the Largest of Three Numbers

Question: Write a C program to find the largest of three numbers.

c
CopyEdit
#include <stdio.h>

int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

if (a >= b && a >= c)


printf("%d is the largest\n", a);
else if (b >= a && b >= c)
printf("%d is the largest\n", b);
else
printf("%d is the largest\n", c);

return 0;
}

Explanation:

 Uses if-else conditions to compare three numbers.

5. Swap Two Numbers Without Using a Third Variable

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;

printf("After swapping: a = %d, b = %d\n", a, b);


return 0;
}

Explanation:

 Uses arithmetic operations instead of a temporary variable to swap values.

6. Calculate Factorial of a Number

Question: Write a C program to find the factorial of a given number.

c
CopyEdit
#include <stdio.h>

int main() {
int num, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);

for (int i = 1; i <= num; i++)


fact *= i;

printf("Factorial = %d\n", fact);


return 0;
}

Explanation:

 Uses a loop to multiply numbers from 1 to num.

7. Reverse a Number

Question: Write a C program to reverse a given 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;
}

printf("Reversed number = %d\n", rev);


return 0;
}

Explanation:

 Extracts the last digit and appends it to rev in each iteration.

8. Check if a Number is Prime

Question: Write a C program to check if a number is prime.

c
CopyEdit
#include <stdio.h>

int main() {
int num, i, flag = 1;
printf("Enter a number: ");
scanf("%d", &num);

if (num <= 1)
flag = 0;

for (i = 2; i <= num / 2; i++) {


if (num % i == 0) {
flag = 0;
break;
}
}

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

Question: Write a C program to reverse a string.

c
CopyEdit
#include <stdio.h>
#include <string.h>

int main() {
char str[100], temp;
int i, len;

printf("Enter a string: ");


scanf("%s", str);

len = strlen(str);
for (i = 0; i < len / 2; i++) {
temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}

printf("Reversed string: %s\n", str);


return 0;
}

Explanation:

 Swaps characters from both ends to reverse the string.

10. Generate Fibonacci Series

Question: Write a C program to print the Fibonacci series up to n terms.

c
CopyEdit
#include <stdio.h>

int main() {
int n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);

printf("Fibonacci Series: %d, %d", t1, t2);


for (int i = 3; i <= n; i++) {
nextTerm = t1 + t2;
printf(", %d", nextTerm);
t1 = t2;
t2 = nextTerm;
}
printf("\n");
return 0;
}

Explanation:

 Generates the Fibonacci sequence by adding the last two numbers.

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.

1. Print Numbers from 1 to N using while loop

Question: Write a C program to print numbers from 1 to N using a while loop.

#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:

 Starts from i = 1 and increments until i <= n.


 Prints numbers in sequence.
2. Print Even Numbers from 1 to N using while loop

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:

 Initializes i = 2 (first even number).


 Increments i by 2 to get the next even number.

3. Sum of Natural Numbers using while loop

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++;
}

printf("Sum = %d\n", sum);


return 0;
}

Explanation:

 Adds numbers from 1 to N using sum += i.


4. Reverse a Number using while loop

Question: Write a C program to reverse a given number using a while loop.

#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;
}

printf("Reversed number = %d\n", rev);


return 0;
}

Explanation:

 Extracts digits one by one and builds the reversed number.

5. Find Factorial using while loop

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);

while (i <= num) {


fact *= i;
i++;
}

printf("Factorial = %d\n", fact);


return 0;
}

Explanation:

 Multiplies numbers from 1 to N to compute factorial.


6. Find the Sum of Digits using while loop

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;
}

printf("Sum of digits = %d\n", sum);


return 0;
}

Explanation:

 Extracts digits using % and accumulates the sum.

7. Print Multiplication Table using do-while loop

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:

 Runs at least once before checking i <= 10.


8. Check if a Number is Palindrome using do-while loop

Question: Write a C program to check if a number is palindrome using a do-while loop.

#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.

9. Count Number of Digits using do-while loop

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);

printf("Number of digits = %d\n", count);


return 0;
}
Explanation:

 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);

} while (num >= 0);

printf("Program terminated.\n");
return 0;
}

Explanation:

 Takes input in a loop until a negative number is entered.

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! 🚀

Here are 10 questions on the switch case statement in C with explanations.

1. Simple Calculator Using Switch Case

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;

printf("Enter an operator (+, -, *, /): ");


scanf("%c", &op);
printf("Enter two numbers: ");
scanf("%lf %lf", &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:

 Takes an operator and two numbers as input.


 Uses switch to perform arithmetic operations.

2. Check Vowel or Consonant

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:

 Uses multiple case labels for vowels.


 Default case handles consonants.

3. Weekday Name Using Switch Case

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:

 Maps numbers 1-7 to weekdays using switch.

4. Check Even or Odd Using Switch Case

Question: Write a C program to check if a number is even or odd using switch.

#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:

 Uses num % 2 to check even or odd.

5. Find Number of Days in a Month

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.

6. Convert Number to Roman Numeral (1-5)

Question: Write a C program that converts a number (1-5) to a Roman numeral.

#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:

 Maps numbers 1-5 to Roman numerals.

7. Menu-Driven Program for Basic Operations

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:

 Uses a menu-driven approach with different cases.

8. Grade System Using Switch Case

Question: Write a C program that assigns grades based on marks.

#include <stdio.h>

int main() {
int marks;
printf("Enter marks (0-100): ");
scanf("%d", &marks);

switch (marks / 10) {


case 10:
case 9: printf("Grade A\n"); break;
case 8: printf("Grade B\n"); break;
case 7: printf("Grade C\n"); break;
case 6: printf("Grade D\n"); break;
default: printf("Fail\n");
}

return 0;
}

Explanation:

 Divides marks by 10 to categorize into grades.

9. Convert Temperature Scale

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;

printf("Enter temperature scale (C/F): ");


scanf(" %c", &scale);
printf("Enter temperature: ");
scanf("%f", &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:

 Converts temperature based on user input.

10. Traffic Light System

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:

 Simulates a traffic light system.

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.

1. Print Numbers from 1 to N using for loop

Question: Write a C program to print numbers from 1 to N using a for loop.

#include <stdio.h>

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

for (int i = 1; i <= n; i++) {


printf("%d ", i);
}

return 0;
}

Explanation:

 Loops from 1 to N, printing each number.

2. Print Even Numbers from 1 to N

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);

for (int i = 2; i <= n; i += 2) {


printf("%d ", i);
}

return 0;
}

Explanation:

 Initializes i as 2 and increments by 2 to print even numbers.

3. Sum of First N Natural Numbers

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);

for (int i = 1; i <= n; i++) {


sum += i;
}

printf("Sum = %d\n", sum);


return 0;
}

Explanation:

 Adds numbers from 1 to N to sum.

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);

for (int i = 1; i <= num; i++) {


fact *= i;
}
printf("Factorial = %d\n", fact);
return 0;
}

Explanation:

 Multiplies numbers from 1 to N to compute factorial.

5. Reverse Number Printing

Question: Write a C program to print numbers from N to 1 using a for loop.

#include <stdio.h>

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

for (int i = n; i >= 1; i--) {


printf("%d ", i);
}

return 0;
}

Explanation:

 Starts from N and decrements until 1.

6. Multiplication Table using for loop

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);

for (int i = 1; i <= 10; i++) {


printf("%d x %d = %d\n", num, i, num * i);
}

return 0;
}

Explanation:

 Iterates from 1 to 10, printing multiplication results.

7. Find the Sum of Digits of a Number

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);

for (; num != 0; num /= 10) {


sum += num % 10;
}

printf("Sum of digits = %d\n", sum);


return 0;
}

Explanation:

 Extracts each digit using % 10 and adds it to sum.

8. Fibonacci Series Using for Loop

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);

for (int i = 1; i <= n; i++) {


printf("%d ", a);
next = a + b;
a = b;
b = next;
}
return 0;
}

Explanation:

 Uses a loop to generate Fibonacci numbers up to N terms.

9. Check Prime Number Using for Loop

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:

 Checks divisibility from 2 to sqrt(num).


 Uses break if a factor is found.

10. Print Pyramid Pattern Using for Loop

Question: Write a C program to print a pyramid pattern using a for loop.

#include <stdio.h>
int main() {
int rows;
printf("Enter number of rows: ");
scanf("%d", &rows);

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:

 Uses nested loops to create a pyramid pattern.

Summary

These questions cover different uses of the for loop:


✅ Printing sequences
✅ Summations
✅ Factorial & Fibonacci
✅ Number operations (prime check, sum of digits)
✅ Pattern printing

Let me know if you need more! 🚀

Here are 10 pattern printing questions using C, along with their expected outputs.

1. Right-Angled Triangle (Star Pattern)

Question: Print the following right-angled triangle pattern.

*
**
***
****
*****

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:

 Outer loop controls rows (i).


 Inner loop prints i stars in each row.

2. Inverted Right-Angled Triangle

Question: Print an inverted right-angled triangle.

*****
****
***
**
*

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:

 Starts with 5 stars and decreases.


3. Pyramid Pattern

Question: Print a pyramid 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");
}
return 0;
}

Explanation:

 First loop prints spaces.


 Second loop prints 2*i - 1 stars to form a pyramid.

4. Inverted Pyramid

Question: Print an inverted pyramid pattern.

*********
*******
*****
***
*

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:

 Similar to pyramid but starts with rows stars and decreases.

5. Hollow Square

Question: Print a 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

Question: Print a number pyramid.


1
222
33333
4444444
555555555

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:

 Each row prints the same number.

7. Floyd’s Triangle

Question: Print 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:

 Uses num to count continuously across rows.

8. Pascal’s Triangle

Question: Print 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:

 Uses factorial formula for binomial coefficients.

9. Diamond Pattern

Question: Print a 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:

 Prints an upper and lower pyramid.

10. Alphabet Pyramid

Question: Print an alphabet pyramid.

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:

 Each row prints the same alphabet, increasing from 'A'.

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.

1. Input and Output a 2D Matrix

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];

cout << "Enter 4 elements for the 2x2 matrix:\n";


for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
cin >> matrix[i][j];

cout << "Matrix:\n";


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++)
cout << matrix[i][j] << " ";
cout << endl;
}

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 << "Enter 9 elements for the 3x3 matrix:\n";


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++) {
cin >> matrix[i][j];
sum += matrix[i][j];
}

cout << "Sum of all elements: " << sum << endl;
return 0;
}

3. Transpose of a Matrix

Question: Write a C++ program to find the transpose of a matrix.

#include <iostream>
using namespace std;

int main() {
int matrix[3][3], transpose[3][3];

cout << "Enter 9 elements for the 3x3 matrix:\n";


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> matrix[i][j];

// Compute transpose
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
transpose[j][i] = matrix[i][j];

cout << "Transpose:\n";


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
cout << transpose[i][j] << " ";
cout << endl;
}

return 0;
}
4. Addition of Two Matrices

Question: Write a C++ program to add two 3x3 matrices.

#include <iostream>
using namespace std;

int main() {
int A[3][3], B[3][3], sum[3][3];

cout << "Enter first 3x3 matrix:\n";


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> A[i][j];

cout << "Enter second 3x3 matrix:\n";


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> B[i][j];

// 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];

cout << "Sum of matrices:\n";


for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
cout << sum[i][j] << " ";
cout << endl;
}

return 0;
}

5. Multiplication of Two Matrices

Question: Write a C++ program to multiply two matrices.

#include <iostream>
using namespace std;

int main() {
int A[2][2], B[2][2], product[2][2] = {0};

cout << "Enter first 2x2 matrix:\n";


for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
cin >> A[i][j];

cout << "Enter second 2x2 matrix:\n";


for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
cin >> B[i][j];

// 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];
}
}
}

cout << "Product of matrices:\n";


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++)
cout << product[i][j] << " ";
cout << endl;
}

return 0;
}

6. Find the Largest Element in a Matrix

Question: Write a C++ program to find the largest element in a matrix.

#include <iostream>
using namespace std;

int main() {
int matrix[3][3], maxElement;

cout << "Enter 9 elements for the matrix:\n";


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> matrix[i][j];

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];

cout << "Largest element: " << maxElement << endl;


return 0;
}

7. Find the Row-wise Sum of a Matrix

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];

cout << "Enter 9 elements for the 3x3 matrix:\n";


for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> matrix[i][j];

for (int i = 0; i < 3; i++) {


int sum = 0;
for (int j = 0; j < 3; j++)
sum += matrix[i][j];
cout << "Sum of row " << i + 1 << ": " << sum << endl;
}

return 0;
}

8. Check if a Matrix is Symmetric

Question: Write a C++ program to check if a matrix is symmetric.

9. Find the Column-wise Sum of a Matrix

Question: Write a C++ program to find the sum of each column in a matrix.

10. Find the Diagonal Sum of 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.

1. Find the Maximum Element in an Array


Question: Write a C++ program to find the largest element in an array.

#include <iostream>
using namespace std;

int main() {
int arr[5], maxElement;

cout << "Enter 5 elements: ";


for (int i = 0; i < 5; i++)
cin >> arr[i];

maxElement = arr[0];
for (int i = 1; i < 5; i++)
if (arr[i] > maxElement)
maxElement = arr[i];

cout << "Largest element: " << maxElement << endl;


return 0;
}

2. Find the Minimum Element in an Array

Question: Write a C++ program to find the smallest element in an array.

#include <iostream>
using namespace std;

int main() {
int arr[5], minElement;

cout << "Enter 5 elements: ";


for (int i = 0; i < 5; i++)
cin >> arr[i];

minElement = arr[0];
for (int i = 1; i < 5; i++)
if (arr[i] < minElement)
minElement = arr[i];

cout << "Smallest element: " << minElement << endl;


return 0;
}

3. Reverse an Array

Question: Write a C++ program to reverse the elements of an array.

#include <iostream>
using namespace std;
int main() {
int arr[5];

cout << "Enter 5 elements: ";


for (int i = 0; i < 5; i++)
cin >> arr[i];

cout << "Reversed array: ";


for (int i = 4; i >= 0; i--)
cout << arr[i] << " ";

return 0;
}

4. Calculate the Sum of Elements in an Array

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;

cout << "Enter 5 elements: ";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
sum += arr[i];
}

cout << "Sum of elements: " << sum << endl;


return 0;
}

5. Count Even and Odd Numbers in an Array

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 << "Enter 5 elements: ";


for (int i = 0; i < 5; i++) {
cin >> arr[i];
if (arr[i] % 2 == 0)
evenCount++;
else
oddCount++;
}

cout << "Even numbers: " << evenCount << ", Odd numbers: " << oddCount <<
endl;
return 0;
}

6. Copy Elements from One Array to Another

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];

cout << "Enter 5 elements: ";


for (int i = 0; i < 5; i++)
cin >> arr1[i];

for (int i = 0; i < 5; i++)


arr2[i] = arr1[i];

cout << "Copied array: ";


for (int i = 0; i < 5; i++)
cout << arr2[i] << " ";

return 0;
}

7. Search for an Element in an Array

Question: Write a C++ program to search for an element in an array.

#include <iostream>
using namespace std;

int main() {
int arr[5], key, found = 0;

cout << "Enter 5 elements: ";


for (int i = 0; i < 5; i++)
cin >> arr[i];

cout << "Enter the element to search: ";


cin >> key;

for (int i = 0; i < 5; i++) {


if (arr[i] == key) {
found = 1;
cout << "Element found at index " << i << endl;
break;
}
}

if (!found)
cout << "Element not found" << endl;

return 0;
}

8. Sort an Array in Ascending Order

Question: Write a C++ program to sort an array in ascending order.

#include <iostream>
using namespace std;

int main() {
int arr[5], temp;

cout << "Enter 5 elements: ";


for (int i = 0; i < 5; i++)
cin >> arr[i];

// 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;
}
}
}

cout << "Sorted array: ";


for (int i = 0; i < 5; i++)
cout << arr[i] << " ";

return 0;
}

9. Merge Two Arrays into a Third Array

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];

cout << "Enter 3 elements for first array: ";


for (int i = 0; i < 3; i++)
cin >> arr1[i];

cout << "Enter 3 elements for second array: ";


for (int i = 0; i < 3; i++)
cin >> arr2[i];

for (int i = 0; i < 3; i++)


merged[i] = arr1[i];

for (int i = 0; i < 3; i++)


merged[i + 3] = arr2[i];

cout << "Merged array: ";


for (int i = 0; i < 6; i++)
cout << merged[i] << " ";

return 0;
}

10. Find the Second Largest Element in an Array

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 << "Enter 5 elements: ";


for (int i = 0; i < 5; i++)
cin >> arr[i];

for (int i = 0; i < 5; i++) {


if (arr[i] > firstMax) {
secondMax = firstMax;
firstMax = arr[i];
} else if (arr[i] > secondMax && arr[i] != firstMax) {
secondMax = arr[i];
}
}

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.

Basic Questions on Functions in C++

1. Function to Add Two Numbers

Question: Write a C++ function that takes two integers as input and returns their sum.

#include <iostream>
using namespace std;

int add(int a, int b) {


return a + b;
}

int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "Sum: " << add(num1, num2) << endl;
return 0;
}

2. Function to Find the Maximum of Two Numbers

Question: Write a C++ function that takes two numbers as input and returns the maximum.

#include <iostream>
using namespace std;

int maxNumber(int a, int b) {


return (a > b) ? a : b;
}

int main() {
int x, y;
cout << "Enter two numbers: ";
cin >> x >> y;
cout << "Maximum: " << maxNumber(x, y) << endl;
return 0;
}

3. Function to Check if a Number is Even or Odd


Question: Write a C++ function that checks whether a given number is even or odd.

#include <iostream>
using namespace std;

void checkEvenOdd(int num) {


if (num % 2 == 0)
cout << num << " is Even\n";
else
cout << num << " is Odd\n";
}

int main() {
int n;
cout << "Enter a number: ";
cin >> n;
checkEvenOdd(n);
return 0;
}

4. Function to Calculate Factorial of a Number

Question: Write a C++ function to compute the factorial of a number.

#include <iostream>
using namespace std;

long long factorial(int n) {


if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}

int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Factorial: " << factorial(num) << endl;
return 0;
}

5. Function to Check if a Number is Prime

Question: Write a C++ function to check if a given number is prime.

#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;
}

Advanced Questions on Functions in C++

6. Function Overloading for Adding Two or Three Numbers

Question: Write a C++ program that demonstrates function overloading for adding two and
three numbers.

#include <iostream>
using namespace std;

int add(int a, int b) {


return a + b;
}

int add(int a, int b, int c) {


return a + b + c;
}

int main() {
cout << "Sum of 2 numbers: " << add(5, 10) << endl;
cout << "Sum of 3 numbers: " << add(5, 10, 15) << endl;
return 0;
}

7. Recursive Function for Fibonacci Series

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;
}

8. Function to Swap Two Numbers using Call by Reference

Question: Write a C++ function to swap two numbers using call by reference.

#include <iostream>
using namespace std;

void swap(int &a, int &b) {


int temp = a;
a = b;
b = temp;
}

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;
}

9. Function to Find the GCD (Greatest Common Divisor)

Question: Write a C++ function to compute the GCD of two numbers using recursion.

#include <iostream>
using namespace std;

int gcd(int a, int b) {


return (b == 0) ? a : gcd(b, a % b);
}

int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
cout << "GCD: " << gcd(num1, num2) << endl;
return 0;
}

10. Function to Find the Power of a Number

Question: Write a C++ function to calculate power (x^y) using recursion.

#include <iostream>
using namespace std;

long long power(int base, int exp) {


if (exp == 0)
return 1;
return base * power(base, exp - 1);
}

int main() {
int base, exp;
cout << "Enter base and exponent: ";
cin >> base >> exp;
cout << base << "^" << exp << " = " << power(base, exp) << endl;
return 0;
}

Summary of Topics Covered

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 = &num; // Pointer storing the address of num

cout << "Value of num: " << num << endl;


cout << "Address of num: " << &num << endl;
cout << "Value using pointer: " << *ptr << endl;

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;

cout << "First element: " << *ptr << endl;


ptr++; // Move to the next element
cout << "Second element: " << *ptr << endl;
ptr--; // Move back to the first element
cout << "First element again: " << *ptr << endl;

return 0;
}

3. Pointer to Pointer (Double Pointer)

Question: Write a C++ program to demonstrate a pointer to a pointer.

#include <iostream>
using namespace std;

int main() {
int num = 42;
int *ptr = &num;
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;
}

4. Swap Two Numbers using Pointers

Question: Write a C++ program to swap two numbers using pointers.

#include <iostream>
using namespace std;

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

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;
}

5. Dynamic Memory Allocation (new and delete)

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;

delete ptr; // Free allocated memory


return 0;
}
6. Array Access using Pointers

Question: Write a C++ program to access an array using pointers.

#include <iostream>
using namespace std;

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to first element

for (int i = 0; i < 5; i++) {


cout << "Element " << i + 1 << ": " << *(ptr + i) << endl;
}

return 0;
}

7. Function Returning a Pointer

Question: Write a C++ function that returns a pointer to an integer.

#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

Question: Write a C++ program to use a pointer to access members of a structure.

#include <iostream>
using namespace std;

struct Student {
string name;
int age;
};

int main() {
Student s1 = {"John", 21};
Student *ptr = &s1;

cout << "Name: " << ptr->name << endl;


cout << "Age: " << ptr->age << endl;

return 0;
}

9. Pointer and String (Character Array)

Question: Write a C++ program to access a string using a pointer.

#include <iostream>
using namespace std;

int main() {
char str[] = "Hello";
char *ptr = str;

while (*ptr != '\0') {


cout << *ptr;
ptr++;
}

return 0;
}

10. Function Pointer

Question: Write a C++ program to use a function pointer.

#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;
}

Summary of Topics Covered

1. Declaring and initializing pointers


2. Pointer arithmetic
3. Pointer to pointer
4. Swapping numbers using pointers
5. Dynamic memory allocation (new and delete)
6. Accessing arrays using pointers
7. Function returning a pointer
8. Pointer to a structure
9. Pointer and string manipulation
10. Function pointers

These questions help understand basic pointer operations and their advanced uses in C++. Let
me know if you need further explanations! 🚀

You might also like