C Exercises - Practice Questions with Solutions for C Programming (2024)

Table of Contents
C Programming Exercises Q1: Write a Program to Print “Hello World!” on the Console. Click here to view the solution. Q2: Write a Program to find the Sum of two numbers entered by the user. Click here to view the solution. Q3: Write a Program to find the size of int, float, double, and char. Click here to view the solution. Q4: Write a Program to Swap the values of two variables. Click here to view the solution. Q5: Write a Program to calculate Compound Interest. Click here to view the solution. Q6: Write a Program to check if the given number is Even or Odd. Click here to view the solution. Q7: Write a Program to find the largest number among three numbers. Click here to view the solution. Q8: Write a Program to make a simple calculator. Click here to view the solution. Q9: Write a Program to find the factorial of a given number. Click here to view the solution. Q10: Write a Program to Convert Binary to Decimal. Click here to view the solution. Q11: Write a Program to print the Fibonacci series using recursion. Click here to view the solution. Q12: Write a Program to Calculate the Sum of Natural Numbers using recursion. Click here to view the solution. Q13: Write a Program to find the maximum and minimum of an Array. Click here to view the solution. Q14: Write a Program to Reverse an Array. Click here to view the solution. Q15: Write a Program to rotate the array to the left. Click here to view the solution. Q16: Write a Program to remove duplicates from the Sorted array. Click here to view the solution. Q17: Write a Program to search elements in an array (using Binary Search). Click here to view the solution. Q18: Write a Program to reverse a linked list. Click here to view the solution. Q18: Write a Program to create a dynamic array in C. Click here to view the solution. Q19: Write a Program to find the Transpose of a Matrix. Click here to view the solution. Q20: Write a Program to concatenate two strings. Click here to view the solution. Q21: Write a Program to check if the given string is a palindrome string or not. Click here to view the solution. Q22: Write a program to print the first letter of each word. Click here to view the solution. Q23: Write a program to reverse a string using recursion Click here to view the solution. Q24: Write a program to Print Half half-pyramid pattern. Click here to view the solution. Q25: Write a program to print Pascal’s triangle pattern. Click here to view the solution. Q26: Write a program to sort an array using Insertion Sort. Click here to view the solution. Q27: Write a program to sort an array using Quick Sort. Click here to view the solution. Q28: Write a program to sort an array of strings. Click here to view the solution. Q29: Write a program to copy the contents of one file to another file. Click here to view the solution. Q30: Write a program to store information on students using structure. Click here to view the solution. Conclusion Frequently Asked Questions (FAQs) Q1. What are some common mistakes to avoid while doing C programming exercises? Q2. What are the best practices for beginners starting with C programming exercises? Q3. How do I debug common errors in C programming exercises? Please Login to comment... FAQs

The best way to learn C programming language is by hands-on practice. This C Exercise page contains the top 30 C exercise questions with solutions that are designed for both beginners and advanced programmers. It covers all major concepts like arrays, pointers, for-loop, and many more.

C Exercises - Practice Questions with Solutions for C Programming (1)

So, Keep it Up! Solve topic-wise C exercise questions to strengthen your weak topics.

C Programming Exercises

The following are the top 30 programming exercises with solutions to help you practice online and improve your coding efficiency in the C language. You can solve these questions online in GeeksforGeeks IDE.

Q1: Write a Program to Print “Hello World!” on the Console.

In this problem, you have to write a simple program that prints “Hello World!” on the console screen.

For Example,

Output: Hello World!

Click here to view the solution.

Q2: Write a Program to find the Sum of two numbers entered by the user.

In this problem, you have to write a program that adds two numbers and prints their sum on the console screen.

For Example,

Input: Enter two numbers A and B : 5 2Output: Sum of A and B is: 7

Click here to view the solution.

Q3: Write a Program to find the size of int, float, double, and char.

In this problem, you have to write a program to print the size of the variable.

For Example,

Output: Size of int = 4

Click here to view the solution.

Q4: Write a Program to Swap the values of two variables.

In this problem, you have to write a program that swaps the values of two variables that are entered by the user.

C Exercises - Practice Questions with Solutions for C Programming (2)

Swap two numbers

For Example,

Input: Enter Value of x: 5Enter Value of y: 10Output: After Swapping: x = 10, y = 5

Click here to view the solution.

Q5: Write a Program to calculate Compound Interest.

In this problem, you have to write a program that takes principal, time, and rate as user input and calculates the compound interest.

For Example,

Input: Enter Principal (amount): 1200Enter Time: 2Enter Rate: 5.4Output: Compound Interest is: 133.099243

Click here to view the solution.

Q6: Write a Program to check if the given number is Even or Odd.

In this problem, you have to write a program to check whether the given number is even or odd.

For Example,

Input: Enter a Number: 2Output: even

Click here to view the solution.

Q7: Write a Program to find the largest number among three numbers.

In this problem, you have to write a program to take three numbers from the user as input and print the largest number among them.

For Example,

Input: Enter a Number1: 5Enter a Number2: 50Enter a Number3: 10Output: Largest number is: 50

Click here to view the solution.

Q8: Write a Program to make a simple calculator.

In this problem, you have to write a program to make a simple calculator that accepts two operands and an operator to perform the calculation and prints the result.

For Example,

Input: Enter an operator (+, -, *, /): +Enter operand 1: 7Enter operand 2: 8Output: 15.0

Click here to view the solution.

Q9: Write a Program to find the factorial of a given number.

In this problem, you have to write a program to calculate the factorial (product of all the natural numbers less than or equal to the given number n) of a number entered by the user.

C Exercises - Practice Questions with Solutions for C Programming (3)

Factorial

For Example,

Input: Enter a number: 5Output: Factorial of 5 is: 120

Click here to view the solution.

Q10: Write a Program to Convert Binary to Decimal.

In this problem, you have to write a program to convert the given binary number entered by the user into an equivalent decimal number.

For Example,

Input: Enter a binary number: 10101001Output: Decimal number is: 169

Click here to view the solution.

Q11: Write a Program to print the Fibonacci series using recursion.

In this problem, you have to write a program to print the Fibonacci series(the sequence where each number is the sum of the previous two numbers of the sequence) till the number entered by the user using recursion.

C Exercises - Practice Questions with Solutions for C Programming (4)

Fibonacci Series

For Example,

Input: Enter value of n: Output: 0 1 1 2 3 5 8 13 21 

Click here to view the solution.

Q12: Write a Program to Calculate the Sum of Natural Numbers using recursion.

In this problem, you have to write a program to calculate the sum of natural numbers up to a given number n.

For Example,

Input: Enter value of n: 10Output: 55 

Click here to view the solution.

Q13: Write a Program to find the maximum and minimum of an Array.

In this problem, you have to write a program to find the maximum and the minimum element of the array of size N given by the user.

For Example,

Input: arr[] = {4, 7, 2, 1, 9}Output:Minimum element is: 1Maximum element is: 9

Click here to view the solution.

Q14: Write a Program to Reverse an Array.

In this problem, you have to write a program to reverse an array of size n entered by the user. Reversing an array means changing the order of elements so that the first element becomes the last element and the second element becomes the second last element and so on.

C Exercises - Practice Questions with Solutions for C Programming (5)

Reverse an array

For Example,

Input: arr[] = {1, 2, 3, 4, 5 ,6} , n = 6Output:Reversed array is: 6 5 4 3 2 1 

Click here to view the solution.

Q15: Write a Program to rotate the array to the left.

In this problem, you have to write a program that takes an arrayarr[]of sizeN from the user and rotates the array to the left (counter-clockwise direction) byDsteps, whereDis a positive integer.

For Example,

Input: arr[] = {1, 2, 3, 4, 5}, D = 2Output:After rotation: 3 4 5 1 2

Click here to view the solution.

Q16: Write a Program to remove duplicates from the Sorted array.

In this problem, you have to write a program that takes a sorted arrayarr[]of sizeN from the user and removes the duplicate elements from the array.

For Example,

Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5}Output:array after removal of duplicates: 1 2 3 4 5

Click here to view the solution.

Q17: Write a Program to search elements in an array (using Binary Search).

In this problem, you have to write a program that takes an arrayarr[]of sizeN and a target value to be searched by the user. Search the target value using binary search if the target value is found print its index else print ‘element is not present in array‘.

For Example,

Input: arr[] = { 2, 3, 4, 10, 40 }target: 10Output: Element is present at index 3

Click here to view the solution.

Q18: Write a Program to reverse a linked list.

In this problem, you have to write a program that takes apointer to the head node of a linked list, you have to reverse the linked list and print the reversed linked list.

For Example,

Input : 1->2->3->4->NULLOutput : Reversed Linked list: 4->3->2->1->NULL

Click here to view the solution.

Q18: Write a Program to create a dynamic array in C.

In this problem, you have to write a program to create an array of size n dynamically then take n elements of an array one by one by the user. Print the array elements.

For Example,

Output : Elements of the array are: 1, 2, 3, 4, 5,

Click here to view the solution.

Q19: Write a Program to find the Transpose of a Matrix.

In this problem, you have to write a program to find the transpose of a matrix for a given matrix A with dimensions m x n and print the transposed matrix. The transpose of a matrix is formed by interchanging its rows with columns.

For Example,

Input : matrix A: { {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4} }Output : Result matrix is : 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4

Click here to view the solution.

Q20: Write a Program to concatenate two strings.

In this problem, you have to write a program to read two strings str1 and str2 entered by the user and concatenate these two strings. Print the concatenated string.

For Example,

Input: str1 = "hello", str2 = "world"Output: helloworld

Click here to view the solution.

Q21: Write a Program to check if the given string is a palindrome string or not.

In this problem, you have to write a program to read a string str entered by the user and check whether the string is palindrome or not. If the str is palindrome print ‘str is a palindrome’ else print ‘str is not a palindrome’. A string is said to be palindrome if the reverse of the string is the same as the string.

For Example,

Input: Enter str: "abbba"Output: abbba is a palindrome

Click here to view the solution.

Q22: Write a program to print the first letter of each word.

In this problem, you have to write a simple program to read a string str entered by the user and print the first letter of each word in a string.

For Example,

Input: Enter str: Geeks for GeeksOutput: G f G

Click here to view the solution.

Q23: Write a program to reverse a string using recursion

In this problem, you have to write a program to read a string str entered by the user, and reverse that string means changing the order of characters in the string so that the last character becomes the first character of the string using recursion.

C Exercises - Practice Questions with Solutions for C Programming (6)

reverse a string

For Example,

Input: Enter str: GeeksOutput: Reversed string is: skeeG

Click here to view the solution.

Q24: Write a program to Print Half half-pyramid pattern.

In this problem, you have to write a simple program to read the number of rows (n) entered by the user and print the half-pyramid pattern of numbers. Half pyramid pattern looks like a right-angle triangle of numbers having a hypotenuse on the right side.

For Example,

Input: Enter number of rows: 5Output: 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 

Click here to view the solution.

Q25: Write a program to print Pascal’s triangle pattern.

In this problem, you have to write a simple program to read the number of rows (n) entered by the user and print Pascal’s triangle pattern. Pascal’s Triangle is a pattern in which the first row has a single number 1 all rows begin and end with the number 1. The numbers in between are obtained by adding the two numbers directly above them in the previous row.

C Exercises - Practice Questions with Solutions for C Programming (7)

Pascal’s Triangle

For Example,

Input: Enter number of rows: 5Output:  1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

Click here to view the solution.

Q26: Write a program to sort an array using Insertion Sort.

In this problem, you have to write a program that takes an arrayarr[]of sizeN from the user and sorts the array elements in ascending or descending order using insertion sort.

For Example,

Input: arr[] = {12, 11, 13, 5, 6} , N = 5Output:  5 6 11 12 13 

Click here to view the solution.

Q27: Write a program to sort an array using Quick Sort.

In this problem, you have to write a program that takes an arrayarr[]of sizeN from the user and sorts the array elements in ascending order using quick sort.

For Example,

Input: arr[] = { 19, 17, 15, 12, 16, 18, 4, 11, 13 } , N = 9Output: 4 11 12 13 15 16 17 18 19 

Click here to view the solution.

Q28: Write a program to sort an array of strings.

In this problem, you have to write a program that reads an array of strings in which all characters are of the same case entered by the user and sort them alphabetically.

For Example,

Input: arr[] = Output: After sorting: clanguage , geeksforgeeks , geeksquiz

Click here to view the solution.

Q29: Write a program to copy the contents of one file to another file.

In this problem, you have to write a program that takes user input to enter the filenames for reading and writing. Read the contents of one file and copy the content to another file. If the file specified for reading does not exist or cannot be opened, display an error message “Cannot open file: file_name” and terminate the program else print “Content copied to file_name”

For Example,

Input: Enter the filename you want to copy: a.txtOutput: File after copying content: b.txt 

Click here to view the solution.

Q30: Write a program to store information on students using structure.

In this problem, you have to write a program that stores information about students using structure. The program should create various structures, each representing a student’s record. Initialize the records with sample data having data members’ Names, Roll Numbers, Ages, and Total Marks. Print the information for each student.

For Example,

Output: Student Records:Name = Student1Roll Number = 1Age = 12Total Marks = 78.50Name = Student2Roll Number = 5Age = 10Total Marks = 56.84

Click here to view the solution.

Conclusion

We hope after completing these C exercises you have gained a better understanding of C concepts. Learning C language is made easier with this exercise sheet as it helps you practice all major C concepts. Solving these C exercise questions will take you a step closer to becoming a C programmer.

Frequently Asked Questions (FAQs)

Q1. What are some common mistakes to avoid while doing C programming exercises?

Answer:

Some of the most common mistakes made by beginners doing C programming exercises can include missing semicolons, bad logic loops, uninitialized pointers, and forgotten memory frees etc.

Q2. What are the best practices for beginners starting with C programming exercises?

Answer:

Best practices for beginners starting with C programming exercises:

  1. Start with easy codes
  2. Practice consistently
  3. Be creative
  4. Think before you code
  5. Learn from mistakes
  6. Repeat!

Q3. How do I debug common errors in C programming exercises?

Answer:

You can use the following methods to debug a code in C programming exercises

  1. Read the error message carefully
  2. Read code line by line
  3. Try isolating the error code
  4. Look for Missing elements, loops, pointers, etc
  5. Check error online


C Exercises - Practice Questions with Solutions for C Programming (8)

GeeksforGeeks

C Exercises - Practice Questions with Solutions for C Programming (9)

Improve

Next Article

How to Write a Command Line Program in C?

Please Login to comment...

C Exercises - Practice Questions with Solutions for C Programming (2024)

FAQs

Where can I practice C programming questions? ›

Utilize CodeChef's online courses and resources for a structured learning experience and practice through coding exercises and small projects. You can easily run and debug C code using our free Online C Compiler and Debugger.

How can I practice C programming at home? ›

Best practices for beginners starting with C programming exercises:
  1. Start with easy codes.
  2. Practice consistently.
  3. Be creative.
  4. Think before you code.
  5. Learn from mistakes.
  6. Repeat!
Jan 2, 2024

What is the hardest topic in C programming? ›

Tough and easy all are relative and depends on how much clear concept you have on that topic. Still according to me the toughest topic in C language is 'Pointer'. The reason why most of the people find it difficult is: First of all it works with addresses, so most of the time it becomes confusing.

How to solve any C programming problem? ›

Problem solving through Programming In C
  1. Formulate simple algorithms for arithmetic and logical problems.
  2. Translate the algorithms to programs (in C language)
  3. Test and execute the programs and correct syntax and logical errors.
  4. Implement conditional branching, iteration and recursion.

How to get coding answers for free? ›

Quora is the most popular question-and-answer website which is open for all users to share knowledge, ask questions, and give solutions. A lot of tech giants and experts have an account on Quora that can guide programmers and solve their problems. You will find the majority of answers given by experts from their field.

Is C hard for beginners? ›

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Can I learn C in 10 days? ›

Likewise, the educational program isn't excessively intricate or tedious to follow, as all you require is to experience a few subjects every day and you'll cover the whole schedule in basically 10 days. Along these lines, plunge into the C language world and improve your programming abilities for new job openings!

Which app is used to practice C programming? ›

10 Best Apps for Learning C Programming – Overview
S.No.App NameUser Ratings
1GUVI4.3
2Encode4.5
3Sololearn4.6
4Programming Hub4.7
6 more rows
May 7, 2024

Is C harder than Python? ›

Python is easier than C to learn. But C helps to learn the fundamentals of programming while Python focuses on doing the job. Because Python is made in C doesn't mean you need to learn it. It is supposed to be an opposite and make a fast learning environment, unlike C.

Why is C so tough? ›

The C language is less forgiving syntactically and requires significantly more awareness and concentration in regards to putting things in order. Memory management and garbage collection is handled manually whereas other languages have automatic garbage collection.

What is the hardest part of C? ›

Understanding pointers in C is broadly thought of as “the hard part” by new programmers approaching the language for the first time, but BACK IN THE DAY that was truly no big deal.

Where can I practice C questions? ›

Best Websites to Practice C Programming
  • GUVI. CodeKata hosts a diverse range of coding problems used by top companies like Microsoft, Walmart, and Samsung, aimed at sharpening coding skills. ...
  • HackerRank. ...
  • Coderbyte. ...
  • CodeChef. ...
  • LeetCode. ...
  • Exercism. ...
  • Codewars. ...
  • GeeksforGeeks.
Apr 30, 2024

What are the common coding errors in C? ›

Here are seven common errors in C programming and C++ programming.
  • Initialization. Data initialization is always important. ...
  • Name-Hiding. Name-hiding of declarations is a particularly difficult bug. ...
  • Boolean Expressions. ...
  • Logic Flaws. ...
  • Unreachable Code. ...
  • Type Conversions. ...
  • Casting Away CONST.
Jul 12, 2016

In which app we can practice C language? ›

10 Best Apps for Learning C Programming – Overview
S.No.App NameApp Store Link
2EncodeDownload
3SololearnDownload
4Programming HubDownload
5ProgramizDownload
6 more rows
May 7, 2024

Where can I get help for C programming? ›

FavTutor provides online C programming help to students with original quality and professional competency. You can receive instant C programming homework help or assignment help right now. Our experts follow extensive research and help in completing your assignments from scratch.

Where can I practice programming language? ›

10 Best Websites to Practice Coding Online
  • Geektastic. Geektastic has a mixture of multiple-choice and peer-reviewed code challenges to enjoy. ...
  • freeCodeCamp. Complete the coding challenges and build projects for nonprofits. ...
  • Coderbyte. ...
  • DataCamp. ...
  • HackerRank. ...
  • HackerEarth. ...
  • CodinGame. ...
  • TopCoder.

How to study for C programming? ›

Tips and Strategies for Learning C
  1. Learn the variable types. Understand the type of data that you are working with, such as whether it's an integer or a character. ...
  2. Learn the operators. ...
  3. Use standard libraries. ...
  4. Understand error handling. ...
  5. Use a debugger. ...
  6. Look at sample code.

Top Articles
How 'Knock at the Cabin' Differs From the Book It's Based on
New Products | Beauty, Nail & Hair Products
19 Awesome Things to Do in Redmond, Oregon
Kokomoscanner
Fone Tech Cleveland Ms
Www Craigslist Com Wisconsin Milwaukee
Omega Pizza-Roast Beef -Seafood Middleton Menu
Tmobile Ipad 10Th Gen
Craigslist Southern Oregon Coast
Castle Nail Spa (Plano)
The STAR Market - China's New NASDAQ for Rising Star Companies
Bullocks Grocery Weekly Ad
Hessaire Mini Split Remote Control Manual
Comparing Each Tacoma Generation, Which is Best?
Equity Livestock Altoona Market Report
Jennifer Lenzini Leaving Ktiv
Craigslist Furniture By Owner Dallas
Math Playground Protractor
Amanda Balionis makes announcement as Erica Stoll strides fairways with Rory McIlroy
Tani Ahrefs
Hartford Healthcare Employee Tools
Advance Auto Parts Near Me Open Now
Stronghold Slayer Cave
Ottumwa Evening Post Obits
German American Bank Owenton Ky
Craigslist Hunting Land For Lease In Ga
三上悠亜 Thank You For Everything Mikami Yua Special Photo Book
Wie funktioniert der Ochama Supermarkt? | Ladenbau.de Ratgeber
Meagan Flaherty Tells Kelli Off
How To Create A Top Uber Boss Killer In POE 3.25 League?
Arialectra Baby Alien
8662183887
Kcu Sdn
Used Golf Clubs On Craigslist
Dan And Riya Net Worth In 2022: How Does They Make Money?
Star News Mugshots
Craigslist Farm Garden Modesto
Skip The Games Albany
Strange World Showtimes Near Harkins Theatres Christown 14
Dr Bizzaro Bubble Tea Menu
2-bedroom house in Åkersberga
Pre-Order Apple Watch Series 10 – Best Prices in Dubai, UAE
Cvs On 30Th And Fowler
Ssndob Cm
Johnnie Robinson Auto Sales
Sona Systems Tcu
Bonbast قیمت ارز
Eliza Hay, MBA on LinkedIn: I’m happy to share that I’ve started a new position as Regional Director… | 36 comments
"Wordle" #1,176 answer, clues and hints for Saturday, September 7 game
Mcknet Workday
Eugenics Apush
Omaha World-Herald from Omaha, Nebraska
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 6064

Rating: 4 / 5 (41 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.