0% found this document useful (0 votes)
31 views8 pages

C

c simple programs

Uploaded by

Gayathri Shiva
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)
31 views8 pages

C

c simple programs

Uploaded by

Gayathri Shiva
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/ 8

11 b)

i. C program to find sum of 10 natural numbers:

#include <stdio.h>

int main() {
int n = 10;
int sum = 0;
int i;

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


sum += i;
}
printf("Sum of the first %d natural numbers is: %d\n", n, sum);

return 0;
}

OUTPUT:
Sum of the first 10 natural numbers is: 55

ii. Prime number or not

#include <stdio.h>
#include <math.h>

int is_prime(int num) {


if (num <= 1) {
return 0;
}
if (num <= 3) {
return 1;
}
if (num % 2 == 0 || num % 3 == 0) {
return 0;
}

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]);}
}

printf("enter the elements for second matrix:\n");


for(i=0;i<p;i++){
for(j=0;j<q;j++){
scanf("%d",&b[i][j]);}
}
for(i=0;i<m;i++){
for(j=0;j<q;j++){
for(k=0;k<p;k++){
res[i][j]+=a[i][k] * b[k][j];}
}
}
printf("\n the product of two matrices is:\n");
for(i=0;i<m;i++){
for(j=0;j<q;j++){
printf("%d\t",res[i][j]);
}
printf("\n");
}
}
return 0;
}
OUTPUT:
enter the order of first matrix:
2
2
enter the order of second matrix:
2
2
enter the elements for first matrix:
1
2
3
4
enter the elements for second matrix:
5
6
7
8
the product of two matrices is:
19 22
43 50
12 b)
I. Functions can be invoked in two ways: Call by Value or Call by Reference. These two ways
are generally differentiated by the type of values passed to them as parameters.
The parameters passed to the function are called actual parameters whereas the parameters
received by the function are called formal parameters.
Call By Value in C
• In call by value method of parameter passing, the values of actual parameters are copied to
the function’s formal parameters.
• There are two copies of parameters stored in different memory locations.
• One is the original copy and the other is the function copy.
• Any changes made inside functions are not reflected in the actual parameters of the caller.

Example of Call by Value:


#include <stdio.h>
void swapx(int x, int y);
int main()
{
int a = 10, b = 20; swapx(a, b); // Actual Parameters
printf("In 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 Function:\nx = %d y = %d\n", x, y);}
OUTPUT:
Inside Function:
x = 20 y = 10
In the Caller:
a = 10 b = 20

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.

Example of Call by reference

#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>

void concatenate(char str1[], char str2[]) {


strcat(str1, str2);
printf("Concatenated String: %s\n", str1);
}
void length(char str[]) {
printf("Length of String: %lu\n", strlen(str));
}

void copy(char dest[], char src[]) {


strcpy(dest, src);
printf("Copied String2: %s\n", dest);
}

void reverse(char str[]) {


int len = strlen(str);
char temp;
for (int i = 0; i < len / 2; i++) {
temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
printf("Reversed String: %s\n", str);
}

int main() {
char str1[100], str2[100], copyStr[100];

printf("Enter the first string: ");


gets(str1);
printf("Enter the second string: ");
gets(str2);

printf("\nPerforming Concatenation...\n");
concatenate(str1, str2);

printf("\nCalculating Length of the First String...\n");


length(str1);

printf("\nCopying the First String to Another String...\n");


copy(copyStr, str1);

printf("\nReversing the First String...\n");


reverse(str1);

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

Copying the First String to Another String...


Copied String2: HAIHELLO

Reversing the First String...


Reversed String: OLLEHIAH

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

You might also like