0% found this document useful (0 votes)
60 views4 pages

C Programming Examples and Exercises

This document provides examples of C programming concepts including printf and scanf statements, if/else statements, looping structures, nested loops, arrays, pointers, built-in functions, user-defined functions, structures, and file handling. It includes over 10 examples for each concept to demonstrate different ways to apply the programming techniques.

Uploaded by

Satyam Charola
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views4 pages

C Programming Examples and Exercises

This document provides examples of C programming concepts including printf and scanf statements, if/else statements, looping structures, nested loops, arrays, pointers, built-in functions, user-defined functions, structures, and file handling. It includes over 10 examples for each concept to demonstrate different ways to apply the programming techniques.

Uploaded by

Satyam Charola
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
  • Basic Input and Output
  • Conditional Statements
  • Arithmetic Operations
  • Looping Structures
  • Arrays and Pointers
  • Functions
  • Structures and File Handling

[Link] () Statement.

.(1) Print "aptech".


.(2) Print Your Name On screen.
.(3) Print Address on screen.
.(4) Datatypes

[Link]() statement.
.(1) Enter the value of an integer variable and datatypes in scanf print it on
screen .
.(2) Input two numbers and prints its square and cube.
.(3) Input two numbers and prints its Addition, Substraction, Multiplication,
and Division.
.(4) Verify the formula of Simple Interest.
si=p*r*n/100
.(5) Input a rupees and prints its value converted into Dollar.
.(6) Input a Number of Chairs and its Total Cost and Prints the Cost of Each
chair.
.(7) Verify the formula t=((v+s)+(l-m)*l)
.(8) Calculate the area of circle.(A=PI*R*R)
.(9) Calculate the area of Triangel.(area = altitude * base /2;)
.(10) Verify the formula c=(a+b)*(a+b)

[Link]...Else statement:

.(1) Enter value and check it is less, equal or greter than zero.
.(2) Input 2 number and print the maximum among them.
.(3) Input 2 number and print the maximum among them. (without If statement)
.(4) Input 2 number and print the minimum among them.
.(5) Input 2 number and print the minimum among them. (without If statement)
.(6) Input 3 number and print the maximum among them.
.(7) Print & calculate addition, substrection, multiplication & division
of two value in one progarm using if() statement.
.(8) Input a number & print the number is odd(ak) or even(bk).
.(9) Get a character from user and print tell that is it vowel(a,e,i,o,u) or
consonant.

Make all above programs using switch() statement.

-> Get A Character From User And Decide Is It Vowel Or Consonant(Use Switch)
-> Get A Program To Show Calculations (Use Switch)
-> Get A Program To Get How Many Days Are There In Selected Month(Using Switch)
-> Get A Program To Find Week Days(Use Switch)
-> Get A Program To Find Wheather The Entered Gender Alphabet Is Male Or
Female(Using Switch)

[Link] structure (while, do...while, for)


.(1) print 1 to 10.
.(2) print 1,3,5,7,9,
.(3) print 1,2,4,8,16,32,64,
.(4) print first 100 even([Link]) number.
.(5) print first 100 odd([Link]) number.
.(6) print first 50 natural number.
.(7) print the total of 1 to 50.
.(8) print the total of first 100 even numbers.
.(9) Except one no from user and display Reverse if it.
.(10) Except one no from user and display sum of its digits.
.(11) Except one no from user and display its factorial.
.(12) Except one no from user and find if it is prime or not.
.(13) Except one no from user and find if it is Armstrong or not. 121---- 1*1*1--
+ -- 2*2*2-- + -- 1*1*1
.(14) Print 0 1 1 2 3 5 8 13 21 34 55 � (Fibonaci series)
.(15).pelimdrom number

5. Nested looping structure.


Pattern-I Pattern-II Pattern-III
(1).
*
**
***
****
*****
(2).

12345
12345
12345
12345
12345

(3).
11111
22222
33333
44444
55555

(4).
A A A A A
B B B B B
C C C C C
D D D D D
E E E E E

(5).
1
22
333
4444
55555

(6).
1
12
123
1234
12345

(7).
5
54
543
5432
54321
(8).
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

(9).
a
b c
d e f
g h i j
k l m n o

Implement all above program with Pattern-II & Pattern-III

(10).
12345
1234
123
12
1

(11).
12345
2468
369
48
5

(12).
55555
4444
333
22
1

6. Array & Pointer

.[Link] 5 numbers in an array and display it.


.[Link] 10 numbers in an array, sort it and display it.
.[Link] 4 numbers in 2-d array......
.[Link] 9 numbers in form of matrix. (3 X 3 matrix)......
.[Link] 10 numbers in an array and store it in another array.
.[Link] 10 numbers in an array and find total of positive and negative no.
.[Link] 5 numbers in an array and find maximum and minimum value.
.[Link] 5 numbers in an array and calculate average of it.r
.[Link] a program that shows concept of pointer.
.[Link] a program to swap two values (Pass by value)

7. Built in functions
[Link] one no and display its absolute value.
[Link] one no and display its log.
[Link] one no and display its square root.
[Link] 2 no from user and display base^exp.(2^3=8)
[Link] Ceil Using Math Function.
[Link] Floor Using Math Function.
[Link] a string from user and display length of that string.
[Link] a string from user and copy it in another variable.
[Link] two strings from user and compare weather both are same or not.
[Link] a string from user and display reverse of it.
[Link] two strings from user and merge it into first string.
[Link] a string from user and display it in lower case.
[Link] a string from user and display it in upper case.

8. User Define Function


.[Link] a user define function named sum which accept 2 arguments (of integer
type) and return the sum of them.
.[Link] a user define function named avg which pass array as argument (of integer
type) and return its average value.
.[Link] a user define function named evenodd which accept one argument (of
integer type) and return if the number is even or odd.
.[Link] a user define function named fact which accept one argument (of integer
type) and return the factorial of given number.
.[Link] a user define function named prime which accept one argument (of integer
type) and display that the no is prime or not. (no need to return value)
.[Link] a user define function named triangle which accept one argument (of
integer type) and display a triangle like.
(If you have given 4 as argument)
1
1 2
1 2 3
1 2 3 4

[Link]
[Link] a structure named student that have member variable roll no, name, m1,
m2, m3, sum, average, and grade. Program ask for roll no, name, m1, m2, m3 and
calculate sum, average and grade.
[Link] above structure create an array of structure.
[Link] above structure create an array of structure and use pointer.
[Link] a program to find size of structure.

10. File Handling


[Link] a programme to create a file.
[Link] a programme to read a file.
[Link] a programme to copy one file to another file.
[Link] a programme to add data into an existing file.

Common questions

Powered by AI

A user-defined function can calculate the average of numerical arrays by accepting the array as an argument, iterating through the elements to sum them, and dividing by the number of elements to find the average. This function's regular use in data analysis is crucial for statistical operations, performance metrics, and summarizing data insights .

Nested looping structures can be used to generate complex patterns by controlling multiple loop counters to iterate over rows and columns. An advanced pattern example is a triangle of increasing numbers, which uses nested loops to print each row with an incrementing number of elements. For instance, the triangle pattern with input 4 would be generated as follows: 1; 1 2; 1 2 3; 1 2 3 4 .

To implement a loop that checks for Armstrong numbers, repeatedly extract each digit of the number, raise it to the power equal to the number of digits, sum these powers, and compare the sum to the original number. This process is significant as it introduces programmers to concepts like digit extraction, power calculations, and iterative comparison, enhancing problem-solving skills .

A user-defined function can determine if a number is prime by accepting an integer argument and using a loop to check divisibility from 2 to the square root of the number. If any divisor is found, the number is not prime. The function does not return a value but directly prints the result as 'prime' or 'not prime' based on its findings .

The Fibonacci sequence is generated using a loop by initializing the first two numbers and using the loop to calculate subsequent numbers as the sum of the previous two. This simple yet recursive pattern helps in understanding iterative algorithms, memory management, and optimization techniques essential in dynamic programming .

To verify the formula for Simple Interest using a scanf() statement and mathematical operations, you can create a program that accepts the principal amount (p), the rate of interest (r), and the time period (n) as inputs from the user via the scanf() function. Then you calculate the simple interest using the formula si = p * r * n / 100, and print the result to verify the calculation .

The switch() statement can map each character to its case by using 'case' labels for vowels ('a', 'e', 'i', 'o', 'u') to print 'vowel', and a 'default' case for consonants or other characters. Logical operations are minimized as explicit checks for vowels are handled directly within cases .

Pointers in arrays allow direct memory management by storing the address of the first element of an array, enabling efficient data traversal, modification, and complex data manipulation tasks directly through memory operations without indexing overhead. They provide a powerful tool for optimizing performance and facilitating dynamic data structures .

Built-in functions facilitate mathematical operations, like calculating the square root, by providing optimized and ready-to-use methods such as sqrt() in standard libraries, ensuring precision and efficiency. The advantage is reduced coding complexity, increased reliability, and leveraging thoroughly tested implementations .

To copy data from one file to another, open the source file in read mode and the target file in write mode. Read the content of the source file and write it to the destination file using file handling functions such as fread() and fwrite(). Close both files after the operation is complete. File copying is important in programming for data backup, transferring data, and preserving data integrity during operations .

1.Printf () Statement.
.(1) Print "aptech".
.(2) Print Your Name On screen.
.(3) Print Address on screen.
.(4) Datatypes 
2.S
.(13) Except one no from user and find if it is Armstrong or not.   121---- 1*1*1--
+ -- 2*2*2-- + -- 1*1*1  
.(14) Print 0 1
(8).
1
2  3
4  5  6
7  8  9  10
11 12 13 14 15
(9).
a
b c
d e f
g h i j
k l m n o
Implement all above program with Pattern-II
3.Accept one no and display its square root.
4.Get 2 no from user and display base^exp.(2^3=8)
5.Get Ceil Using Math Function

You might also like