UNIT 3
1.write a program to find the odd or evan number
#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
// true if num is perfectly divisible by 2
if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);
return 0;
2. write a program to find positive and negative number
#include<stdio.h>
int main()
int num;
scanf("%d",&num);
if(num == 0)
printf("Neither positive nor negative");
else if(num < 0)
printf("Negative");
else
printf("Positive");
return 0;
3. write a program to find factorial of a number
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
printf("Factorial of %d = %llu", n, fact);
return 0;
4. write a program to find square of a number
#include <stdio.h>
double square(double num)
return (num * num);
int main()
int num;
double n;
printf("\n\n Function : find square of any number :\n");
printf("------------------------------------------------\n");
printf("Input any number for square : ");
scanf("%d", &num);
n = square(num);
printf("The square of %d is : %.2f\n", num, n);
return 0;
5. write a program to find cube of a number
#include <stdio.h>
int main() {
int n = 5;
printf("Cube of %d = %d", n, (n*n*n));
return 0;
6. write a program to display table of any number
#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
for (int i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
return 0;
7. write a program to demonstrate while loop in c
#include <stdio.h>
int main() {
int count = 1;
// A simple while loop
while (count <= 5) {
printf("Count is: %d\n", count);
count++; // Increment count
return 0;
8. write a program to demonstrate do - while loop in c
#include <stdio.h>
int main() {
int i = 1;
do {
printf("This is iteration %d\n", i);
i++;
} while (i <= 5);
return 0;
UNIT 4
1.write a program to addition of matrix
#include <stdio.h>
int main() {
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
printf("Matrix created using an array:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
printf("\n");
}
return 0;
2. write a program to accept the elements in two dimesional array
#include <stdio.h>
int main() {
int rows, cols;
// Get the dimensions of the array
printf("Enter the number of rows: ");
scanf("%d", &rows);
printf("Enter the number of columns: ");
scanf("%d", &cols);
int array[rows][cols];
// Accept the elements of the array
printf("Enter the elements of the array:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("Enter element at position (%d, %d): ", i + 1, j + 1);
scanf("%d", &array[i][j]);
// Display the array
printf("\nThe entered 2D array is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", array[i][j]);
}
printf("\n");
return 0;
3. write a program to find the length of a string
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Geeks";
// Find length of string using strlen()
printf("%lu", strlen(s));
return 0;
4. write a program to compare two strings in c
#include <stdio.h>
#include <string.h>
int main() {
char str1[100], str2[100];
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
if (strcmp(str1, str2) == 0) {
printf("The strings are equal.\n");
} else {
printf("The strings are not equal.\n");
}
return 0;
UNIT 5
1. write c program for book structure with member book_id , book_price ,
book_author
#include <stdio.h>
// Define the structure for Book
struct Book {
int book_id;
float book_price;
char book_author[50];
};
int main() {
// Create a variable of type Book
struct Book book1;
// Input details for the book
printf("Enter Book ID: ");
scanf("%d", &book1.book_id);
printf("Enter Book Price: ");
scanf("%f", &book1.book_price);
printf("Enter Book Author: ");
getchar(); // To consume the newline character left by previous scanf
fgets(book1.book_author, sizeof(book1.book_author), stdin);
// Output the details of the book
printf("\nBook Details:\n");
printf("Book ID: %d\n", book1.book_id);
printf("Book Price: %.2f\n", book1.book_price);
printf("Book Author: %s\n", book1.book_author);
return 0;
}
2. write c program for sttudent structure with member student_roll ,
student_roll ,sname ,marks
#include <stdio.h>
// Define the structure
struct Student {
int student_roll;
char student_name[50];
float marks;
};
int main() {
// Declare a variable of type Student
struct Student student1;
// Input details for the student
printf("Enter Student Roll Number: ");
scanf("%d", &student1.student_roll);
printf("Enter Student Name: ");
scanf(" %[^\n]", student1.student_name); // To accept spaces in name
printf("Enter Marks: ");
scanf("%f", &student1.marks);
// Display the student details
printf("\nStudent Details:\n");
printf("Roll Number: %d\n", student1.student_roll);
printf("Name: %s\n", student1.student_name);
printf("Marks: %.2f\n", student1.marks);
return 0;
}