How to Execute a C Program in Linux
You can write, compile, and run your C programs directly from the Linux terminal. Follow these steps:
-
Open a text editor (e.g.,
gedit
) and create a file:gedit test1.c
now it opens a text area where you can type your code. type the code here.
observe the small * in the file name (its on the top left)
that star says the file is not savednow you have to save the code.
press ctrl+son saving, the star goes off, it meas the file is saved
now you have to close this text editor. to do that
press ctrl+q to quit
-
Compile the program using the GCC compiler:
cc test1.c
-
Run the compiled output:
./a.out
Example:
gedit test1.c
cc test1.c
./a.out
⚠️ Common Mistakes
-
no space between
test1 .c
, usetest1.c
instead. -
Forgetting the
./
beforea.out
when running the program. -
Saving the file with the wrong extension (e.g.,
test1.txt
instead oftest1.c
). -
Using
cc test1
without.c
— the compiler won't find the file.
Keyboard Shortcuts
Try using the keyboard as much as possible to improve speed and efficiency.
Use the left side of the keyboard with your left hand and the right side with your right hand for better flow.
-
Select all text or code
Ctrl + A
-
Copy selected text
Ctrl + C
-
Paste copied text
Ctrl + V
-
Save file in editor
Ctrl + S
-
Quit/Exit editor
Ctrl + Q
-
Shift key usage:
Hold
Shift
to type uppercase letters or access the top symbols on keys.
⚠️ Common Mistakes
- Forgetting to hold Shift while typing uppercase letters or symbols.
- Using Caps Lock instead of Shift — may lead to unexpected ALL CAPS text.
-
Closing the editor without saving changes (
Ctrl + S
is your friend!).
C Program to Print "Hello World"
To print the “Hello World”, we can use the printf function from the stdio.h library that prints the given string on the screen.
// Header file for input output functions
#include <stdio.h>
// Main function: entry point for execution
int main() {
// Writing print statement to print hello world
printf("Hello World");
return 0;
}
Hello World
Explanation:
- #include <stdio.h> - This line includes the standard input-output library in the program.
- main() - The main function where the execution of the program begins.
-
printf(“Hello, World!\n”); - This function call prints “Hello, World!” followed by a new line. - return 0; -This statement indicates that the program ended successfully.
C Program To Print Your Own Name
Printing your own name means displaying your name on the computer screen. In this article, we will learn how to print your own name using a C program.
Examples
-
Input: name = "Pruthviraj"
Output: Pruthviraj -
Input: name = "Guddu"
Output: Guddu
Print Your Own Name Using printf()
The simplest way to print something is to use the printf() function. You can provide your name in the form of string to printf() and it will print it on the output screen.
Program
#include <stdio.h>
int main() {
printf("Pruthviraj Guddu");
return 0;
}
Pruthviraj Guddu
Other Different Ways to Print Your Own Name in C
- Take Your Name as Input and Then Print It
- Using
puts()
- Using
fputs()
- Using
fprintf()
- Using
write()
System Call
Program using scanf()
#include <stdio.h>
int main() {
char name[100];
printf("Enter Your Name: ");
scanf("%s", name);
printf("Your Name: %s \n", name);
return 0;
}
Enter Your Name: Pruthviraj
Your Name: Pruthviraj
Program using puts()
#include <stdio.h>
int main() {
puts("Guddu");
return 0;
}
Guddu
Print an Integer Value in C
Printing an Integer in C is one of the most basic tasks. Input and output of the elements are essential for taking input from the user and then giving the output to the user. Here we are going to take an integer as input from the user with the help of the scanf() function and print that integer with the help of the printf() function in C language.
1. Printing Integer Values in C
Store the integer value in the variable of type int
and
print this value using the printf() method.
Syntax:
printf("%d", variableOfIntType);
Program
// C Program to Print Integer value
#include <stdio.h>
// Driver code
int main() {
// Declaring integer
int x = 5;
// Printing values
printf("Printing Integer value %d", x);
return 0;
}
Printing Integer value 5
2. Reading Integer Values in C
The user enters an integer value when asked. This value is taken from the user with the help of the scanf() method.
Syntax:
scanf("%d", &variableOfIntType);
Program
#include <stdio.h>
int main() {
int num;
printf("Enter the integer: ");
scanf("%d", &num);
printf("Entered integer is: %d", num);
return 0;
}
Enter the integer: 10
Entered integer is: 10
C Program to Add Two Integers
Given two integers, the task is to add these integer numbers and return their sum.
Examples
-
Input: a = 5, b = 3
Output: 8
Explanation: The sum of 5 and 3 is 8. -
Input: a = -2, b = 7
Output: 5
Explanation: The sum of -2 and 7 is 5.
In C, we have multiple methods to add two numbers, such as using the addition operator (+), the increment operator (++ ), or bitwise operators.
1. Adding Two Numbers using + Operator
Adding two numbers is a simple task in C language that can be accomplished using the '+' operator that takes two operands and returns their sum as the result.
Program
#include <stdio.h>
int main() {
int a, b, sum = 0;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum: %d", sum);
return 0;
}
Enter two integers: 5 3
Sum: 8
Complexity
- Time Complexity: O(1)
- Auxiliary Space: O(1)
Explanation
In the above program, the user is first asked to enter two numbers.
The input is taken using the scanf() function and
stored in the variables a
and b
. Then, the
variables a
and b
are added using the
arithmetic addition operator +
, and the result is stored
in the variable sum
.
C Program to Check Voting Eligibility
This program takes the user's age as input and checks if they are eligible to vote. If the entered age is 18 or above, it displays a message saying the person can vote.
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age > = 18) {
printf("You can vote.\n");
}
return 0;
}
Enter your age: 20
You can vote.
Explanation:
- The program reads an integer input (age) from the user.
- It then checks if the age is greater than or equal to 18.
- If true, the program prints You can vote.
- Otherwise, nothing is printed and the program ends.
⚠️ Common Mistakes
-
Forgetting to use &age in
scanf("%d", &age);
will cause errors. -
Missing braces
{ }
after if when writing multiple statements.
C Program to Check if a Number is Even or Odd
This program reads a number from the user and checks whether it is even or odd using the modulo (%) operator.
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
return 0;
}
Enter a number: 7
The number is odd.
Explanation:
- The program takes an integer input from the user.
- The condition number % 2 == 0 checks if the number is divisible by 2.
- If true, the program prints The number is even.
- Otherwise, it prints The number is odd.
⚠️ Common Mistakes
-
Forgetting &number in
scanf("%d", &number);
will not store the input. -
Writing
if (number % 2 = 0)
instead of==
— single=
is assignment, not comparison.
C Program to Find the Largest of Two Numbers
This program takes two numbers as input from the user and compares them to determine which one is larger.
#include <stdio.h>
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
if (num1 > num2) {
printf("%d is the largest.\n", num1);
} else if (num2 > num1) {
printf("%d is the largest.\n", num2);
} else {
printf("Both numbers are equal.\n");
}
return 0;
}
Enter two numbers: 12 25
25 is the largest.
Explanation:
-
The program takes two integer inputs from the user using
scanf()
. -
If
num1 > num2
, it prints thatnum1
is larger. -
If
num2 > num1
, it prints thatnum2
is larger. - If neither condition is true, both numbers are equal.
⚠️ Common Mistakes
-
Forgetting &num1 and
&num2 in
scanf("%d %d", &num1, &num2);
. -
Using
=
instead of==
when checking equality. - Not handling the case when both numbers are equal.
Different Ways to Find the Largest of Three Numbers Using If Conditions
These examples show various ways to find the largest among three numbers using if-based logic in C.
Method 1: Using Nested if
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c)
printf("%d is the largest.\n",
a);
else
printf("%d is the largest.\n",
c);
} else {
if (b > c)
printf("%d is the largest.\n",
b);
else
printf("%d is the largest.\n",
c);
}
return 0;
}
Method 2: Using if...else if...else
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > = b && a > = c)
printf("%d is the largest.\n", a);
else if (b > = a && b > = c)
printf("%d is the largest.\n", b);
else
printf("%d is the largest.\n", c);
return 0;
}
Method 3: Using Simple if Conditions
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
printf("%d is the largest.\n", a);
if (b > a && b > c)
printf("%d is the largest.\n", b);
if (c > a && c > b)
printf("%d is the largest.\n", c);
if (a == b && b == c)
printf("All numbers are equal.\n");
return 0;
}
🔍 Tips & Notes
- Always validate input when using scanf.
-
Be cautious of using only
if
withoutelse
— multiple blocks may execute. - Handle equal values when necessary.
C Program to Classify Score using if-else
This program takes a score as input and classifies it into distinction, first class, second class, pass class, or fail using if-else statements.
#include <stdio.h>
int main() {
int score;
printf("Enter your score (0-100): ");
scanf("%d", &score);
if (score > = 90) {
printf("Distinction\n");
} else if (score > = 80) {
printf("First Class\n");
} else if (score > = 70) {
printf("Second Class\n");
} else if (score > = 50) {
printf("Pass Class\n");
} else {
printf("Fail\n");
}
return 0;
}
Enter your score (0-100): 85
First Class
Explanation:
- User inputs a score using
scanf()
. -
if-else
conditions check ranges and print the corresponding class. - Final
else
handles the "fail" case.
C Program to Classify Score using switch
This program uses switch
to classify a score. Since
switch
doesn't support ranges directly, we divide the
score by 10 and use the result.
#include <stdio.h>
int main() {
int score;
printf("Enter your score (0-100): ");
scanf("%d", &score);
switch (score / 10) {
case 10:
case 9:
printf("Distinction\n");
break;
case 8:
printf("First Class\n");
break;
case 7:
printf("Second Class\n");
break;
case 5:
case 6:
printf("Pass Class\n");
break;
default:
if (score < 0 || score >
100) {
printf("Invalid
Score\n");
} else {
printf("Fail\n");
}
}
return 0;
}
Enter your score (0-100): 73
Second Class
Explanation:
- Score is divided by 10 to get a category (e.g., 85 becomes 8).
switch
checks the case values to classify.-
Special handling for out-of-range scores in the
default
case.
C Program to Print Weekday Name using switch
This program takes a number (1 to 7) and uses switch
to
print the corresponding weekday.
#include <stdio.h>
int main() {
int day;
printf("Enter day number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day
number\n");
}
return 0;
}
Enter day number (1-7): 3
Wednesday
Explanation:
- User enters a number from 1 to 7.
-
The
switch
matches the number to a case and prints the corresponding weekday. -
If the number is out of range, the
default
case prints an error message.
C Program to Print Squares of First 10 Numbers (for loop)
This program uses a for
loop to print the squares of
numbers from 1 to 10.
#include <stdio.h>
int main() {
int i;
for (i = 1; i < = 10; i++) {
printf("%d^2 = %d\n", i, i * i);
}
return 0;
}
Output:
1^2 = 1
2^2 = 4
3^2 = 9
...
10^2 = 100
C Program to Display Even Numbers up to 20 (for loop)
This program prints all even numbers between 1 and 20 using a
for
loop.
#include <stdio.h>
int main() {
printf("Even numbers from 1 to 20:\n");
for (int i = 2; i < = 20; i += 2) {
printf("%d ", i);
}
return 0;
}
Output: 2 4 6 8 10 12 14 16 18 20
C Program to Check if a Number is Prime (for loop)
This program checks whether a given number is prime using a
for
loop.
#include <stdio.h>
int main() {
int n, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 2; i < = n / 2; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime && n > 1)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
return 0;
}
Output:
Enter a number: 20
20 is not a prime number.
C Program to Find Sum of Digits (while loop)
This program finds the sum of digits of a given number using a
while
loop.
#include <stdio.h>
int main() {
int n, sum = 0, digit;
printf("Enter a number: ");
scanf("%d", &n);
while (n ! = 0) { /*no spaces*/
digit = n % 10;
sum + = digit; /*no spaces*/
n / = 10; /*no spaces*/
}
printf("Sum of digits = %d\n", sum);
return 0;
}
Enter a number: 1234
Sum of digits = 10
C Program to Find Sum of First N Numbers (while loop)
This program calculates the sum of the first N natural numbers using a
while
loop.
#include <stdio.h>
int main() {
int n, sum = 0, i = 1;
printf("Enter a number: ");
scanf("%d", &n);
while (i < = n) {
sum += i;
i++;
}
printf("Sum = %d\n", sum);
return 0;
}
Enter a number: 5
Sum = 15
C Program to Reverse a Number (while loop)
This program reverses a given number using a while
loop.
#include <stdio.h>
int main() {
int num, rev = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
while (num ! = 0) {
digit = num % 10;
rev = rev * 10 + digit;
num / = 10;
}
printf("Reversed number = %d\n", rev);
return 0;
}
Enter a number: 1234
Reversed number = 4321
C Program to Check Palindrome Number (while loop)
This program checks if a number is a palindrome using a
while
loop.
this is follows the same logis as above to reverse the number, additionally we will compare the original number with reversed number
#include <stdio.h>
int main() {
int num, temp, rev = 0, digit;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while (temp ! = 0) {
digit = temp % 10;
rev = rev * 10 + digit;
temp / = 10;
}
if (rev == num)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
}
Enter a number: 121
Palindrome
C Program to Check if a String is Palindrome (while loop, flag, start/end)
This program checks if a string is a palindrome using a
while
loop, a flag
, and
start
/end
variables.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int start = 0, end, flag = 1;
printf("Enter a string: ");
scanf("%s", str);
end = strlen(str) - 1;
while (start < end) {
if (str[start] ! = str[end]) {
flag = 0;
break;
}
start++;
end--;
}
if (flag = = 1)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
}
Enter a string: radar
Palindrome
C Program to Check if a String is Palindrome (while loop)
This program checks if a string is a palindrome using a
while
loop.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int start = 0, end, isPalindrome = 1;
printf("Enter a string: ");
scanf("%s", str);
end = strlen(str) - 1;
while (start < end) {
if (str[start] ! = str[end]) {
isPalindrome = 0;
break;
}
start++;
end--;
}
if (isPalindrome)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
return 0;
}
Enter a string: gadag
Palindrome
Enter a string: madam
Palindrome
Enter a string: apple
Not Palindrome
C Program to Find Factorial of a Number (while loop)
This program calculates the factorial of a number using a
while
loop.
#include <stdio.h>
int main() {
int n, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
int i = 1;
while (i < = n) {
fact *= i;
i++;
}
printf("Factorial = %d\n", fact);
return 0;
}
Output: Enter a number: 5
Factorial = 120
Output: Enter a number: 8
Factorial = 40320
C Program to Count Digits in a Number (while loop)
This program counts the total number of digits in a given number using
a
while
loop.
#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n ! = 0) {
n / = 10;
count++;
}
printf("Number of digits = %d\n", count);
return 0;
}
Output: Enter a number: 2142
Number of digits = 4
Output: Enter a number: 32345
Number of digits = 5
C Program to Print Numbers from 1 to N (do-while loop)
This program prints numbers from 1 to N using a
do-while
loop.
#include <stdio.h>
int main() {
int n, i = 1;
printf("Enter the value of N: ");
scanf("%d", &n);
do {
printf("%d ", i);
i++;
} while (i < = n);
return 0;
}
Output:
Enter the value of N: 5
1 2 3 4 5
C Program to Display Menu using do-while loop
This program demonstrates a simple menu-driven system using a
do-while
loop.
#include <stdio.h>
int main() {
int choice;
do {
printf("\n1. Add\n2. Subtract\n3.
Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Addition
selected\n");
break;
case 2:
printf("Subtraction
selected\n");
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid
choice!\n");
}
} while (choice ! = 3);
return 0;
}
Output:
1. Add
2. Subtract
3. Exit
Enter choice: 1
Addition selected
1. Add
2. Subtract
3. Exit
Enter choice: 2
Subtraction selected
1. Add
2. Subtract
3. Exit
Enter choice:
C Program to Find Sum of Positive Numbers (do-while loop)
This program adds positive numbers entered by the user until a negative number is entered.
#include <stdio.h>
int main() {
int num, sum = 0;
do {
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
sum += num;
} while (num > = 0);
printf("Sum = %d\n", sum);
return 0;
}
Output:
Enter a number: 20
Enter a number: 30
Enter a number: 0
Enter a number: -1
Sum = 50
Output:
Enter a number: 33
Enter a number: 5
Enter a number: 11
Enter a number: 25
Enter a number: 35
Enter a number: -1
Sum = 109
C Program to Reverse a Number (do-while loop)
This program reverses a given number using a
do-while
loop.
#include <stdio.h>
int main() {
int n, rev = 0, digit;
printf("Enter a number: ");
scanf("%d", &n);
do {
digit = n % 10;
rev = rev * 10 + digit;
n / = 10;
} while (n ! = 0);
printf("Reversed Number = %d\n", rev);
return 0;
}
Output:
Enter a number: 245316
Reversed Number = 613542
Output:
Enter a number: 391245
Reversed Number = 542193
C Program - Simple Calculator (do-while loop)
A menu-driven calculator using a
do-while
loop to perform basic arithmetic operations.
#include <stdio.h>
int main() {
int choice;
float a, b, result;
do {
printf("\n1. Add\n2. Subtract\n3.
Multiply\n4. Divide\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
if (choice >= 1 && choice <=
4) {
printf("Enter two numbers:
");
scanf("%f %f", &a,
&b);
}
switch (choice) {
case 1: result = a + b;
break;
case 2: result = a - b;
break;
case 3: result = a * b;
break;
case 4:
if (b != 0)
result =
a / b;
else {
printf("Cannot
divide by zero!\n");
continue;
}
break;
case 5: printf("Exiting...\n");
break;
default: printf("Invalid
choice!\n"); continue;
}
if (choice > = 1 && choice <=
4)
printf("Result = %.2f\n",
result);
} while (choice ! = 5);
return 0;
}
Output:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 1
Enter two numbers: 5 3
Result = 8.00
...
Output:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 1
Enter two numbers: 15 20
Result = 35.00
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: 3
Enter two numbers: 4 2
Result = 8.00
1. Add
2. Subtract
3. Multiply
4. Divide
5. Exit
Enter your choice: