0% found this document useful (0 votes)
22 views16 pages

Wa0016.

The document contains multiple C code examples demonstrating various programming concepts such as palindrome checking, prime number verification, Fibonacci series generation (both iterative and recursive), array handling, dynamic memory allocation using malloc and calloc, and student data management. Each code snippet includes user input and output examples, showcasing the functionality of the programs. The final example summarizes student performance metrics based on input data for multiple subjects.

Uploaded by

rpmrkjxpdz
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)
22 views16 pages

Wa0016.

The document contains multiple C code examples demonstrating various programming concepts such as palindrome checking, prime number verification, Fibonacci series generation (both iterative and recursive), array handling, dynamic memory allocation using malloc and calloc, and student data management. Each code snippet includes user input and output examples, showcasing the functionality of the programs. The final example summarizes student performance metrics based on input data for multiple subjects.

Uploaded by

rpmrkjxpdz
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

Code-5

/*ZENIL RANA

68

E-3*/

#include <stdio.h>

int main()

{ char string[40];

int length = 0, flag = 1, i;

printf("Enter string:\n");

gets(string);

for (i = 0; string[i] != '\0'; i++)

{ length++;

for (i = 0; i < length / 2; i++)

if (string[i] != string[length - 1 - i])

flag = 0;

break;

if (flag == 1)

printf("PALINDROME");

else

printf("NOT PALINDROME");

return 0;

}
Output:

Run 1:

------------

Enter String:

madam

PALINDROME

Run 2:

------------

Enter String:

PCU

NOT PALINDROME
Code-6a
/*ZENIL RANA

68

E-3*/

#include <stdio.h>

int PrimeOrNot(int);

int main()

{int n1, prime;

printf("\n\n Function : check whether a number is prime number or not :\n");

printf("---------------------------------------------------------------\n");

printf(" Input a positive number : ");

scanf("%d", &n1);

prime = PrimeOrNot(n1);

if (prime == 1)

printf(" The number %d is a prime number.\n", n1);

else

printf(" The number %d is not a prime number.\n", n1);

return 0;}

int PrimeOrNot(int n1)

{int i = 2;

while (i <= n1 / 2)

{ if (n1 % i == 0){

return 0;}

else{ i++;

return 1;}

Output:

Input a positive number : 5

The number 5 is a prime number.


Code-6b-without Recursive function
/*

ZENIL RANA

68

E-3

*/

#include <stdio.h>

void generateFibonacci(int n)

{ int a = 0, b = 1, next;

printf("Fibonacci Series: ");

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

{ printf("%d, ", a);

next = a + b;

a = b;

b = next;

printf("\n");

int main()

int n;

printf("Enter the number of terms in the Fibonacci series: ");

scanf("%d", &n);

generateFibonacci(n);

return 0;

}
Code-6b-with Recursive function

/*

ZENIL RANA

68

E-3

*/

#include <stdio.h>

int fibonacci(int n)

{ if (n <= 1) {

return n;}

else {

return fibonacci(n - 1) + fibonacci(n - 2);}

void generateFibonacciRecursive(int n)

{ printf("Fibonacci Series: ");

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

{ printf("%d, ", fibonacci(i));

printf("\n");

int main()

{ int n;

printf("Enter the number of terms in the Fibonacci series: ");

scanf("%d", &n);

generateFibonacciRecursive(n);

return 0;

Output:

Enter the number of terms in the Fibonacci series: 10

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34


Code-7a
/*

ZENIL RANA

68

E-3

*/

#include <stdio.h>

int main()

{ int n;

printf("Enter the number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d elements:\n", n);

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

{ scanf("%d", &arr[i]);

int *ptr = arr;

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

{ printf("%d ", *(ptr + i));

return 0;

Output:

Enter the number of elements: 4

Enter 4 elements:

Elements in the array: 3 5 2 8


Code-7b-malloc()
/*

ZENIL RANA

68

E-3

*/

#include <stdio.h>

#include <stdlib.h>

int main()

int n, i, *ptr, sum = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

ptr = (int *)calloc(n, sizeof(int));

if (ptr == NULL)

printf("Error! memory not allocated.");

exit(0);

printf("Enter elements: ");

for (i = 0; i < n; ++i)

scanf("%d", ptr + i);

sum += *(ptr + i);

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

free(ptr);

return 0;

}
Output of malloc():

Enter number of elements: 3

Enter elements: 100

20

36

Sum=156
Code-7b-calloc()
/*

ZENIL RANA

68

E-3

*/

#include <stdio.h>

#include <stdlib.h>

int main()

int n, i, *ptr, sum = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

ptr = (int *)malloc(n * sizeof(int));

// if memory cannot be allocated

if (ptr == NULL)

printf("Error! memory not allocated.");

exit(0);

printf("Enter elements: ");

for (i = 0; i < n; ++i)

scanf("%d", ptr + i);

sum += *(ptr + i);

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

// deallocating the memory

free(ptr);

return 0;

}
Output of calloc():

Enter number of elements: 3

Enter elements: 100

20

36

Sum=156
Code-8

/*

ZENIL RANA

68

E-3

*/

#include <stdio.h>

#include <string.h>

#define NUM_STUDENTS 5

#define NUM_SUBJECTS 3

struct Student

char name[50];

float marks[NUM_SUBJECTS];

};

void inputStudentData(struct Student students[]);

void displayHighestInSubject(struct Student students[], int subject);

float calculateOverallPercentage(struct Student students[]);

int countPassingStudents(struct Student students[]);

int countFailingOneSubject(struct Student students[]);

int countDistinctions(struct Student students[]);

int main()

struct Student students[NUM_STUDENTS];

inputStudentData(students);

displayHighestInSubject(students, 0);

float overallPercentage = calculateOverallPercentage(students);

printf("\nOverall Percentage Result of the Class: %.2f%%\n", overallPercentage);

int passingStudents = countPassingStudents(students);


printf("Total Passing Students in the Class: %d\n", passingStudents);

int failingOneSubject = countFailingOneSubject(students);

printf("Total Students Failing in One Subject: %d\n", failingOneSubject);

int distinctions = countDistinctions(students);

printf("Total Distinctions in the Class: %d\n", distinctions);

return 0;

void inputStudentData(struct Student students[])

printf("Enter details for %d students:\n", NUM_STUDENTS);

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

printf("Student %d:\n", i + 1);

printf("Enter Name: ");

scanf("%s", students[i].name);

printf("Enter Marks for 3 Subjects:\n");

for (int j = 0; j < NUM_SUBJECTS; ++j)

printf("Subject %d: ", j + 1);

scanf("%f", &students[i].marks[j]);

void displayHighestInSubject(struct Student students[], int subject)

float highestMarks = students[0].marks[subject];

int highestIndex = 0;

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

if (students[i].marks[subject] > highestMarks)

{
highestMarks = students[i].marks[subject];

highestIndex = i;

printf("\nStudent with Highest Marks in Subject %d: %s\n", subject + 1,


students[highestIndex].name);

float calculateOverallPercentage(struct Student students[])

float totalMarks = 0.0;

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

for (int j = 0; j < NUM_SUBJECTS; ++j)

totalMarks += students[i].marks[j];

return (totalMarks / (NUM_STUDENTS * NUM_SUBJECTS)) * 100.0;

int countPassingStudents(struct Student students[])

int passingCount = 0;

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

int totalMarks = 0;

for (int j = 0; j < NUM_SUBJECTS; ++j)

totalMarks += students[i].marks[j];

if (totalMarks >= 40 * NUM_SUBJECTS)

{
passingCount++;

return passingCount;

int countFailingOneSubject(struct Student students[])

int failingCount = 0;

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

int failCount = 0;

for (int j = 0; j < NUM_SUBJECTS; ++j)

if (students[i].marks[j] < 40)

failCount++;

if (failCount == 1)

failingCount++;

return failingCount;

int countDistinctions(struct Student students[])

int distinctions = 0;

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

int distinctionCount = 0;
for (int j = 0; j < NUM_SUBJECTS; ++j)

if (students[i].marks[j] >= 75)

distinctionCount++;

if (distinctionCount == NUM_SUBJECTS)

distinctions++;

return distinctions;

Output:

Enter details for 5 students:

Student 1:

Enter Name: Aditya

Enter Marks for 3 Subjects:

Subject 1: 90

Subject 2: 95

Subject 3: 82

Student 2:

Enter Name: Prerna

Enter Marks for 3 Subjects:

Subject 1: 88

Subject 2: 93

Subject 3: 94

Student 3:
Enter Name: Anushka

Enter Marks for 3 Subjects:

Subject 1: 78

Subject 2: 79

Subject 3: 82

Student 4:

Enter Name: Ritwik

Enter Marks for 3 Subjects:

Subject 1: 92

Subject 2: 90

Subject 3: 89

Student 5:

Enter Name: Pooja

Enter Marks for 3 Subjects:

Subject 1: 90

Subject 2: 91

Subject 3: 93

Student with Highest Marks in Subject 1: Ritwik

Overall Percentage Result of the Class: 88.40%

Total Passing Students in the Class: 5

Total Students Failing in One Subject: 0

Total Distinctions in the Class: 5

You might also like