0% found this document useful (0 votes)
13 views

C Logic Programming

Uploaded by

biharwala8581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

C Logic Programming

Uploaded by

biharwala8581
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 97

C

LOGIC
PROGRAMMING

Dr. John T Mesia Dhas


Dr. T. S. Shiny Angel
C
LOGIC
PROGRAMMING

Dr. John T Mesia Dhas


Dr. T. S. Shiny Angel
Title: C LOGIC PROGRAMMING
Author: Dr. John T Mesia Dhas, Dr. T. S. Shiny Angel
Publisher: Self-published by Dr. John T Mesia Dhas
Copyright © 2020 Dr. John T Mesia Dhas
All rights reserved, including the right of reproduction in whole or in part or any
form
Address of Publisher: No-1, MGR Street, Charles Nagar, Pattabiram
Chennai – 600072
India
Email: jtmdhasres@gmail.com
Printer: The Palm
Mogappair West
Chennai -600037
India
ISBN: 978-93-5416-366-1
i

CONTENTS
Pgm. No Title Page No
I PROGRAMS IN INPUT, OUTPUT, DATA TYPES & OPERATORS
1 Display a string using printf() statement 1
2 Count number of variables received by scanf() function 1
3 Declaring and using variables 2
4 Converting data types 3
5 Arithmetic operations 4
6 Relational operations 4
7 Logical operators 5
8 Bitwise operators 5
9 Increment & Decrement operator 6
10 Convert octal number into decimal number 7
11 Evaluate the expression a = b++ + ++b 7
12 Solve the expression a = 5 <= 8 && 6 != 5 8
II PROGRAMS IN INPUT OUTPUT LIBRARY FUNCTIONS
13 Sizeof() function 9
14 getchar() & putchar() functions 9
15 getche(), getch() and putch()functions 10
16 gets() and puts() functions 11
17 cgets() and cputs() functions 11
III CONDITIONAL AND BRANCHING – IF, CASE STRUCTURE
18 Checking equivalence of two numbers using if structure 13
19 Find biggest of two numbers using if – else structure 13
20 Find biggest of three numbers using nested if structure 14
21 Create a report card using if – else – if ladder 15
22 Demonstrate case structure. 17
23 Find the roots of a quadratic equation 18
IV CONDITIONAL AND LOOPING
24 Display first n even numbers 20
25 Display number triangle, nested for loop 20
26 Display number pyramid 21
27 Factorial using while structure 22
28 Sum of digits using do- while 23
29 Sum of n numbers using do while- while structure 24
30 Search prime or not 24
31 Exp(x) = 1 + x/1! + x2/2! + x3/3! + ………. + xn/n! 25
32 Sin() series 26
33 Cos() series 27
34 Print the given number in reverse order 28
35 Sum = 1 + ½ + 1/3 + ¼ + …….. + 1/n 29
V STRINGS
36 Strlen () 31
37 Strcpy() 31
38 Strcmp() 32
39 Strlwr() & strupr() 33
40 Strcat() 34
41 Strrev() 34
ii

Pgm. No Title Page No


42 Find the length, reverse and Check palindrome of the given string 35
43 Count vowels, consonants, words and blank space of a line of 36
string
44 Substring detection and count in a line of text 37
45 Substring removal 39
VI USER DEFINED FUNCTIONS
46 Functions without arguments and without return value 41
47 Functions with arguments and without return type 41
48 Function without arguments with return value 42
49 Functions with arguments with return values 43
50 Call by reference 43
51 Recursion – Factorial 44
52 Function as an argument 45
53 Function with operators 46
54 Pre-processor 47
55 NCR using recursion 48
56 Find GCD of two numbers 49
57 Fibonacci series 50
VII ARRAYS
58 Insertion sort 52
59 Bubble sort 53
60 Linear search 54
61 matrix addition and subtraction 55
62 Matrix multiplication 56
63 Matrix transpose 58
64 Determinant of a matrix 59
VIII POINTERS
65 Sample program using pointer 60
66 Sum of N numbers using pointers 61
67 Pointers and string 61
IX STRUCTURE & UNION
68 Structures 63
69 Structure with arrays 63
70 Structure with pointer 65
71 Structure with function 65
72 Union 66
X ANNA UNIVERSITY ADDITIONAL PROGRAMS
73 Area & circumference of a circle 68
74 Find the largest digit of a number 68
75 Check a number and its reverse are same or not 69
76 Armstrong or not 70
77 Find odd position and even position digits of a number 71
78 Find the second highest number 72
79 Print numbers with special condition 73
80 Generate prime numbers 74
81 Arrange digits of a number into ascending order 75
82 Multiplication without using ‘*’ operator 76
iii

Pgm. No Title Page No


83 Find differences in date 77
84 Convert Celsius into Fahrenheit 78
85 Leap year or not 79
86 Check a digit present in a number or not 80
87 Swap two arrays without using third array 80
88 Find row sum and column sum of a matrix 81
89 Print all combinations of a four digit number 82
90 Odd or even 84
91 Square root of a number 84
92 Swap without third variable 85
93 Check palindrome without using string function 85
94 Largest element in a row and column of a matrix 86
95 Number triangle 87
96 Number ‘5’ triangle 88
97 Odd position and even position sum of an array 89
98 Students mark processing using union 90
CHAPTER 1
PROGRAMS IN INPUT, OUTPUT, DATA TYPES &
OPERATORS

Program 1:
Display a string using printf() statement.
Aim:
Create a program in C to display your college name and format it using format
specifiers.
Source code:
# include<stdio.h>
# include<conio.h>
void main()
{
clrscr();
printf("\n\n\n\t\tRVS Padmavathy College of Engineering and
Technology");
printf("\n\t\t\tDept. of Computer Science & Engineering");
printf("\n\t\t\t\t\tChennai");
getch();
}
Output:

Program 2:
Count number of variables received by scanf() function.
Aim:
Create a program in C to count number of variables received by scanf() function.
1
Source code:

Output:

Program 3:
Declaring and using variables.
Aim:
Create a program in C to declare variables and print it.
Source code:
# include <stdio.h>
# include <conio.h>
void main()
{
int a = 10;
float b = 20.5;
char c = 'C', d[] = "RVS Padmavathy";
clrscr();
printf("\n\na = %d\nb = %f\nc = %c\nd = %s\n",a,b,c,d);
getch();
}
Output:
a = 10
b = 20.500000
c=C
d = RVS Padmavathy

2
Program 4:
Converting data types.
Aim:
Create a C program to convert data types.
Source code:

Output:

Program 5:
Arithmetic operations.
Aim:
Create a program in C to perform arithmetic operations.
Source code:

3
Output:

Program 6:
Relational operations
Aim:
Create a program in C to demonstrate relational operations.
Source code:

4
Output:

Program 7:
Logical operators
Aim:
Create a program to demonstrate logical operators.
Source code:

Output:

Program 8:
Bitwise operators.
Aim:
Create a program in C to demonstrate bitwise operators.
Source code:

5
Output:

Program 9:
Increment & Decrement operator
Aim:
Create a program in C to perform increment and decrement operations.
Source code:

6
Output:

Program 10
Convert octal number into decimal number.
Aim:
Create a program in C to convert octal number into decimal number.
Source code:

Output:

Program 11
Evaluate the expression a = b++ + ++b
Aim:
Create a program in C to evaluate the above expression.
Source code:

7
Output:

Program 12
a = 5 <= 8 && 6 != 5
Aim:
Create a program in C to solve the expression a = 5 <= 8 && 6 != 5.
Source code:

Output:

8
CHAPTER 2
PROGRAMS IN LIBRARY FUNCTIONS

Program 13
Sizeof() function
Aim:
Create a program in C to use sizeof() function.
Source code:

Output:

Program 14
Accepting characters using getchar() function from keyboard and display it using
putchar().
Aim:
Create a program in C to accept characters from key board and display it.
Source code:

9
Output:

Program 15
Accepting characters using getche() ant getch() then display it using putch().
Aim:
Create a program in C to accept characters using getche() and getch() functions then
display it using putch() function.
Source code:

10
Output:

Program 16
Demonstrate gets() and puts() functions.
Aim:
Create a program in C to accept and display captions using gets() and puts() functions.
Source code:

Output:

Program 17
Demonstrate cgets() and cputs().
Aim:
Create a program in C to accept and display strings using cgets() and cputs()
functions.
Source code:

11
Output:

12
CHAPTER 3
CONDITIONAL AND BRANCHING – IF STRUCTURE

Program 18
Checking equivalence of two numbers using if structure.
Aim:
Create a program in C to check equivalence of two numbers using if structure.
Source code:

Output:

Program 19
Find biggest of two numbers using if – else structure.
Aim:
Create a program in C to find biggest of two numbers using if – else structure.
Source code:

13
Output:

Program 20
Find biggest of three numbers using nested if structure.
Aim:
Create a program in C to find biggest of three numbers using nested if structure.
Source code:

Output:

14
Program 21
Create a report card using if – else – if ladder using the following constraints.
Average Grade Result
<50 Fail Fail
Pass all the subjects and Second Pass
average >=50 and <60
Pass all the subjects and First Pass
average >=60 and <75
Pass all the subjects and Distinction Pass
average >=75 and <90
Pass all the subjects and Honors Pass
average >=90

Aim:
Create a program in C to find grade and result using if - else – if structure.
Source code:

15
16
Output:

Program 22
Demonstrate case structure.
Aim:
Create a program in C to perform basic arithmetic operations using switch case
structure.
Source code:

17
Output:

Program 23
Find the roots of a quadratic equation.
Aim:
Create a program in C to find roots of a quadratic equation.
Source code:

18
Output:

19
CHAPTER 4
CONDITIONAL AND LOOPING

Program 24
Display first n even numbers.
Aim:
Create a program in C to display first n even numbers
Source code:

Output:

Program 25
Display number triangle, nested for loop.
Aim:
Create a program in C to display number triangle.
Source code:

20
Output:

Program 26
Display number pyramid
Aim:
Create a program in C to display number pyramid.
Source code:

21
Output

Program 27
Factorial using while structure.
Aim:
Create a program in C to find factorial of a given number using while structure.
Source code:

22
Output:

Program 28
Sum of digits using do- while
Aim:
Create a program in c to calculate sum of digits of a given number.
Source code:

Output:

23
Program 29
Sum of n numbers using do while- while structure.
Aim:
Create a program in C to add given set of numbers.
Source code:

Output:

Program 30
Search prime or not
Aim:
Create a C program to check the given number is prime or not.

24
Source code:

Output:

Program 31
Find the sum of the following series
Exp(x) = 1 + x/1! + x2/2! + x3/3! + ………. + xn/n!

Aim:
Create a program in C to evaluate exponential series.
Source code:

25
Output:

Program 32
Find the sum of the following series

Aim:

Create a program in C to find the sine value.

Source Code:

26
Output:

Program 32

Find the sum of the following series

Aim:

Create a program in C to find cos value.

Source code:

27
Output:

Program 34
Print the given number in reverse order.
Aim:
Create a program in C to print the given number in reverse order.
Source code:

28
Output:

Program 35
Find sum of the following series.
1 + ½ + 1/3 + ¼ + …….. + 1/n.
Aim:
Create a program in C to find the sum of series.
Source code:

29
Output:

30
CHAPTER 5
STRINGS

Program 36
Strlen ()
Aim:
Create a program in C to use strlen().
Source code:

Output:

Program 37
Strcpy().
Aim:
Create a program in C for strcpy().
Source code:

31
Output:

Program 38
Strcmp()
Aim:
Create a program in C for comparing two strings.
Source code:

32
Output:

Program 39
Strlwr() & strupr().
Aim:
Create a program in C for strlwr() & strupr().
Source code:

33
Output:

Program 40
Strcat()
Aim:
Create a program in C for strcat().
Source code:

Output:

Program 41
Strrev().
Aim:
Create a program in C for strrev().
Source code:

34
Output:

Program 42
Find the length, reverse and Check palindrome of the given string
Aim:
Create a program in C to find length, reverse and check the given string is palindrome
or not.
Source code:

35
Output:

Program 43
Count vowels, consonants, words and blank space of a line of string.
Aim:
Create a program in C to count vowels, consonants, words and blank space of a line of
string.
Source code:

36
Output:

Program 44
Substring detection and count in a line of text.
Aim:
Create a program in C to check the substring is present in a main string.
Source code:

37
Output:

38
Program 45
Substring removal.
Aim:
Create a program in C to find a substring and remove it from the main string.
Source code:

39
Output:

40
CHAPTER 6
USER DEFINED FUNCTIONS

Program 46
Functions without arguments and without return value
Aim:
Create a program in C to display your college name using function.
Source code:

Output:

Program 47
Functions with arguments and without return type.
Aim:
Create a program in C to add two numbers using function.
Source code:

41
Output:

Program 48
Function without arguments with return value.
Aim:
Create a program in C to multiply two numbers using function.
Source code:

Output:

42
Program 49
Functions with arguments with return values.
Aim:
Create a program in C to divide two numbers using functions.
Source code:

Output:

Program 50
Call by reference.
Aim:
Create a program in C to swap numbers using function.
Source code:

43
Output:
Enter two numbers : 10 20
Before swap x = 10 y = 20
After swap x = 20 y = 10

Program 51
Recursion – Factorial.
Aim:
Create a program in C to find factorial of a number.
Source code:

44
Output:

Program 52
Function as an argument
Aim:
Create a program in C to use a function as an argument of another function.
Source code:

45
Output:

Program 53
Function with operators
Aim:
Create a program in C to perform function with operators + and -.
Source code:

46
Output:

Program 54
Preprocessor
Aim:
Create a program in C to demonstrate preprocessor.
Source code:

47
Output:

Program 55
NCR using recursion
Aim:
Create a program in C to find NCR value.
Source code:

Output:

48
Program 56
Find GCD of two numbers.
Aim:
Create a program in C to find GCD of two numbers.
Source code:

Output:

49
Program 57
Fibonacci series

Aim:
Create a program in C to display Fibonacci series.
Source code:

Output:

50
51
CHAPTER 7
ARRAYS

Program 58
Insertion sort
Aim:
Create a program to do insertion sort
Source code:

Output:

52
Program 59
Bubble sort
Aim:
Create a program in C to do bubble sort.
Source code:

Output:

53
Program 60
Linear search
Aim:
Create a program in C to search a number using linear search method.
Source code:

Output:

54
Program 61
Perform matrix addition and subtraction
Aim:
Create a program in C to perform matrix addition and subtraction.

Source code:

55
Output:

Program 62
Matrix multiplication
Aim:
Create a program in C to do matrix multiplication.
Source code:

56
Output:

57
Program 63
Matrix transpose
Aim:
Create a program in C to transpose a matrix.
Program:

Output:

58
Program 64
Determinant of a matrix
Aim:
Create a program in C to find determinant of a matrix.
Source code:

Output:

59
CHAPTER 8
POINTERS
Program 65
Sample program using pointer.
Aim:
Create a program in C to access pointer variable and its address.
Source code:

Output:
Enter two numbers : 5 10
Value of a =5
Value of b = 10
Sum of a and b : 15
Value of x =5
Address of x = 65518
Value of y = 10
Address of y = 65516
Sum of x and y : 15

60
Program 66
Sum of N numbers using pointers.
Aim:
Create a program in C to sum a set of numbers.
Source code:

Output:
Sum of the array : 55
Program 67
Pointers and string.
Aim:
Create a program in C to print a pointer char variable.
Source code:

Output:
Enter your name : John T Mesia Dhas

61
Given name : John T Mesia Dhas
Life is beautiful

62
CHAPTER 9
STRUCTURE AND UNION

Program 68
Structures.
Aim:
To create a C program for structure.
Source code:

Output:

Program 69
Structure with arrays.
Aim:
To create a program in C for Employee details using structure with arrays.
Source code:

63
Output:

64
Program 70
Structure with pointer.
Aim:
Create a program in C to demonstrate structure with pointer
Source code:

Output:

Program 71
Structure with function.
Aim:
Create a program in C to demonstrate structure with function.
Source code:

65
Output:

Program 72
Union
Aim:
Create a program in C to demonstrate union.
Source code:

66
Output:

67
CHAPTER 10
ANNA UNIVERSITY ADDITIONAL PROGRAMS

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

You might also like