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

CCC

C language codes

Uploaded by

sujeevbongi
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)
22 views9 pages

CCC

C language codes

Uploaded by

sujeevbongi
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/ 9

1.

Create a structure called "Student" with OUTPUT:-


members name, age, and total marks. Write
Student 1: Name: Alice, Age: 20, Total Marks:
a C program to input data for two students,
85.50
display their information, and find the
average of total marks. Student 2: Name: Bob, Age: 22, Total Marks:
90.00
INPUT:-
Average Total Marks: 87.75
#include <stdio.h>
2. Write a program to initialize an array of
struct Student {
structures to store the details of 5 students
char name[50]; (name, roll number, and marks) and display
the information.
int age;
INPUT:-
float totalMarks;
#include <stdio.h>
};
struct Student {
int main() {
char name[50];
struct Student s1, s2;
int rollNo;
float averageMarks;
float marks;
printf("Enter name, age, and total marks for
Student 1:\n"); };

scanf("%s %d %f" ,s1.name,&s1.age, int main() {


&s1.totalMarks);
struct Student students[5] = {
printf("Enter name, age, and total marks for
{"Alice", 1, 85.5},
Student 2:\n");
{"Bob", 2, 90.0},
scanf("%s %d %f", s2.name, &s2.age,
&s2.totalMarks); {"Charlie", 3, 78.2},
printf("\nStudent 1: Name: %s, Age: %d, {"Diana", 4, 88.8},
Total Marks: %.2f\n", s1.name, s1.age,
s1.totalMarks); {"Eve", 5, 92.3}

printf("Student 2: Name: %s, Age: %d, Total };


Marks: %.2f\n", s2.name, s2.age, printf("Student Details:\n");
s2.totalMarks);
for (int i = 0; i < 5; i++) {
averageMarks = (s1.totalMarks +
s2.totalMarks) / 2; printf("Name: %s, Roll No: %d, Marks:
%.2f\n",
printf("\nAverage Total Marks: %.2f\n",
averageMarks); students[i].name, students[i].rollNo,
students[i].marks);
return 0;
}
}
return 0; printf("Difference: %d\n", difference);

OUTPUT:- return 0;

Student Details: }

Name: Alice, Roll No: 1, Marks: 85.50 OUTPUT:-

Name: Bob, Roll No: 2, Marks: 90.00 Enter two integers: 10 5

Name: Charlie, Roll No: 3, Marks: 78.20 Sum: 15

Name: Diana, Roll No: 4, Marks: 88.80 Difference: 5

Name: Eve, Roll No: 5, Marks: 92.30 4. Define a structure named "Date" with
members day, month, and year. Write a C
3. Define a structure that takes two integers
program to input two dates and find the
as arguments (call by reference) and
difference in days between them.
calculates their sum and difference.
INPUT:-
INPUT:-
#include <stdio.h>
#include <stdio.h>
struct Date {
struct Numbers {
int day;
int a;
int month;
int b;
int year;
};
};
void calculate(struct Numbers *nums, int
*sum, int *difference) { int dateToDays(struct Date d) {

*sum = nums->a + nums->b; return d.year * 365 + d.month * 30 + d.day;

*difference = nums->a - nums->b; }

int main() { int main() {

struct Numbers nums; struct Date date1, date2;

int sum, difference; int days1, days2, difference;

printf("Enter two integers: "); printf("Enter first date (day month year): ");

scanf("%d %d", &nums.a, &nums.b); scanf("%d %d %d", &date1.day,


&date1.month, &date1.year);
calculate(&nums, &sum, &difference);

printf("Enter second date (day month year):


// Display results
");
printf("Sum: %d\n", sum);
scanf("%d %d %d", &date2.day, }
&date2.month, &date2.year);
int main() {
days1 = dateToDays(date1);
struct Circle c1, c2;
days2 = dateToDays(date2);
printf("Enter radius of Circle 1: ");
difference = (days1 > days2) ? (days1 -
scanf("%f", &c1.radius);
days2) : (days2 - days1);
printf("Enter radius of Circle 2: ");
printf("Difference in days: %d\n",
difference); scanf("%f", &c2.radius);

printf("\nCircle 1 - Area: %.2f, Perimeter:


%.2f\n",
return 0;
calculateArea(c1),
}
calculatePerimeter(c1));
OUTPUT:-
printf("Circle 2 - Area: %.2f, Perimeter: %.2f\
Enter first date (day month year): 15 8 2023 n",

Enter second date (day month year): 20 8 calculateArea(c2),


2023 calculatePerimeter(c2));

Difference in days: 5 return 0;

5. Define a structure named Circle to }


represent a circle with a radius. Write a C
OUTPUT:-
program to calculate the area and perimeter
of two circles and display the results. Enter radius of Circle 1: 5
INPUT:- Enter radius of Circle 2: 7
#include <stdio.h> Circle 1 - Area: 78.54, Perimeter: 31.42
#define PI 3.14159 Circle 2 - Area: 153.94, Perimeter: 43.98
struct Circle {

float radius; 6.Write a program in C to demonstrate the


array of structures and vice versa with a
};
suitable example.

INPUT:-
float calculateArea(struct Circle c) {
#include <stdio.h>
return PI * c.radius * c.radius;
struct Student {
}
char name[50];

int marks[3];
float calculatePerimeter(struct Circle c) {
};
return 2 * PI * c.radius;
void displayArrayOfStructures(struct Student Details of Students:
students[], int n) {
Student 1 Name: Alice
printf("\nDetails of Students:\n");
Marks: 85, 90, 88
for (int i = 0; i < n; i++) {
Student 2 Name: Bob
printf("Student %d Name: %s\n", i + 1,
Marks: 78, 82, 80
students[i].name);
7. Write a program that counts the
printf("Marks: %d, %d, %d\n",
occurrences of a specific value in an array
students[i].marks[0], students[i].marks[1],
students[i].marks[2]); INPUT:-
} #include <stdio.h>
} int main() {

int arr[] = {1, 2, 3, 2, 4, 2, 5};


int main() { int size = sizeof(arr) / sizeof(arr[0]);
int n = 2; int value = 2;
struct Student students[n]; int count = 0;
for (int i = 0; i < n; i++) { for (int i = 0; i < size; i++) {
printf("Enter name of Student %d: ", i + if (arr[i] == value) {
1);
count++;
scanf("%s", students[i].name);
}
printf("Enter marks in 3 subjects for %s: ",
students[i].name); }

for (int j = 0; j < 3; j++) { printf("The value %d occurs %d times.\n",


value, count);
scanf("%d", &students[i].marks[j]);
return 0;
}
}
}
OUTPUT:-
displayArrayOfStructures(students, n);
The value 2 occurs 3 times.
return 0;

OUTPUT:-

Enter name of Student 1: Alice

Enter marks in 3 subjects for Alice: 85 90 88

Enter name of Student 2: Bob

Enter marks in 3 subjects for Bob: 78 82 80


8. WAP that adds two arrays of the same size int main() {
element-wise and stores the result in a third
int arr[] = {1, 2, 3, 4, 5};
array.
int size = sizeof(arr) / sizeof(arr[0]);
INPUT:-

#include <stdio.h>
printf("Array in reverse order: ");

for (int i = size - 1; i >= 0; i--) {


int main() {
printf("%d ", arr[i]);
int arr1[] = {1, 2, 3};
}
int arr2[] = {4, 5, 6};
printf("\n");
int size = sizeof(arr1) / sizeof(arr1[0]);

int result[size];
return 0;

}
for (int i = 0; i < size; i++) {

result[i] = arr1[i] + arr2[i];


Q.Similarities and Differences Between if-
}
else, if-else ladder, and switch-case

1. if-else Statement
printf("Resultant Array: ");
 Description: The if-else statement is
for (int i = 0; i < size; i++) { used to test a condition. If the
condition is true, one block of code
printf("%d ", result[i]);
executes; otherwise, another block
} executes.

printf("\n");  Syntax:

 if (condition) {

return 0;  // Code if condition is true

}  } else {

OUTPUT:-  // Code if condition is false

Resultant Array: 5 7 9  }

9. Write a program that prints the elements  Example:


of an array in reverse order without
 int num = 10;
modifying the original array.
 if (num > 5) {
INPUT:-
 printf("Number is greater than 5");
#include <stdio.h>
 } else {
 printf("Number is less than or equal check a variable against several
to 5"); constant values. It’s more efficient
than multiple if-else statements when
 }
comparing a single variable with many
 Output: Number is greater than 5 values.

2. if-else ladder Statement  Syntax:

 Description: An if-else ladder allows  switch (variable) {


multiple conditions to be checked
 case value1:
sequentially. Each else if checks a new
condition after the previous one fails.  // Code for value1
It’s useful when there are more than
 break;
two conditions to check.
 case value2:
 Syntax:
 // Code for value2
 if (condition1) {
 break;
 // Code if condition1 is true
 default:
 } else if (condition2) {
 // Code if no match
 // Code if condition2 is true
 break;
 } else {
 }
 // Code if all conditions are false
 Example:
 }
 int num = 2;
 Example:
 switch (num) {
 int num = 10;
 case 1:
 if (num > 20) {
 printf("One");
 printf("Greater than 20");
 break;9
 } else if (num > 5) {
 case 2:
 printf("Greater than 5 but less than
or equal to 20");  printf("Two");
 } else {  break;
 printf("Less than or equal to 5");  default:
 }  printf("Other number");
 Output: Greater than 5 but less than  break;
or equal to 20
 }
3. switch-case Statement
 Output: two
 Description: The switch-case
statement is used when you need to
Conclusion: printf("First repeated element is:
%d\n", result);
 Use if-else when you need to check a
simple condition. } else {

 Use if-else ladder when you need to printf("No repeated elements


check multiple conditions found.\n");
sequentially.
}
 Use switch-case when you need to
compare a variable against multiple
constant values efficiently. return 0;
10. Write a function that finds the }
first repeated element in an array.
OUTPUT:-
INPUT:-
First repeated element is: 3
#include <stdio.h>
11. Create a function that checks if a
substring exists within a given string
and returns the index of its first
int findFirstRepeated(int arr[], int size)
occurrence.
{
INPUT:-
for (int i = 0; i < size; i++) {
#include <stdio.h>
for (int j = i + 1; j < size; j++) {
#include <string.h>
if (arr[i] == arr[j]) {

return arr[i];
int findSubstringIndex(const char *str,
}
const char *substr) {
}
int strLen = strlen(str);
}
int subLen = strlen(substr);
return -1; // Return -1 if no
repetition is found
for (int i = 0; i <= strLen - subLen; i+
}
+) {

int j;
int main() {
for (j = 0; j < subLen; j++) {
int arr[] = {1, 2, 3, 4, 5, 3, 6};
if (str[i + j] != substr[j]) {
int size = sizeof(arr) / sizeof(arr[0]);
break;
int result = findFirstRepeated(arr,
}
size);
}

if (j == subLen) {
if (result != -1) {
return i; int count = 0;

} int inWord = 0;

return -1; for (int i = 0; str[i] != '\0'; i++) {

} if (isspace(str[i]) == 0) {

if (inWord == 0) {

int main() { count++;

const char *str = "Hello, world!"; inWord = 1;

const char *substr = "world"; }

int index = findSubstringIndex(str, } else {


substr);
inWord = 0;

}
if (index != -1) {
}
printf("Substring found at index:
%d\n", index);
return count;
} else {
}
printf("Substring not found.\n");

}
int main() {

const char *str = "Hello, how are you


return 0;
doing?";
}
int wordCount = countWords(str);
OUTPUT:-
printf("Number of words: %d\n",
Substring found at index: 7 wordCount);

12. Write a function that counts the number


of words in a string (words are separated by
return 0;
spaces).
}
INPUT:-
OUTPUT:-
#include <stdio.h>
Number of words: 5
#include <string.h>

#include <ctype.h>

int countWords(const char *str) {


13. Create a function that checks if a
substring exists within a given string and
returns the index of its first occurrence.

INPUT:-

#include <stdio.h>

#include <stdlib.h>

int compare(const void *a, const void *b) {

return (*(int *)b - *(int *)a);

int findKthLargest(int arr[], int size, int k) {

qsort(arr, size, sizeof(int), compare);

return arr[k-1];

int main() {

int arr[] = {12, 3, 5, 7, 19};

int size = sizeof(arr) / sizeof(arr[0]);

int k = 2;

int result = findKthLargest(arr, size, k);

printf("The %dth largest element is: %d\n",


k, result);

return 0;

OUTPUT:-

The 2nd largest element is: 12

14.

You might also like