KEMBAR78
CSA02 - C Programming - Easy Questions | PDF | Numbers | Text File
0% found this document useful (0 votes)
6 views19 pages

CSA02 - C Programming - Easy Questions

Uploaded by

akshayamohan265
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)
6 views19 pages

CSA02 - C Programming - Easy Questions

Uploaded by

akshayamohan265
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/ 19

Easy Questions

1. Compile and Execute the C program to calculate Arithmetic Operators Functions


such as Pow(x, n), Add(x, n), Sub(x, n), Mul(x, n), Div(x, n), where x and n are the two
operands. Get the input and choice from the user.
Sample Input:
X= 2
N= 4
Choice : 2
Sample Output:
Add(X, N) = 6

Test cases:
1. X= 0, N= 4
2. X= 5, N= 0
3. X = -3 , N = 3
4. X= 0, N= 0
5. X = 123, N = 123

2. Compile and Execute the C program to change all the digits of a number to bring the
digit at the last position to the first position and vice-versa using loop. Get the input
from user.
Sample Input:
Number: 14567
Sample Output:
Reverse Number: 76541
Test cases:
1. -45721
2. 000
3. AD1947
4. ! @#$%
5. 145*999=144855

3. Compile and Execute the C program to find the Factorial of a positive integer
(number) is the sum of multiplication of all the integers smaller than that positive
integer. N! is denoted as factorial of N. For example, factorial of 5 is 5 * 4 * 3 * 2 * 1
which equals to 120.

Sample Input:
N= 5
Sample Output:
120

Test cases:
1. N= 0
2. N = -5
3. N= 1
4. N= Q
5. N = 3A

4. Suppose the number to be check is 6: The user has provided one number that is
‘ 6’ , now we have to think and calculate by which other numbers we can divide ‘ 6’ and
get the remainder as ‘ 0’ . If the remainder is zero, then we can say that ‘ 6’ is
divisible by that number and we can take this number into consideration to get the
sum by using some more other numbers. Compile and execute the above C program.

Sample Input:
Given Number: 6

Sample Output:
Its a Perfect Number

Test cases:
1. 17
2. 26!
3. 143
4. 84. 1
5. -963
5. Compile and Execute the C program were the user to enter an integer to compute the
square, cube of the given decimal number using standard method.

Sample Input:
Given Number: 0. 6

Sample Output:
Square Number: 0. 36
Cube Number: 0. 216

Test cases:
1. 12
2. 0
3. -0. 5
4. 14. 25
5. -296

6. Compile and Execute the C program to find whether the person is eligible for vote or
not. And if that particular person is not eligible, then print how many years are left to
be eligible.

Sample Input:
Enter your age: 7

Sample output:
You are allowed to vote after 11years
Test cases:
1. 25
2. Eighteen
3. 12
4. -18
5. 34. 5
7. Compile and Execute the C program to check whether the number divisible by 2,
then print the given number even otherwise odd. Check and display the output on the
screen.

Sample Input:
Enter the number : 6561

Sample Output:
The given number is odd

Test cases:
1. 0
2. -1254
3. A144
4. 145. 23
5. 23. 456

8. Compile and Execute the C program to print numbers from P to Q but except
the digit R using continue statement. Check and display the output on the screen.
Sample Input:
P = 60
Q = 70
R= 3

Sample Output:
Numbers are = 60, 61, 62, 64, 65, 66, 67, 68, 69, 70

Test cases:
1. P = 200, Q = 200, R = 5
2. P = 100, Q = 200, R = 0
3. P = -100, Q = 100, R = 5
4. P = 1073, Q = 1075, R = 4
5. P = 444, Q = 499, R = 4
9. Write a program to print the following pattern. Get the number of rows from the user.

Sample input:

Number of Rows: 5

Sample output:

2 1

3 2 1

4 3 2 1

5 4 3 2 1

Test case:

Number of Rows: 0. 5

Test Condition:

Number of Rows: 5

Sample Output:

4 1

9 4 1

16 9 4 1

25 16 9 4 1

10. Write a program to print the below pattern.


Sample Input:
Number of line: 4
Sample output:
1
2 2
3 3 3
4 4 4 4
Test case:
Number of line: 0. 4
Test condition:
Number of line: 4
0. 1
0. 2 0. 2
0. 3 0. 3 0. 3
0. 4 0. 4 0. 4 0. 4

11. Compile and Execute the C program to print the multiplication table of number m up
to n.
Sample Input:
M= 4
N= 5
Sample Output:
1x4=4
2x4=8
3x4=12
4x4=16
5x4=20
Test cases:
1. M= 6, N = -3
2. M= -3, N= 5
3. M= 4, N= 0
4. M= 0, N= 0
5. M= -5, N = -5

12. Write a program to find the number of composite numbers in an array of elements
Sample Input; :
Array of elements = {16, 18, 27, 16, 23, 21, 19}
Sample Output:

Number of Composite Numbers = 5


Test cases:
1. Array of elements = {26, 28, 37, 26, 33, 31, 29}
2. Array of elements = {1. 6, 1. 8, 2. 7, 1. 6, 2. 3, 2. 1, . 19}
3. Array of elements = {0, 160, 180, 270, 160, 230, 210, 190, 0}
4. Array of elements = {200, 180, 180, 270, 270, 270, 190, 200}
5. Array of elements = {100, 100, 100, 100, 100, 100, 100, 100}

13. Write a program to print the number of negative numbers in an array of numbers
Sample Input;
Array of elements = {16, -18, 27, -16, 23, -21, 19}
Sample Output:

Number of negative numbers in Array elements = 3


Test cases:
1. Array of elements = {-26, 28, 37, -26, 33, -31, -29}
2. Array of elements = {1. 6, 1. 8, 2. 7, -1. 6, 2. 3, -2. 1, . 19}
3. Array of elements = {0, 160, 180, 270, 160, 230, 210, 190, 0}
4. Array of elements = {-16, 2. 8, -7, -1. 5, 2. 8, -2. 8, -. 19}
5. Array of elements = {-160, -160, -180, -270, -160, -230, -210, 1-90, 0}

14. Write a program to reverse an array


Sample Input;
Array of elements = {16, 18, 27, 16, 23, 21, 19}
Sample Output:

Reverse Array elements = {19, 2123, 16, 27, 18, 16}


Test cases:
1. Array of elements = {26, 28, 37, 26, 33, 31, 29}
2. Array of elements = {1. 6, 1. 8, 2. 7, 1. 6, 2. 3, 2. 1, . 19}
3. Array of elements = {0, 160, 180, 270, 160, 230, 210, 190, 0}
4. Array of elements = {200, 180, 180, 270, 270, 270, 190, 200}
5. Array of elements = {100, 100, 100, 100, 100, 100, 100, 100}

15. The transpose of a matrix is found by interchanging its rows into columns or columns
into rows. Write a program to find the Transpose of a given Matrix.
Sample input:
12
5 3
Output:
15
23

16. Program to remove duplicates from the sorted array


Sample Input:
rray = {15, 14, 25, 14, 32, 14, 31}
Sample Output:
ed Array = {14, 15, 25, 31, 32}
Test cases:
1. {16, 16, 16 16, 16}
2. {0, 0, 0, 0}
3. {-12, -78, -35, -42}
4. {1, 2, 3, 7, 8, 9, 4, 5, 6}
5. {1-2, 2-3, 3-4, 4-5, 5-6}

17. Write a program to search the given element and display its position in a linear array.
Sample Input; :
Array of elements = {16, 18, 27, 16, 23, 21, 19}
Element to search = 23
Sample Output:

Given element 23 is found at 5 th position


18. Write a program in C to add two numbers using pointers.

Test Data :

Input the first number : 5

Input the second number : 6

Expected Output :

The sum of the entered numbers is : 11


Test cases:
1. X= 0, N= 4
2. X= 5, N= 0
3. X = -3 , N = 3
4. X= 0, N= 0
5. X = 123, N = 123

19. Write a program in C to find the maximum number between two numbers using a
pointer.

Test Data :

Input the first number : 5

Input the second number : 6

Expected Output :

a. is the maximum number.


Test cases:
1. X = 0 , N = 4
2. X= 5, N= 0
3. X = -3 , N = 3
4. X= 0, N= 0
5. X = 123, N = 123

20. Write a program in C to find the largest element using Dynamic Memory Allocation.

Test Data :

Input total number of elements(1to 100): 5

Number 1: 5
Number 2: 7

Number 3: 2

Number 4: 9

Number 5: 8

Expected Output :

The Largest element is : 9. 00


Test Cases:
1. N = 0, 1, 3, 8, 7, -5
2. N = 5, 5, 5, 5, 5, 5. 4
3. N = -2, 2, -2, 4, -4
4. N = -5, 55, 30, 0. 5
5. N = 0. 2, 2, 4, 5, 8

21. Write a program in C to Calculate the length of the string using a pointer

Test Data :
Input a string : SAVEETHA

Expected Output :
The length of the given string saveetha is : 8
22. Write a program in C to compute the sum of all elements in an array using pointers.

Test Data :

Input the number of elements to store in the array (max 10) : 5

Input 5 number of elements in the array :

element - 1: 2

element - 2 : 3

element - 3 : 4

element - 4 : 5
element - 5 : 6

Expected Output :

The sum of array is : 20


Test cases:
1. A = 11, B = 11
2. A = 20, B = 10
3. A = 0, B = 0
4. A = -5, B = 5
5. A = 7, B = -12

23. Write a program in C to find the square of any number using the function.

Test Data :

Input any number for square : 20

Expected Output :

The square of 20 is : 400. 00


Test cases:
1. 12
2. 0
3. -0. 5
4. 14. 25
5. -296

24. Write a program in C to check a given number is even or odd using the function.

Test Data :

Input any number : 5

Expected Output :

The entered number is odd.


Test cases:
1. N = 16
2. N = -8
3. N= 0
4. N = -10. 01
5. N = 11. 22
25. Write a program in C to get the largest element of an array using the function.

Test Data :

Input the number of elements to be stored in the array : 5

Input 5 elements in the array :

element - 0 : 1

element - 1: 2

element - 2 : 3

element - 3 : 4

element - 4 : 5

Expected Output :

The largest element in the array is : 5

Test Cases:
6. N = 0, 1, 3, 8, 7, -5
7. N = 5, 5, 5, 5, 5, 5. 4
8. N = -2, 2, -2, 4, -4
9. N = -5, 55, 30, 0. 5
10. N = 0. 2, 2, 4, 5, 8

26. Write a program to calculate Pow(x, n), Add(x, n), Sub(x, n), Mul(x, n), Div(x, n)? Get
the input and choice from the user.
Sample Input:
X= 2
N= 4
Choice : 2
Sample Output:
Add(X, N) = 6
Test cases:
27. X= 0, N= 4
28. X= 5, N= 0
29. X = -3 , N = 3
30. X= 0, N= 0
31. X = 123, N = 123

27. Write a program to store information of n students ( Roll No, Name, Marks ) in
structure and display it.
Sample Input :
Enter No. Students: 1
Enter student 1Roll No. , name, Marks: 2001, AAA, 98

Sample Output:
Student 1details:
Roll no. : 2001
Name: AAA
Marks: 98

Test Cases:

No. Student : 4 (Any details of student)


No. Student: 5
No. Student: 1(2005, 4563, 62)
No. Student: A
No. Student: 1( xxx, John, 98. 2)

28. Write a program to input and print student data using pointer object of structure.
Sample Input :
Enter No. Students: 1
Enter student 1Roll No. , name, Marks: 2001, AAA, 98

Sample Output:
Student 1details:
Roll no. : 2001
Name: AAA
Marks: 98

Test Cases:

No. Student : 4 (Any details of student)


No. Student: 5
No. Student: 1(2005, 4563, 62)
No. Student: A
No. Student: 1( xxx, John, 98. 2)

29. Write a C Program to print the book details( Title , author, Subject, bookid - s
structure member) using structures as Function Arguments
Sample input:
Enter the number of books: 1
Enter the book 1title : C Programming
Enter the book 1Author: Nuha Al
Enter the Book 1subject : C Programming Tutorial
Enter the Book 1book_id : 6495407

Sample output:
Book 1title : C Programming
Book 1author : Nuha Ali
Book 1subject : C Programming Tutorial
Book 1book_id : 6495407

Test Case:

No. books : 4 (Any details of books)


No. books: 5
No. books: 1(2005, aaa ccc, 62)
No. Student: A
No. Student: 1( xxx, 123, yyyy 98. 2)

30. Write a C Program to Calculate Total and Percentage marks of a student using
structure.
Input:
Enter RollNo, : 12
Name : rama
three sub marks 30 40 50

Output:
rollno=12
Name=rama
m1=30 m2=40 m3=50 total=120 per=40. 000000

Test Case

24, AA, 77, 65, 75


22, BB, 56, 62, 50
23, CC. 42, 45, 38
25, Annie 55, 65, 74
26, joy. 85, 84, 87

31. Write a program to store information of n students ( Roll No, Name, Marks ) in
Union and display it.
Sample Input :
Enter No. Students: 1
Enter student 1Roll No. , name, Marks: 2001, AAA, 98

Sample Output:
Student 1details:
Roll no. : 2001
Name: AAA
Marks: 98

Test Cases:

No. Student : 4 (Any details of student)


No. Student: 5
No. Student: 1(2005, 4563, 62)
No. Student: A
No. Student: 1( xxx, John, 98. 2)

32. Write a C program that uses functions and structures to perform the addition of
complex numbers
Input:
Enter First complex number:
"enter real part of complex number: 2
enter Imaginary part of complex number: 3

Enter Second complex number:


"enter real part of complex number: 4
enter Imaginary part of complex number: 5

Output:
Addition of complex number :
Real part of addition : 6
Imaginary part of addition : 8

Test Cases:
2+5i, a+4i
3+6i. 6+4i
8+4i. 12+7i
15-4i. 6-2i
3-8i. 18-6i

33. Write a program to display information of n students ( Roll No, Name, Marks ) using
Union
Sample Input :
Enter No. Students: 1
Enter student 1Roll No. , name, Marks: 2001, AAA, 98

Sample Output:
Student 1details:
Roll no. : 2001
Name: AAA
Marks: 98

Test Cases:

No. Student : 4 (Any details of student)


No. Student: 5
No. Student: 1(2005, 4563, 62)
No. Student: A
No. Student: 1( xxx, John, 98. 2)

34. Write a C program that uses functions and structures to perform the Subtracting
complex numbers
Enter First complex number:
"enter real part of complex number: 2
enter Imaginary part of complex number: 3

Enter Second complex number:


"enter real part of complex number: 4
enter Imaginary part of complex number: 5
Output:
Subtraction of complex number :
Real part of Subtraction : -2
Imaginary part of Subtraction: -2

Test Cases:

2+5i, a+4i
3+6i. 6+4i
8+4i. 12+7i
15-4i. 6-2i
3-8i, 8-6i
35. Write a C program that uses functions and structures to perform the following
operations:
i) Multiplication of two complex numbers

Enter First complex number:


"enter real part of complex number: 2
enter Imaginary part of complex number: 3

Enter Second complex number:


"enter real part of complex number: 4
enter Imaginary part of complex number: 5

Test Cases:

2+5i, a+4i
3+6i. 6+4i
8+4i. 12+7i
15-4i. 6-2i
3-8i. 18-6i

36. Write a C program that uses functions and structures to perform the following
operations: . Division of two complex numbers

Enter First complex number:


"enter real part of complex number: 2
enter Imaginary part of complex number: 3

Enter Second complex number:


"enter real part of complex number: 4
enter Imaginary part of complex number: 5

Test Cases:

2+5i, a+4i
3+6i. 6+4i
8+4i. 12+7i
15-4i. 6-2i
3-8i. 18-6i
37. Write a program to read name and roll number of n number of students from user
and store them in a file.
Sample input
Enter the roll number: 101
Enter the name : aaaa
Sample Output:
(File Name Student. txt )

101
Aaaa
Test Case:

1. 105, ccccc
2. 101A, invalid
3. 10_1, invalid
4. 110, bbbb
5. 100, dddd

38. Write a C program to display the contents of a file.


Enter the file name : sample. txt

Output:
this is myPractice Program

Test Case:
Sample. txt
sample1. txt
S123. txt
S@12. txt
Sample S. txt

39. Write a C program to copy the contents of one file to another.

INPUT: a. txt

OUTPUT: 57 bytes copied from ‘ a. txt’ to ‘ b. txt’


2 files closed

Test Case
Sample. txt
sample1. txt
S123. txt
S@12. txt
Sample S. txt
40. Write a C program to merge two files into a third file.

Enter first file name : a. txt


Enter second file name: b. txt
Enter destination file name: c. txt

Output:
File Merging Sucessfull

Test Cases
Sample. txt , a. txt
sample1. txt, a. txt
S123. txt, a. txt
S@12. txt, a. txt
Sample S. txt, a. txt

41. Write a C program to reverse the first n characters in a file by specifying the file name
and n on the command line

Input: source. c

this is source
Ouput:
source. c
this is source ecruos si siht

Test Case
Sample. c
sample1. c
S123. c
S@12. c
Sample S. c

42. Write a C program to handle the situation where the file required to be read is not
available in the store.
Input:
Enter Name of file to open for reading: text. txt
Output:
The file is not found.

43. File Manipulation - Open a file using the function fopen() and store the reference of
the file in a FILE pointer. Read contents of the file using any of these functions fgetc(),
fgets(), fscanf(), or fread(). File close the file using the function fclose(). - Write a
program in C to implement the sequence of operations defined as above.
Input:
Enter name of file to open: text. txt
Enter the text: This is a test text.
Output:
The file is successfully opened.
Test Conditions:
a. Test. txt
b. File1. c
c. File empty

44. Write a C program to read entire contents from afile and print the contents in to the
screen.

Input:

Enter the name of file for reading: firsttest. txt

Output:

There is everything inside you expected to be in this file.

45. Write a C program to read contents from the keyboard and write it into the file.
Input:

Enter text to input: This is a test information for checking to be displayed.

Output:

Information written on Screen:

This is a test information for checking to be displayed.


46. Write a C program to read the content from the keyboard and append the content in
to the existing file.
Input:
Enter the name of file for reading: test1. txt
Enter number to append: 66 77 88 99
Output:
11 22 33 44 55
66 77 88 99

Test Conditions:
a. File. Text
b. Test1. text
c. Empty file.

You might also like