Basic C Programs

Basic C Programs

Basic C Programs

Basic C Programs

Basic C Programs

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe

Basic C Programs

Basic structure of c program

Dennis Ritchie developed the procedural, general-purpose computer language C at Bell Labs in the early 1970s. C offers low-level access to hardware and system memory. Because of its straightforward syntax, condensed set of keywords, and compact standard library, it is quick and effective for system-level programming.

As we all know people have liked the programming language C for more than 40 years, and many people still use it today. 

Statistics from Statista say that 7 million people will have used the C computer language in 2023. Some basic C programs syntax and features have been added to many modern computer languages. 

For example, blocks of code are enclosed in curly braces, and variables and functions are used.

Basic structure of c programs
Basic structure of c programs
  • Documentation: The documentation tells you what the tool is for and how to use it.
  • Link: A link in C programming is a way to refer to or connect different parts of a code. Pointers, functions, and other programming structures can be used to make links.
  • Definition: A definition is a sentence that tells a variable, function, or other piece of code what its value, type, or additional attribute is.
  • Global declaration: Declares variables and functions that can be accessed from anywhere in the programme.
  • Main function: Serves as the entry point of the programme and controls the flow of the programme.
  • Subprograms: Code blocks that work on their own and can be called from other parts of the programme to do specific jobs.

Hello World Basic C Programs

The C Hello World program is a basic C program usually used as an introduction to programming in a new language. To write a ‘Hello, World’ program as the first program in any new language. It’s a tradition in the programming community.

 

Hello world, basic programs of c language

 

Explanation:

 

  • The #Include<stdio.h> line shows our program’s input/output library. 
  • A main() function is the program’s entry point where the execution begins. 
  • Printf() function prints the c code Hello World string.
  • The return 0 statement indicates the program has been executed successfully. 

Palindrome in C programming

A palindrome in basic C programs is a sequence of characters that reads the same backward as forward. In other words, a palindrome in C is a Number spelled the same way from left to right and right to left.

In simple terms, it’s an English word that means the straight and backward forms of any word or number are the same.

For Example: 

  1. HANNAH whose reverse is also HANNAH, so it’s a palindrome
  2. KAJAL whose reverse is LAJAK, it’s not a palindrome.

Program for palindrome in C

Explanation:

  • Read an integer n from the user using scanf().
  • Initialize two variables reversedN and originalN.
  • Store the original value of n in originalN.
  • Reverse the number using a while loop:
    • The modulus operator% can be used to get the number’s last digit.
    • Add the last digit to the reversed number reversedN multiplied by 10 (which shifts the digits to the left by one place).
    • Divide the original number n by 10 to remove the last digit.
    • Repeat the above steps until n becomes 0.
  • Check if the reversed number reversedN is equal to the original number originalN.
  • If they are equal, print a message indicating that the number is a palindrome.
  • If the number is not a palindrome, print the message.

Basic C programs to Swap two numbers

In the basic computer language C, switching two numbers means swapping the values of two variables. 

For example, if you have two variables, 7 &  4, with values num1 and num2, respectively. Swapping the values of 7 & 4 converts into 7 having the value of num2 (4) and 4 having the value of num1 (7).

Swapping is a common operation in programming and can be useful in various situations.

E.g., If you require to sort a list of numbers & you may need to swap their positions to get them in the correct order.

Swapping of two numbers in C

 

Explanation:

  • The first thing that the basic C program does is declare three number variables: temp, num1, and num2.
  • It uses the “printf()” function to ask the user for two numbers, and the “scanf()” function reads those numbers into “num1” and “num2”.
  • So basically the printf() function is used to show what num1 and num2 were originally set to.
  • The program stores the value of num1 in a temporary variable called “temp” so that the values of “num1” and “num2” can be switched.
  • It changes the value of num2 to num1, which is the same as the value of num1 before.
  • Finally, it gives num2 the value of temp, which was num1 original value. 
  • Printf() is then used to show the new numbers num1 and num2.
  • By returning 0, it shows that the program ran properly.

Basic C programs to Find ASCII value of a character 

American Standard Code for Information Interchange is how ASCII is officially referred to. ASCII value in values refers to the numerical representation of characters in the ASCII character set. 

Each character in the ASCII character set is allocated a unique integer value between 0 and 127. For example, the character ‘A’ has an ASCII value of 65. The character ‘a’  ASCII value is  97. The ASCII value for the character ‘#’ is 35.

 

ASCII value C program

Approach 1: Here a user enters any characters in a basic C program that prints the ASCII value of a character. 

 

Explanation:

  • We declare a variable C of type char to store the user input.
  • We prompt the user to enter a character using printf().
  • We use scanf() to read the input character into the variable c.
  • We print the ASCII value of the entered character using printf() and the %d format specifier.
  • The %d format specifier is used to print the integer value of the entered character, which is its ASCII value.
  • The program then returns 0 to indicate successful completion.

 

Basic C program to print all ASCII value of Alphabets 

Approach 2: The two loops in this ASCII value c programme go through all of the uppercase and lowercase letters in the English language. 

Explanation:

  • We declare a “variable c” of type char to store the current character value in the loops.
  • We are using two for loops to iterate over all uppercase & lowercase letters in English alphabets.
  • In the first loop, we start at the character ‘A’ and increment c by one in each iteration until we reach ‘Z.’ 
  • This loop prints the uppercase letters and their ASCII values using printf().
  • In the second loop, we start at the character ‘a’ and increment c by one in each iteration until we reach ‘z.’ 
  • This loop prints the lowercase letters and their ASCII values using printf().
  • We use the %c format specifier to print the character value of c. The %d format specifier prints its integer value, ASCII value.
  • Finally, the program returns 0 to indicate successful completion.

Even odd program in C

An even or odd program in C is a program that takes an integer as input & determines whether the integer is even or odd. An even integer is divisible by 2 without a remainder. An odd integer is not divided by 2. 

 

Odd or Even Basic C Programs

 

Explanation:

This C programme asks the user to enter a number that is stored in the variable num. Numbers are checked to see if they are divisible by 2 using the % function. 

It will print “num is even” if num is an even number. It’ll say “num is odd” on the screen. 

Odd or even is one of basic C programmes that tells you if a number is even or odd.


Leap year program in C

A leap year basic c program is a year that has 366 days instead of the usual 365 days. This extra day is added to February, which has 29 days instead of 28 days.

Here some rules for checking is leap year or not:

  • If a year is divisible by 4, it is a leap year.
  • The year is not a leap year if it can be divided by 100.
  • If the year is divisible by 400, it is a leap year.

 

Leap year in C

 

Explanation:

  1. The program starts by declaring an integer variable year.
  2. It prompts users to enter a year using the printf() and scanf() functions.
  3. Using the above mentioned rules, it checks whether the entered year is a leap year.
  4. If the year is a leap year, it displays the message “Year is a leap year” using the printf() function.
  5. If the year is not a leap year, it displays the message “year is not a leap year” using the printf() function.
  6. Finally, the program returns 0, indicating successful execution.

Note that the % operator in C returns the remainder after division. Thus, year % 4 gives the remainder when the year is divided by 4. Similarly, year % 100 gives the remainder when the year is divided by 100.


Reverse a number C program

Reversing a number in the C program means changing the order of its digits. The last digit becomes the first. The second becomes the next-to-last digit, etc. Until the first digit becomes the last, for example, reversing the number 1234 would give 4321.

Reverse a number in C

 

Explanation:

  1. We first include the standard input/output header file <stdio.h>.
  2. We declare three variables: “num”, which is the number entered by the user; “rev”, which is the reversed number; and “remainder”, which stores the remainder when the number is divided by 10.
  3. We prompt the user to enter an integer using “printf()” and read the input using “scanf()”.
  4. We use a while loop to iterate until the num becomes zero. 
  5. In each iteration, we calculate the remainder of num when divided by 10 using the modulo operator % and add it to the rev variable. 
  6. We then multiply rev by 10 and add the remainder to reverse the order of the digits.
  7. Finally, we divide the num by 10 to discard the last digit.
  8. We print the reversed number using printf().
  9. We return 0 from the main() function to indicate successful program execution.

Prime number program in C

A Basic C program on a prime number is a positive integer greater than one that has no positive integer divisors other than one and itself. In different words, a prime number is a number that is only divisible by one and itself.

Basic C program to print prime numbers from 1 to n

 

Let’s go through the program step by step:

 The program’s code initially includes the <stdio.h> header file’s standard input and output routines for C.

  • In the main() function, we declare three variables: 
  1. i) num to store the input number
  2. ii) ‘i’ for loop counter

iii) flag to keep track of whether the number is prime or not.

  • With the help of the printf() and scanf() functions, we ask the user to enter a positive number and then acknowledge their response.
  • We determine whether the input number is either 0 or 1. If it is, we print a message saying that the number is not prime.
  • If the input number is greater than 1, we loop through the numbers from 2 to num/2 using a for loop.
  • Checking if num is divisible by “i” inside the loop. If it is, we set the flag variable to 1 and break out of the loop.
  • Check the value of the flag variable after the loop. If it is still 0, we print a message saying that the number is prime. If it is 1, we print a message saying that the number is not prime.
  • Return 0 to indicate successful completion of the program.

 

Note:  That we only need to check the numbers up to num/2 since any number greater than num/2 cannot be a factor of num. This optimization helps reduce the number of iterations required by the loop.


Generate Multiplication table in C

The multiplication table shows what happens when you multiply a number by any whole number from 1 to 10. 

 

Explanation:

  1. Program includes the <stdio.h> header file, which contains the printf and scanf() functions.
  2. The main function is defined, which is the program’s starting point.
  3. Two integer variables, num and i, are declared.
  4. The user is asked for a number using the printf function.
  5. The scanf() function reads the number entered by the user and stores it in the num variable.
  6. The digits 1 through 10 are iterated through using a for loop.
  7. Within the for loop, the printf function is used to display the multiplication table of the num variable.
  8. The program ends with the return statement.

LCM program in C

LCM stands for “Least Common Multiple,” which is the smallest positive integer that can be divided by both numbers. 

LCM of two numbers in C

 

Explanation:

  1. First you have to declare three integer variables, num1, num2, and max, to store the two input numbers and the maximum of the two.
  2. Use the printf() and scanf() functions to get the two input numbers from the user.
  3. Use the ternary operator to find the maximum of the two numbers and store it in the max variable.
  4. Start an infinite loop using the while (1) statement.
  5. In the loop, use an if statement to check if the max variable is divisible by num1 & num2.
  6. If the max variable is divisible by num1 and num2, print the LCM using the printf() function and exit the loop using the break statement.
  7. If the max variable is not divisible by num1 and num2, increment the max variable using the max++ statement.
  8.  “Return 0” to indicate the successful execution of the program after the loop is exited.

Conclusion:

I hope you get a better understanding of basic C programs like even or odd, LCM, palindrome, etc. These programs are important building blocks for learning C programming and developing problem-solving skills.

 If you want to become proficient in C programming or any other programming language, Technogeeks Software Training Institute can help. 

They offer comprehensive training programs in various programming languages and technologies, including C programming. 

Our experienced trainers are professionals in reputed software industries and can help you master the fundamentals of C. 

 Technogeeks Software Training Institute can be the right choice if you’re looking to upskill in C programming or any other technology. 

They offer other programming language courses like data science, cloud computing, and more. 

Their practical training approach through real-time projects, case studies, and assignments can help you gain industry exposure and stand out in the job market.

 

Aniket

Aniket

Leave a Reply

Your email address will not be published. Required fields are marked *

Blogs You May Like

Get in touch to claim Best Available Discounts.

If You Are Looking for Job Assistance Please Fill Up the Form.

× How can I help you?