0% found this document useful (0 votes)
8 views6 pages

C Programs for Patterns and Data Handling

Uploaded by

gkirtiratna
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)
8 views6 pages

C Programs for Patterns and Data Handling

Uploaded by

gkirtiratna
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

Important Program :

[Link] a program to display following pattern


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

#include <stdio.h>

int main() {
int rows = 5; // Number of rows in the pattern

// Outer loop for rows


for(int i = 0; i < rows; i++) {
// Inner loop for printing '*'
for(int j = 0; j < rows - i; j++) {
printf("* ");
}
// Move to the next line after each row
printf("\n");
}
return 0;
}

2. Write a program to identify number entered by use is even and odd.


#include <stdio.h>
int main() {
int number;

// Ask the user to enter a number


printf("Enter an integer: ");
scanf("%d", &number);

// Check if the number is even or odd


if(number % 2 == 0) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);
}

return 0;
}

3. Write a Program to print Fibonacci series up to the term entered by the user .
#include <stdio.h>

int main() {
int n, first = 0, second = 1, next;

// Ask the user to enter the number of terms


printf("Enter the number of terms: ");
scanf("%d", &n);

printf("Fibonacci Series up to %d terms:\n", n);

// Print the first two terms of Fibonacci series


printf("%d, %d, ", first, second);

// Generate and print the rest of the terms


for (int i = 2; i < n; i++) {
next = first + second;
printf("%d, ", next);
first = second;
second = next;
}
printf("\n");

return 0;
}

4. w rite a program to read and print a matrix of 3x3.

#include <stdio.h>

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

// Ask the user to enter the elements of the matrix


printf("Enter the elements of the 3x3 matrix:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("Enter element at position (%d, %d): ", i+1, j+1);
scanf("%d", &matrix[i][j]);
}
}
// Display the matrix
printf("\nThe entered matrix is:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
printf("%d\t", matrix[i][j]);
}
printf("\n");
}

return 0;
}

5. Write a program by using any three string manipulation Function.


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

int main() {
char str1[50] = "Hello";
char str2[50] = "World";
char str3[100];

// String Length Function (strlen)


printf("Length of str1: %d\n", strlen(str1));
printf("Length of str2: %d\n", strlen(str2));

// String Copy Function (strcpy)


strcpy(str3, str1);
printf("Copied string (str3) after strcpy: %s\n", str3);

// String Concatenation Function (strcat)


strcat(str3, " ");
strcat(str3, str2);
printf("Concatenated string (str3) after strcat: %s\n", str3);

return 0;
}

6. Write a program in c by using a structure to store information of students i.e name .roll
number and Marks and also display it.
#include <stdio.h>

// Define a structure to hold student information


struct Student {
char name[50];
int rollNumber;
float marks;
};

int main() {
// Declare an array of structures to store multiple students' information
struct Student students[3]; // Assuming we have 3 students

// Read student information from the user


for (int i = 0; i < 3; i++) {
printf("Enter details for student %d:\n", i + 1);
printf("Name: ");
scanf("%s", students[i].name);
printf("Roll Number: ");
scanf("%d", &students[i].rollNumber);
printf("Marks: ");
scanf("%f", &students[i].marks);
}

// Display student information


printf("\nStudent Information:\n");
for (int i = 0; i < 3; i++) {
printf("Student %d\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].rollNumber);
printf("Marks: %.2f\n", students[i].marks);
printf("\n");
}

return 0;
}

Common questions

Powered by AI

A structure 'Student' is defined to group student-related data such as name, roll number, and marks. An array of this structure type is then used to store data for multiple students. Input is taken for each student, filling the array with corresponding data for each field, which is then displayed using loop iteration over the array, showcasing each student's information stored in the structure .

Abstraction through structures allows grouping related data, which simplifies complex data handling into manageable units. It enhances readability by encapsulating related data, such as student information, into meaningful types, reducing the conceptual complexity seen by developers working with data. Abstraction facilitates maintenance since updates to how data types are internally represented require changes only in structure definitions, rather than all through the code. It promotes organized code design, making the programs more modular and easier to understand and extend .

The Fibonacci program initializes the first two terms of the sequence as 0 and 1. It then uses a loop starting from the third term to generate subsequent terms by adding the two preceding numbers, updating the sequence until it reaches the user-specified number of terms .

The program demonstrates three string manipulation functions: 'strlen', which calculates the length of given strings 'str1' and 'str2'; 'strcpy', which copies the content of 'str1' into another string 'str3'; and 'strcat', which concatenates 'str2' to 'str3', resulting in a combined string of 'str1' and 'str2'. These functions enable basic manipulations like length computation, copying, and concatenation .

Pointers and memory addresses are crucial in handling structures efficiently. Although the specific programs do not explicitly use pointers to reference structures, understanding pointers would be essential for memory management and optimizing data access in more complex scenarios, such as dynamic memory allocation for structures or passing structures to functions using pointers to avoid copying large data structures .

The program uses a nested loop structure; the outer loop iterates over the number of rows (5 rows), controlling the number of times the pattern goes to a new line after completing each row print. Inside the outer loop, there is an inner loop that prints a decreasing number of asterisks in each consecutive row, determined by the equation (rows - i), where i is the current row index .

Designing loop structures for custom tasks involves anticipating variable relationships and controlling loop iterations accurately to match the task's logic requirements. In the Fibonacci series, loops are strategically aligned with specific sequence requirements, where the loop initiates subsequent operations based on established logical conditions (like prior element sums). Similarly, the matrix program leverages dual-loop matrices to account for index-based item aggregation in data storage and retrieval tasks, demonstrating a customized logic approach to handle multi-dimensional data inputs and outputs .

The program determines if a number is even or odd by checking the remainder when the number is divided by 2. If the remainder is 0, the number is even; otherwise, it is odd. This logic is implemented using the modulus operator (%).

Input validation is critical in ensuring that the program handles only precise and expected forms of input, making it robust against erroneous data entry or interactions. For example, in the matrix program, avoiding invalid numbers like characters or symbols, can prevent runtime errors. Implementing checks for valid entries before proceeding with operations ensures predictable behavior, improves reliability, and enhances user experience by avoiding unexpected program failures and providing meaningful feedback upon invalid input .

The program employs nested loops; the outer loop iterates over the rows, and the inner loop iterates over the columns of the matrix. It prompts the user for input for each element of a 3x3 matrix, storing values entered by the user. For displaying, a similar nested loop is used to print each matrix element, thus outputting the entire matrix in a structured format .

You might also like