0% found this document useful (0 votes)
34 views4 pages

SP Assignment

Uploaded by

Siddhant patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views4 pages

SP Assignment

Uploaded by

Siddhant patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Structure Programming Lab: Assignment 2

Course Code: 2413FYC1L1


Academic Year 2024

Question 1(a): Pattern Program


/* WAP to print following pattern */
#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): Series Sum


/* WAP to calculate the sum of the following series */
#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): Product Structure


/* 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>

1
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: Factorial using 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: ");

2
scanf("%d", &n);
printf("Factorial = %d", factorial(n));
getch();
}

Question 3: Structure vs Union Example


/* 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: Pointer Program Output


/* Write the output of the following code */
#include <stdio.h>

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

Output:
Value at ptr = <memory_address>
Value at var = 10
Value at *ptr = 10
Note: All programs are written in old C style with conio.h and include clrscr()
for Turbo C compatibility. The actual memory address in Question 4’s output
will vary with each execution.

You might also like