C Programming Examples: Are you just starting out in C programming but feeling overwhelmed? Maybe you’ve been working through the basics of C programming for a while, but are now looking to take your skills to the next level. If so, we can definitely relate!
Here, we share C programs covering various topics in C Programming, including arrays, strings, series, area & volume of geometrical figures, mathematical calculations, sorting & searching algorithms, and many more. Our goal is to provide comprehensive solutions to all C programming questions you may encounter, whether in interviews or class assignments.
Each program in this article is accompanied by its working code and output. The programs are organized into categories, with related programs grouped together. It is recommended to grasp the fundamentals of the C language through our C tutorial before delving into these C programs. You can also download our C programming examples PDF to get instant access to C programming examples. So, whether you’re just setting out or have already made some progress in your learning adventure, this will be the perfect resource guide for honing your C coding skills!
If you find learning C language challenging, consider exploring our new C course from Physics Wallah. This interactive learning experience involves understanding a concept, completing small coding exercises, and progressing to the next lesson. Use “READER” coupon to get instant discounts on PW courses.
Also Read:
- Data Science Internship By Micron [With 40K Stipend]
- Product Development Lifecycle – A Simple 7-Stage Process
- AI And Data Science Jobs To Apply On October 3
- What Is Retail Analytics? The Ultimate Guide
- Survival Analysis – What It is, How It Works, Pros and Cons
C Programming Examples for Beginners
Below, we will share C programming examples for beginners. You can also download C language programs PDF.
1. C Hello World Program
#include <stdio.h>
int main() {
printf(“Hello, World!\n”);
return 0;
}
2. C Program to Print Your Own Name
#include <stdio.h>
int main() {
printf(“Your Name\n”);
return 0;
}
3. C Program to Print an Integer Entered By the User
#include <stdio.h>
int main() {
int num;
printf(“Enter an integer: “);
scanf(“%d”, &num);
printf(“You entered: %d\n”, num);
return 0;
}
4. C Program to Check Whether a Number is Prime or Not
#include <stdio.h>
int main() {
int num, i, flag = 0;
printf(“Enter a number: “);
scanf(“%d”, &num);
for (i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf(“%d is a prime number.\n”, num);
else
printf(“%d is not a prime number.\n”, num);
return 0;
}
Also read: Top 30 Most Asked Basic Programming Questions Asked During Interviews
C Programming Examples With Solutions
Here are some C programming examples with solutions and codes:
1. C Program to Multiply two Floating-Point Numbers
#include <stdio.h>
int main() {
float num1, num2, product;
printf(“Enter two floating-point numbers: “);
scanf(“%f %f”, &num1, &num2);
product = num1 * num2;
printf(“Product: %f\n”, product);
return 0;
}
2. C Program to Print the ASCII Value of a Character
#include <stdio.h>
int main() {
char ch;
printf(“Enter a character: “);
scanf(“%c”, &ch);
printf(“ASCII value of %c = %d\n”, ch, ch);
return 0;
}
3. C Program to Swap Two Numbers
#include <stdio.h>
int main() {
int num1, num2, temp;
printf(“Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
temp = num1;
num1 = num2;
num2 = temp;
printf(“After swapping: num1 = %d, num2 = %d\n”, num1, num2);
return 0;
}
4. C Program to Calculate Fahrenheit to Celsius
#include <stdio.h>
int main() {
float fahrenheit, celsius;
printf(“Enter temperature in Fahrenheit: “);
scanf(“%f”, &fahrenheit);
celsius = (fahrenheit – 32) * 5 / 9;
printf(“Temperature in Celsius: %f\n”, celsius);
return 0;
}
5. C Program to Find the Size of int, float, double, and char
#include <stdio.h>
int main() {
printf(“Size of int: %d bytes\n”, sizeof(int));
printf(“Size of float: %d bytes\n”, sizeof(float));
printf(“Size of double: %d bytes\n”, sizeof(double));
printf(“Size of char: %d byte\n”, sizeof(char));
return 0;
}
6. C Program to Print Prime Numbers From 1 to N
#include <stdio.h>
int main() {
int i, j, n;
printf(“Enter a number (N): “);
scanf(“%d”, &n);
printf(“Prime numbers between 1 and %d are: “, n);
for (i = 2; i <= n; ++i) {
int isPrime = 1;
for (j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime)
printf(“%d “, i);
}
return 0;
}
Also read: 10 Best Programming Languages for Game Development in 2024
C Programming Examples With Output
Learners can also download c programming examples with output PDF to start their C language journey. But practice is the key to learning C language properly. Here are some of the best C programming examples with output:
1. C Program to Check Whether a Number is Positive, Negative, or Zero
#include <stdio.h>
int main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num > 0)
printf(“Positive number\n”);
else if (num < 0)
printf(“Negative number\n”);
else
printf(“Zero\n”);
return 0;
}
Output:
Enter a number: 7
Positive number
2. C Program to Check Whether Number is Even or Odd
#include <stdio.h>
int main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num % 2 == 0)
printf(“Even number\n”);
else
printf(“Odd number\n”);
return 0;
}
Output:
Enter a number: 15
Odd number
3. C Program to Calculate Sum of Natural Numbers
#include <stdio.h>
int main() {
int n, sum = 0;
printf(“Enter a positive integer: “);
scanf(“%d”, &n);
for (int i = 1; i <= n; ++i) {
sum += i;
}
printf(“Sum of natural numbers from 1 to %d: %d\n”, n, sum);
return 0;
}
Output:
Enter a positive integer: 5
Sum of natural numbers from 1 to 5: 15
4. C Program to Print Alphabets From A to Z Using Loop
#include <stdio.h>
int main() {
char ch;
printf(“Alphabets from A to Z:\n”);
for (ch = ‘A’; ch <= ‘Z’; ++ch) {
printf(“%c “, ch);
}
return 0;
}
Output:
Alphabets from A to Z:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Recommended Technical Course
- Full Stack Development Course
- Generative AI Course
- DSA C++ Course
- Data Analytics Course
- Python DSA Course
- DSA Java Course
- Devops Course
- UI UX Course
- Digital Marketing Course
- Product Management Course
C Programs for Practice
Below table shows the C programming code that you can use for practice:
C Programs for Practice | ||
Example Number | Program Title | Description |
1 | Hello World Program | Basic program to print “Hello, World!” |
2 | Print Your Own Name | Program to print your own name |
3 | Add Two Numbers | Program to input and add two numbers |
4 | Check Positive, Negative, or Zero | Program to check whether a number is positive, negative, or zero |
5 | Even or Odd Number | Program to check whether a number is even or odd |
6 | Vowel or Consonant | Program to check whether a character is a vowel or consonant |
7 | Largest Number Among Three Numbers | Program to find the largest number among three numbers |
8 | Calculate Sum of Natural Numbers | Program to calculate the sum of natural numbers |
9 | Multiply Two Floating-Point Numbers | Program to multiply two floating-point numbers |
10 | Print ASCII Value of a Character | Program to print the ASCII value of a character |
11 | Swap Two Numbers | Program to swap two numbers |
12 | Calculate Fahrenheit to Celsius | Program to convert temperature from Fahrenheit to Celsius |
13 | Find Size of int, float, double, and char | Program to find the size of int, float, double, and char |
14 | Add Two Complex Numbers | Program to add two complex numbers |
15 | Print Prime Numbers From 1 to N | Program to print prime numbers from 1 to N |
16 | Find Simple Interest | Program to calculate simple interest |
17 | Find Compound Interest | Program to calculate compound interest |
18 | Area and Perimeter of Rectangle | Program to calculate the area and perimeter of a rectangle |
19 | Print Alphabets From A to Z Using Loop | Program to print alphabets from A to Z using a loop |
20 | Armstrong Number | Program to check whether a number is an Armstrong number |
Why Choose C Language for Learning?
C language is an excellent entry point into the programming world due to its simplicity and ease of learning. While certain concepts may pose challenges, overall, learners find C language accessible. It introduces fundamental programming concepts like data types, variables, functions, arrays, strings, conditional statements, loops, input/output, and data structures—all foundational principles applicable to various modern programming languages.
For newcomers, acquiring proficiency in C/C++ is crucial for success in college placement interviews, especially with service-based companies such as TCS, Accenture, IBM, etc., which actively seek C developers.
-
Highly Efficient:
C language is renowned for its efficiency and performance. Programs written in C execute swiftly and optimize system resources effectively.
-
System-level Programming:
Offering low-level control over a computer’s hardware and memory, C language is well-suited for system-level programming and the development of operating systems.
-
Portability:
C language is favored for its portability; its code can be compiled and executed on various platforms with minimal modifications. This makes C an ideal choice to run programs on diverse platforms.
-
Easy to Learn:
With a small learning curve, C language involves fewer keywords and concepts. Its syntax is easy to remember and apply. The C compiler provides descriptive errors, simplifying the debugging process and making it particularly beginner-friendly.
-
Versatility:
C language’s versatility allows for creating both small utility software and large-scale enterprise applications. Its broad applicability makes it a valuable language to master in programming.
Benefits of Practicing C Programming Examples
Practicing C Programming Examples offers several benefits for learners and aspiring programmers:
- Concept Reinforcement: By working on C programming examples, individuals reinforce fundamental programming concepts such as variables, loops, conditionals, and functions. This hands-on practice helps solidify theoretical knowledge.
- Preparation for Interviews: Many technical interviews, especially for entry-level programming positions, involve solving coding problems. Practicing C programming examples prepares individuals for such interviews, helping them perform well and showcase their coding proficiency.
- Application of Knowledge: C programming examples provide a practical platform for applying theoretical knowledge gained from textbooks or tutorials. This application-oriented learning approach ensures a deeper understanding of programming concepts.
- Syntax Mastery: Working on examples allows learners to master the syntax of the C programming language. Writing and debugging code regularly contributes to becoming fluent in C syntax, which is essential for effective programming.
- Understanding Algorithms and Logic: Examples often involve implementing algorithms and logical reasoning. Working through these examples improves algorithmic thinking and logic building, essential skills for writing efficient and optimized code.
- Skill Development: Solving a variety of programming problems enhances problem-solving skills. Each example presents a unique challenge, requiring learners to devise efficient solutions and improve their analytical and coding abilities.
- Builds Confidence: Successfully solving programming problems instills confidence in learners. As they tackle increasingly complex examples, individuals become more comfortable handling challenging coding tasks, boosting their overall confidence in programming.
- Diverse Problem-Solving: C programming examples cover a wide range of problems, including mathematical calculations, string manipulations, array operations, and more. This diversity exposes learners to various problem-solving approaches, enriching their programming toolkit.
- Portfolio Building: Individuals can use the solutions to these examples to build a portfolio showcasing their coding skills. A well-documented portfolio becomes a valuable asset when seeking internships, jobs, or contributing to open-source projects.
Also read: C++ For Kids- Learn Programming in The Fun Way
FAQs
Why is it essential to practice C programming examples?
Practicing C programming examples is crucial for several reasons. It helps reinforce theoretical knowledge, enhances problem-solving skills, and provides hands-on experience with the language. Through practice, programmers become familiar with common syntax, logic building, and debugging techniques, contributing to overall proficiency in C programming.
What is the purpose of the "Swap Two Numbers" program?
The "Swap Two Numbers" program is designed to interchange the values of two variables. It is a fundamental programming exercise that helps users understand the concept of swapping values using a temporary variable or without using one. Swapping is a common operation in programming, and mastering it is essential.
Where can I find more C programming examples for practice?
You can find more C programming examples for practice on online platforms, programming websites, and educational resources.
How can I use C programming examples for learning?
Start with simple examples like Hello World and gradually progress to more complex ones. Analyze the code, understand how each line works, and experiment with modifications. This hands-on approach is effective for learning programming.
Can I modify the examples to create my programs?
Absolutely! Modifying existing examples is an excellent way to practice. Experiment with changing variables, adding features, or solving similar problems. This process fosters creativity and a deeper understanding of coding.