0% found this document useful (0 votes)
28 views31 pages

Document

The document contains a series of C programming exercises that cover various topics such as calculating areas, checking odd/even numbers, determining positive/negative values, and handling arrays. Each exercise includes a code snippet demonstrating the solution to the problem. The exercises also involve loops, conditional statements, and basic arithmetic operations.

Uploaded by

goriseyourself
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)
28 views31 pages

Document

The document contains a series of C programming exercises that cover various topics such as calculating areas, checking odd/even numbers, determining positive/negative values, and handling arrays. Each exercise includes a code snippet demonstrating the solution to the problem. The exercises also involve loops, conditional statements, and basic arithmetic operations.

Uploaded by

goriseyourself
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/ 31

1.

Write a program to calculate area and circumference of real circle (hint:


real circle is a circle having positive radius).

#include <stdio.h>

#define PI 3.14159

int main() {

double r, a, c;

printf("Enter radius: ");

scanf("%lf", &r);

if (r > 0) {

a = PI * r * r;

c = 2 * PI * r;

printf("Area: %.2lf\n", a);

printf("Circumference: %.2lf\n", c);

} else {

printf("Invalid radius\n");

return 0;

2. Write a program to check whether the given number is odd or even.

#include <stdio.h>

int main() {

int n;

printf("Enter a number: ");

scanf("%d", &n);

if (n % 2 == 0) {

printf("%d is even\n", n);

} else {
printf("%d is odd\n", n);

return 0;

3. Write a program to read a number and check whether it is positive,


negative or zero.

#include <stdio.h>

int main() {

int n;

printf("Enter a number: ");

scanf("%d", &n);

if (n > 0) {

printf("%d is positive\n", n);

} else if (n < 0) {

printf("%d is negative\n", n);

} else {

printf("The number is zero\n");

return 0;

4. Write a program that inputs cost price (CP) and selling price (SP) and
determines whether there is loss or gain.

#include <stdio.h>

int main() {

float CP, SP, result;

printf("Enter Cost Price: ");


scanf("%f", &CP);

printf("Enter Selling Price: ");

scanf("%f", &SP);

result = SP - CP;

if (result > 0) {

printf("Profit: %.2f\n", result);

} else if (result < 0) {

printf("Loss: %.2f\n", -result);

} else {

printf("No Profit, No Loss\n");

return 0;

5. Write a C program that reads three numbers and displays the largest
and the smallest among them.

#include <stdio.h>

int main() {

int a, b, c, largest, smallest;

printf("Enter three numbers: ");

scanf("%d %d %d", &a, &b, &c);

if (a > b) {

if (a > c) {

largest = a;

} else {

largest = c;

}
} else {

if (b > c) {

largest = b;

} else {

largest = c;

if (a < b) {

if (a < c) {

smallest = a;

} else {

smallest = c;

} else {

if (b < c) {

smallest = b;

} else {

smallest = c;

printf("Largest: %d\n", largest);

printf("Smallest: %d\n", smallest);

return 0;

}
6. Write a C program that takes three different numbers and find out the
middle number.

#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three different numbers: ");

scanf("%d %d %d", &a, &b, &c);

if (a > b) {

if (a < c) {

printf("Middle number is: %d\n", a);

} else if (b > c) {

printf("Middle number is: %d\n", b);

} else {

printf("Middle number is: %d\n", c);

} else {

if (a > c) {

printf("Middle number is: %d\n", a);

} else if (b < c) {

printf("Middle number is: %d\n", b);

} else {

printf("Middle number is: %d\n", c);

return 0;

}
7.Write a program to calculate and display real roots of a quadratic
equation. (Hint check condition (b2-4ac)>0 for real root).

#include <stdio.h>

#include <math.h>

int main() {

float a, b, c, discriminant, root1, root2;

printf("Enter coefficients a, b and c: ");

scanf("%f %f %f", &a, &b, &c);

discriminant = b * b - 4 * a * c;

if (discriminant > 0) {

root1 = (-b + sqrt(discriminant)) / (2 * a);

root2 = (-b - sqrt(discriminant)) / (2 * a);

printf("Real and distinct roots: %.2f and %.2f\n", root1, root2);

} else if (discriminant == 0) {

root1 = -b / (2 * a);

printf("Real and equal roots: %.2f\n", root1);

} else {

printf("No real roots\n");

return 0;

8. Write a program to find the commission amount on the basis of sales


amount as per the following conditions:
Sales amount. Commission

0-1000 5%

1001-2000 10%

>2000 12%

#include <stdio.h>

int main() {

float sales, commission;

printf("Enter the sales amount: ");

scanf("%f", &sales);

if (sales >= 0 && sales <= 1000) {

commission = sales * 0.05;

} else if (sales > 1000 && sales <= 2000) {

commission = sales * 0.10;

} else if (sales > 2000) {

commission = sales * 0.12;

} else {

printf("Invalid sales amount\n");

return 1;

printf("Commission amount: %.2f\n", commission);

return 0;

9. Write Interactive program that takes length and breadth and performs
the following task.
a) Area of rectangle

b) Perimeter of rectangle

c) Exit

#include <stdio.h>

int main() {

float length, breadth, area, perimeter;

int choice;

while (1) {

printf("\nMenu:\n");

printf("1. Area of Rectangle\n");

printf("2. Perimeter of Rectangle\n");

printf("3. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

if (choice == 1 || choice == 2) {

printf("Enter length and breadth of the rectangle: ");

scanf("%f %f", &length, &breadth);

if (choice == 1) {

area = length * breadth;

printf("Area of the rectangle: %.2f\n", area);

} else if (choice == 2) {

perimeter = 2 * (length + breadth);

printf("Perimeter of the rectangle: %.2f\n", perimeter);


} else if (choice == 3) {

printf("Exiting the program...\n");

break;

} else {

printf("Invalid choice. Please try again.\n");

return 0;

10. Write a C program that takes a number less than 10 and display its
multiplication table.

#include <stdio.h>

int main() {

int num;

printf("Enter a number less than 10: ");

scanf("%d", &num);

if (num >= 10 || num < 0) {

printf("Invalid input! Please enter a number less than 10.\n");

} else {

for (int i = 1; i <= 10; i++) {

printf("%d x %d = %d\n", num, i, num * i);

}
return 0;

C Language Lab Works 3 Looping Statements

1. Write a program to calculate and display the following series: 1 3


5......... to 10th term.

#include <stdio.h>

int main() {

int term = 1, i;

for(i = 0; i < 10; i++) {

printf("%d ", term);

term += 2;

return 0;

2. Write a program to display square series of first 'n' natural numbers.

#include <stdio.h>

int main() {

int n, i;

scanf("%d", &n);

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

printf("%d ", i * i);

return 0;

3. Write a program to calculate factorial of any number given by the user.

#include <stdio.h>
int main() {

int n, i, factorial = 1;

scanf("%d", &n);

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

factorial = factorial*i;

printf("Factorial of %d is %d", n, factorial);

return 0;

4. Write a program to calculate and display the multiplication table of a


number.

#include <stdio.h>

int main() {

int n, i;

scanf("%d", &n);

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

printf("%d x %d = %d\n", n, i, n * i);

return 0;

5. Write a program to display Fibonacci series having 'n' terms.

#include <stdio.h>

int main() {

int n, i;

int first = 0, second = 1, next;

scanf("%d", &n);

printf("Fibonacci Series: ");


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

if(i == 1) {

printf("%d ", first);

continue;

if(i == 2) {

printf("%d ", second);

continue;

next = first + second;

first = second;

second = next;

printf("%d ", next);

return 0;

6. Write a program to check the given number is palindrome or not


palindrome.

#include <stdio.h>

int main() {

int n, reverse = 0, remainder, original;

scanf("%d", &n);

original = n;

while (n != 0) {

remainder = n % 10;

reverse = reverse * 10 + remainder;

n /= 10;

}
if (original == reverse)

printf("The number is a palindrome.");

else

printf("The number is not a palindrome.");

return 0;

7. Write a program to calculate sum of the following series.

Sum = 1+1/2+1/3+1/4+.........+1/n

#include <stdio.h>

int main() {

int n, sum = 0;

scanf("%d", &n);

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

sum += 1 / i;

printf("%d", sum);

return 0;

8. Write a program to display the following output:

12345

2 4 6 8 10

3 6 9 12 15

#include <stdio.h>

int main() {
for(int i = 1; i <= 3; i++) {

for(int j = 1; j <= 5; j++) {

printf("%d ", i * j);

printf("\n");

return 0;

9. Write a program to check whether the given number is prime or not.

#include <stdio.h>

int main() {

int n, i, count = 0;

scanf("%d", &n);

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

if (n % i == 0) {

count++;

if (count == 2)

printf("The number is prime.");

else

printf("The number is not prime.");

return 0;

10. Write a program to display the prime series up

2 3 5 7 11 upto nth term.


#include <stdio.h>

int main() {

int n, num = 2, c, i, p;

scanf("%d", &n);

for(c = 0; c < n; num++) {

p = 1;

for(i = 2; i <= num / 2; i++) {

if(num % i == 0) {

p = 0;

break;

if(p && num > 1) {

printf("%d ", num);

c++;

return 0;

C Language Lab Works 4: Array

1. Write a program to input marks of 5 subjects and display the total and
average marks.

#include <stdio.h>

int main() {

int marks[5], total = 0;

float average;
for(int i = 0; i < 5; i++) {

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

total += marks[i];

average = total / 5.0;

printf("Total: %d\n", total);

printf("Average: %.2f\n", average);

return 0;

2. Write a program to input n numbers in an array and display the sum of


even numbers from the array.

#include <stdio.h>

int main() {

int n, sum = 0;

scanf("%d", &n);

int arr[n];

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

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

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

if(arr[i] % 2 == 0) {
sum += arr[i];

printf("Sum of even numbers: %d\n", sum);

return 0;

3. Write a program to find the position of the key number from given set of
numbers which any stored in an array. [Sequential Search]

#include <stdio.h>

int main() {

int n, key, position = -1;

scanf("%d", &n);

int arr[n];

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

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

scanf("%d", &key);

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

if(arr[i] == key) {

position = i + 1; // Adding 1 to convert from index to position

break;

}
}

if(position != -1)

printf("Key %d found at position %d\n", key, position);

else

printf("Key %d not found\n", key);

return 0;

4. Write a program to read the age of 100 persons and count the number
of persons in the age group between 50 and 60 years.

#include <stdio.h>

int main() {

int ages[100], count = 0;

for (int i = 0; i < 100; i++) {

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

for (int i = 0; i < 100; i++) {

if (ages[i] >= 50 && ages[i] <= 60) {

count++;

printf("%d\n", count);

return 0;

}
87, 34, 91, 29, 72, 94, 62, 39, 46, 66, 98, 96, 10, 48, 57, 97, 65, 67, 59,
17, 57, 23, 10, 86, 37, 47, 65, 83, 27, 17, 4, 61, 71, 25, 47, 27, 48, 67, 85,
43, 43, 51, 65, 63, 62, 49, 34, 16, 14, 66, 28, 68, 27, 87, 18, 65, 18, 23,
94, 7, 81, 32, 57, 80, 77, 2, 20, 48, 6, 34, 43, 84, 63, 52, 84, 10, 61, 81,
51, 7, 14, 99, 23, 44, 70, 86, 95, 16, 4, 32, 81, 19, 3, 29, 50, 50, 63, 46,
68, 1

5. Write a program to input 'n' numbers and find out the greatest and
smallest numbers.

#include <stdio.h>

int main() {

int n, i;

scanf("%d", &n);

int numbers[n];

int greatest, smallest;

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

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

greatest = numbers[0];

smallest = numbers[0];

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

if (numbers[i] > greatest) {

greatest = numbers[i];

if (numbers[i] < smallest) {

smallest = numbers[i];
}

printf("Greatest: %d\n", greatest);

printf("Smallest: %d\n", smallest);

return 0;

6. Write a program that takes salary of 100 employees and print the salary
of the employes ascending order.

#include <stdio.h>

int main() {

int salaries[100], i, j, temp;

for (i = 0; i < 100; i++) {

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

for (i = 0; i < 99; i++) {

for (j = i + 1; j < 100; j++) {

if (salaries[i] > salaries[j]) {

temp = salaries[i];

salaries[i] = salaries[j];

salaries[j] = temp;

}
for (i = 0; i < 100; i++) {

printf("%d\n", salaries[i]);

return 0;

7. Write a program to transpose a matrix with size 3X3.

#include <stdio.h>

int main() {

int matrix[3][3], transpose[3][3], i, j;

printf("Enter the elements of the 3x3 matrix:\n");

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

scanf("%d", &matrix[i][j]);

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

transpose[j][i] = matrix[i][j];

printf("Transposed matrix is:\n");

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

printf("%d ", transpose[i][j]);


}

printf("\n");

return 0;

8. Write a program to calculate the sum of all elements of a matrix with


size 3X3.

#include <stdio.h>

int main() {

int matrix[3][3], sum = 0, i, j;

printf("Enter the elements of the 3x3 matrix:\n");

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

scanf("%d", &matrix[i][j]);

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

sum += matrix[i][j];

printf("Sum of all elements: %d\n", sum);

return 0;
}

9. Write a program to calculate the sum of diagonal matrix with size 3X3.

#include <stdio.h>

int main() {

int matrix[3][3], sum = 0, i, j;

for (i = 0; i < 3; i++) {

for (j = 0; j < 3; j++) {

scanf("%d", &matrix[i][j]);

for (i = 0; i < 3; i++) {

sum += matrix[i][i];

printf("%d\n", sum);

return 0;

10. Write a program to add two matrices with size 2X2 supplying by
elements by the user.

#include <stdio.h>

int main() {

int matrix1[2][2], matrix2[2][2], sum[2][2], i, j;

for (i = 0; i < 2; i++) {


for (j = 0; j < 2; j++) {

scanf("%d", &matrix1[i][j]);

for (i = 0; i < 2; i++) {

for (j = 0; j < 2; j++) {

scanf("%d", &matrix2[i][j]);

for (i = 0; i < 2; i++) {

for (j = 0; j < 2; j++) {

sum[i][j] = matrix1[i][j] + matrix2[i][j];

for (i = 0; i < 2; i++) {

for (j = 0; j < 2; j++) {

printf("%d ", sum[i][j]);

printf("\n");

return 0;

C Language Lab Works 5: String


1. Write a program to find the length of string without using built in
function.

2. Write a program to reverse the given string without using built in


function.

3. Write a program to concatenate two given strings without using built in


function.

4. Write programs for the following problems using built in functions:


strlen() and strrev()

5. Write programs for the following problems using built in functions:


strcmp() and strcpy() 7. Write programs for the following problems using
built in functions: strcat() and strupri)

8. Write a program to input line of text and count the number of digit,
vowels, consonants, wh spaces and other characters.

9. Write a program to check whether the given string is palindrome or not.

10. Write a program to input the names of 10 students and sort them in
alphabetical order.

11. Write a program to search a string from the given set of strings.

Computer Science-1

Character Set used in C

The character set defines a group of letters, words, numbers, symbols and
expression which is used in C language. The character set in C language
can be grouped into the following three categories.
Letters: upper case A to Z and lowercase a to z

Digits: 0 to 9

Special characters, listed in the table

Use of Comments

The comments have no effects on program codes but they are useful
components of program for program documentation. They help
programmer for the description of the program codes. They also make
programmer to modify and testing of a progam Comments can be written
in two forms:

//Single Line Comments

/Multi-Line.........................comments/

Keywords

Chapter 3 Programming Concepts and pee

keywords. They cannot be used as identifiers in the program. Mainly there


are 32 keywords There are certain words which are reserved by the C
compiler. These words are known as sed in standard C language.

auto

break

double
int

struct

else

long

switch

case

enum

register

typedef

chat

extern

return

union

const

float
short

unsigned

contime

for

If

signed

vuid

diefault

goto

sizeof

volatile

do

static

while

Basic Data Types in C


A data might be in the form of number, character, string or record and
they are called types. Mainly C language provides two types of data:
primary data types and second data types. The secondary data types will
be discussed later.

Constants and Variables

Variable

A variable is a value that can change any time. It is a memory location


used to store a data value. A variable name should be carefully chosen by
the programmer so that its use is reflected in a useful way in the entire
program. Any variable declared in a program should confirm to the
following:

They must always begin with a letter, although some systems permit
underscore as the first character.

White space is not allowed.

A variable should not be a keyword.

It should not contain any special characters.

It does not allow keyword.

Examples of invalid variable names are

125

(area)
6th

Famples of valid variable narnes are

Sun number

Salary

abc

Emp name

averagel

Mainly, the variables are categorized into two types:

uneric variable: The variable that stores numeric data only is called
numeric variabl The numeric data may be whole number or fractional
number. Examples of numri variables are integer, floating point and
double,

String variable: The variable that stores character data only is called
string variable. The string data may be single character or string.
Examples of string variable are character, array of character (string), table
of strings.

The purpose of variable declaration is to allocate memory space inside a


memory of computer. Declaration does two things:

It tells the compiler what the variable name is


It specifies what type of data the variable will hold.

Constant

A constant value is the one which does not change during the execution of
a program C supports several types of constants. These constants are
given below:

Character Constant

A character constant stores only single character. It is enclosed by single


quotation mark.

Integer Constant

An integer constant is a sequence of digits. There are 3 types of integers


namely decimal integer, octal integers and hexadecimal integer.

You might also like