C
C
#include <stdio.h>
int main() {
int n = 10;
int sum = 0;
int i;
return 0;
}
OUTPUT:
Sum of the first 10 natural numbers is: 55
#include <stdio.h>
#include <math.h>
int i = 5;
while (i * i <= num) {
if (num % i == 0 || num % (i + 2) == 0) {
return 0;
}
i += 6;
}
return 1;
}
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (is_prime(number)) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n", number);
}
return 0;
}
OUTPUT:
Enter a number: 4
4 is not a prime number.
Enter a number: 2
2 is a prime number.
12 a) MATRIX MULTIPLICATION
PROGRAM (RECTANGULAR MATRIX):
#include<stdio.h>
int main()
{
int m,n,p,q,i,j,k;
int a[10][10],b[10][10],res[10][10];
printf("enter the order of first matrix:\n");
scanf("%d%d",&m,&n);
printf("enter the order of second matrix:\n");
scanf("%d%d",&p,&q);
if(n!=p){
printf("matrix is incompatible for multiply\n");
}
else{
printf("\nenter the elements for first matrix:\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);}
}
Call by Reference in C
• In call by reference method of parameter passing, the address of the actual parameters is
passed to the function as the formal parameters. In C, we use pointers to achieve call-by-
reference.
• Both the actual and formal parameters refer to the same locations.
• Any changes made inside the function are actually reflected in the actual parameters of the
caller.
#include <stdio.h>
void swapx(int*, int*);
int main()
{
int a = 10, b = 20;
swapx(&a, &b); // Actual Parameters
printf("Inside the Caller: \na = %d b = %d\n", a, b);
return 0;
}
void swapx(int* x, int y) // Formal Parameters
{
int t;
t = *x;
*x = *y;
*y = t;
printf("Inside the Function:\nx = %d y = %d\n", *x, *y);
}
OUTPUT:
Inside the Function:
x = 20 y = 10
Inside the Caller:
a = 20 b = 10
12 b)
II. PROGRAM:
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100], copyStr[100];
printf("\nPerforming Concatenation...\n");
concatenate(str1, str2);
return 0;
}
OUTPUT:
Enter the first string: HAI
Enter the second string: HELLO
Performing Concatenation...
Concatenated String: HAIHELLO
Calculating Length of the First String...
Length of String: 8
13 a) ||)
PROGRAM:
#include <stdio.h>
struct Student {
int roll_number;
char name[50];
float marks;
};
int main() {
struct Student students[60]; // Array to store details of up to 60 students
int num_students;
printf("Enter the number of students (maximum 60): ");
scanf("%d", &num_students);
if (num_students > 60) {
printf("Error: Maximum 60 students allowed.\n");
return 1; // Exit with error code
}
for (int i = 0; i < num_students; i++) {
printf("\nEnter details for student %d:\n", i + 1);
printf("Roll number: ");
scanf("%d", &students[i].roll_number);
printf("Name: ");
scanf(" %s", students[i].name);
printf("Marks: ");
scanf("%f", &students[i].marks);
}
printf("\nStudent Details:\n");
printf("Roll No.\tName\t\tMarks\n");
for (int i = 0; i < num_students; i++) {
printf("%d\t\t%s\t\t%.2f\n", students[i].roll_number, students[i].name, students[i].marks);
}
return 0;
}
OUTPUT:
Enter the number of students (maximum 60): 2
Enter details for student 1:
Roll number: 1
Name: k
Marks: 34
Enter details for student 2:
Roll number: 2
Name: s
Marks: 36
Student Details:
Roll No. Name Marks
1 k 34.00
2 s 36.00