/* Question 1(a)
WAP to print following pattern:
(It will print a right triangle pattern of stars) */
#include <stdio.h>
#include <conio.h>
void main() {
int i, j, n = 5;
clrscr();
for(i = 1; i <= n; i++) {
for(j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
getch();
}
/* Question 1(b)
WAP to calculate the sum of the following series.
(Harmonic Series: 1 + 1/2 + 1/3 + ... + 1/n) */
#include <stdio.h>
#include <conio.h>
void main() {
float sum = 0;
int i, n;
clrscr();
printf("Enter n: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
sum += 1.0/i;
printf("Sum = %f", sum);
getch();
}
/* Question 1(c)
WAP for structure named ’Product’ which will store pid, pname, price and date of
manufacturer as a collection of day, month and year for 3 products. */
#include <stdio.h>
#include <conio.h>
struct Date {
int day, month, year;
};
struct Product {
int pid;
char pname[20];
float price;
struct Date mfg;
};
void main() {
struct Product p[3];
int i;
clrscr();
for(i = 0; i < 3; i++) {
printf("Enter Product %d details:\n", i+1);
scanf("%d %s %f %d %d %d", &p[i].pid, p[i].pname, &p[i].price,
&p[i].mfg.day, &p[i].mfg.month, &p[i].mfg.year);
}
for(i = 0; i < 3; i++) {
printf("\nProduct %d: %d %s %.2f %d/%d/%d",
i+1, p[i].pid, p[i].pname, p[i].price,
p[i].mfg.day, p[i].mfg.month, p[i].mfg.year);
}
getch();
}
/* Question 2
Explain recursion. Write a program to find factorial of a number using recursion. */
#include <stdio.h>
#include <conio.h>
int factorial(int n) {
if(n <= 1) return 1;
return n * factorial(n-1);
}
void main() {
int n;
clrscr();
printf("Enter number: ");
scanf("%d", &n);
printf("Factorial = %d", factorial(n));
getch();
}
/* Question 3
Write differences between structure and union with suitable examples,
Also explain nested structure with example. */
#include <stdio.h>
#include <conio.h>
struct Student {
char name[20];
int roll;
float marks;
};
union Data {
char name[20];
int roll;
float marks;
};
struct Nested {
struct Student student;
int class;
};
void main() {
struct Student s = {"John", 1, 85.5};
union Data d;
struct Nested n = {{"Jane", 2, 90.5}, 10};
clrscr();
printf("Structure size: %d\n", sizeof(s));
printf("Union size: %d\n", sizeof(d));
printf("Nested structure: %s %d %.2f Class: %d",
n.student.name, n.student.roll, n.student.marks, n.class);
getch();
}
/* Question 4
Write the output of the following code:
(This is the given code that shows pointer concepts) */
#include <stdio.h>
void point() {
int var = 10;
int* ptr;
ptr = &var;
printf("Value at ptr = %p \n", ptr);
printf("Value at var = %d \n", var);
printf("Value at *ptr = %d \n", *ptr);
}
int main() {
point();
return 0;
}