Tag Archives: Basic Programs

C Program to Count number of digits in number without using mod operator

C Program to Count the number of digits in number without using the mod operator.

In this tutorial, we will learn to create a C program that will Count the number of digits in number without using the mod operator in C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • String functions in c programming.

Count the number of digits in number without using the mod operator.

As we all know the c programming is a very powerful language. In the c programming language, we have many pre-defined functions for arithmetic operations. but today we will count the number of digits in a number without using the mod operator.

With the help of this program, we can count the number of digits in numbers without using the mod operator.

Algorithm:-

 

Program to count the number of digits in numbers without using the mod operator:-

 

Output:-

Including header files.

In the above program, we have first initialized the required variable.

  • ch[10] = it will hold the string value.
  • digits = it will hold the integer value for numbers.
  • no = it will hold the integer value of number input.

Taking input number from the user.

Converting that number to a string.

Storing the string size by using a predefined function.

Printing output of the program.

C Program to Add numbers without using arithmetic Operators

In this tutorial, we will learn to create a C program that add two number without using arithmetic operator(+)  using C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Operators
  • Loop statements
  • C while Loop.

Program to Add two numbers without using arithmetic Operators.

Easiest way for  adding two numbers without using arithmetic operator ‘+’ in C is with using increment(++) and decrement(–) operator .

In this program we will add two number using while loop.
Firstly we declare required header file and variable and also initiates required values in variable. Next we take value from user at run time and then after we will add these two values without using arithmetic operator.

Let see the code for our program.

Output

C Program to Add numbers without using arithmetic Operators

In the above program, we have first declared and initialized a set variables required in the program.

  • a = taking value from user in first variable.
  • b = taking value from user in second variable.

For adding 2 integer values without using arithmetic operators(+,-*,/), we can do this with using increment and decrement operator.

Take a look in below image.

The above condition tells you what is actually we have done  , we used increment and decrement  operator instead of ( + ) or ( ) to add two numbers

Firstly  we got two numbers from  user in a variable a and b . Then, we check  the condition until the second variable  b is not equal to  0. If it’s zero, we terminate the loop . If it’s a not zero number, then execute the following statement .

in which in each iteration the value of a increment to 1 and the value of b decrement to 1.The above statement will run until while (b!=0).

then in each iteration value of a is added and this process continues and we get final required output.

C Program to Add two numbers without using arithmetic Operators also possible Using logarithm and exponential functions  and either using pointers or using  bit-wise operators.

C Program to Demonstrate Printf inside Another Printf Statement

C Program to Demonstrate Printf inside Another Printf Statement

In this tutorial, we will learn to create a C program that will Demonstrate Printf inside Another Printf Statement using C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.

Demonstrate Printf inside Another Printf Statement :-

As we all know every programming language the program cannot be completed without taking input or returning output.  In C programming language printf() and scanf() functions are inbuilt library functions in  which are available in C library by default. These functions are declared and related macros are defined in “stdio.h” which is a header file in C language.

We only have to include the header file named as “stdio.h” as shown in below C program to make use of these printf() and scanf() library functions in C language.

Printf function is used to write the output or show message on the screen.

In nested printf Inner most printf() function writes the output of the program.

Second printf() function counts the output number’s digits and write them.

And third printf() function will write the number of digits returned by the second printf() function

 

Algorithm:-

Program:-

Demonstrate Printf inside Another Printf Statement.

 

 

Output:-

The above program we have to first initialize the required variable.

  • no = it will hold the integer value.

Taking Input from the user an integer value.

Printing using Printf inside Another Printf Statement.

 

C Program to Display Day of the month

In this tutorial, we will learn to create a C program that will accept year,month and date from user and display the day of month using C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Operators.
  • Basic input/output.
  • C loop statements
  • C switch case.
  • Function in C.

Example:

If you give value input in particular format  like year=2021, Month=5, and day is 12 then this  program will display the output as The Given  Date is : 12/05/2021 and Day of Week of the Date : Wednesday.

C Program to Display Day of the month.

In this program we  will print name of the Day of week and Date of calendar for given year, month and day.  Please  check out the program below  to  print, Name of Day of week and Date of calendar.

Output

C Program to Display Day of the month

In the above program, we have first declared and initialized a set variables required in the program.

This program will accept three values from user Year,Month and Date.Then we passes all three value to function called week_day it will accept all three values.With the help or week_day() function we calculate month day and week day.Within this function we gain called a function findmonth(). we find the month value and returns the value to week_day()  function where we will find the day of week and print our required output.

C program to shut down or turn off computer

C program to shut down or turn off computer

In this tutorial, we will learn to create a C program that will shut down or turn off the computer using C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Header Libraries and its usage.

Shut down or Turn off computer

In c programming it is possible shut down or turn off with the help of very small amount of code . The C language has many types of header libraries which has supported function in them with the help of these files the programming is easy.

Shutting Pc with the help of c program is so easy.

Algorithm:-

Program to shut down or turn off the computer :-

Program For Windows 7:-

Note to shutdown immediately use “C:\\WINDOWS\\System32\\shutdown -s -t 0”. To restart use “-r” instead of “-s”.

Here, “shutdown /s /t x”; where x is the number of seconds after which shutdown will occur.

Program For Windows xp:-

Note to shutdown immediately use “C:\\WINDOWS\\System32\\shutdown -s -t 0”. To restart use “-r” instead of “-s”.

Here, “shutdown /s /t x”; where x is the number of seconds after which shutdown will occur.

Program for Ubuntu:-

 

Output:-

The program we have to first include stdlib.h and then we call the system function.

C program to get IP address

C program to get IP address

In this tutorial, we will learn to create a C program that will get the IP address of computer using C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Operators in C Programming.
  • Basic Input and Output function in C Programming.
  • Basic C programming.
  • Array in C Programming.
  • Header Libraries and its usage.

Get IP address

In c programming, it is possible to take or fetch the ip address from the computer and convert it into words or can display in words with the help of a very small amount of code. The C language has many types of header libraries which has supported function in them with the help of these files the programming is easy.

Algorithm:-

Program:-

 

Output:-

The above program we have first initialize the required variable.

  • no = it will hold the integer value ip.
  • ifreq = it will hold the interface name in the ifreq structure.
  • ifr = it will hold the address type.
  • array[] = it will hold the integer value for numbers of ip address.

Including the header files required for program

Body of program retrieving the ip address

Main Program

 

 

C program to print date

In this tutorial, we will learn to find current system date day and time  using C programming language. There are many  different method to find system date and time in C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C library.
  • Operators.
  • in-built  functions.
  • Basic input/output.
  • Basic C programming.

Often we need to work on current date and time in our program.

C program to print date

Here we write a program, in which we want to  check current date and time to  given date and time to confirm the user age .Now we will see how to write a C program to print current date and time.

Output

Here how the above program works:

Here we have taken The time_t datatype it is a data type from the C ISO library  which is define to store system’s time value.

time() function will returns the system date and time.

With the help of  function ctime()  it will returns a string that shows the local time based on t (parameter) timer. This ctime() function is defined in time.h header file.we just need to include inbuilt header file in our program and function define within header file just need call them and that’s it our will call system day and all..

C program to print date

that will give the current day, month , date , time and year..

C program to check number is positive negative or zero

In this tutorial, we will learn to create a C program that will find weather a number is positive, negative or Zero  using C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Operators
  • If-else statement.
  • Nested else-if
  • Basic c programming.
  • Basic input/output.

C program to check whether a number is positive, negative or zero.

In this program we will find whether a number is positive, negative or zero series using if else statement. We would first declared and initialized the required variables. Next, we would prompt user to input any number. Later we will find that number is positive,negative or zero.

Output

Positive Value.

Negative Value

Zero Value.

In the above program, we have first declared and initialized a set variables required in the program.

  • number = it will hold the value of number.

In the next statement user will be prompt to enter any number terms which will be assigned to variable ‘number’.

For finding given integer is positive ,negative or zero we will use We will use nested if else statement

in above logic inside if we check  if(number > 0), number is greater than zero then number is positive.

if(number < 0) if number less than zero then number is negative. and if

if(number == 0), then number is zero.

Here the C program to find whether a number is positive, negative or zero.The above approach is easiest way find the problem. First we check condition for positive number, then for negative or and later we find for zero.

C Program to print all Happy Numbers till N

In this tutorial, we will learn to create a C program that will find Happy number  using C programming.

Prerequisites

Before starting with this program  we assume that you are best aware of the following C programming topics:

  • C Operators.
  • C loop statements.
  • C while and for Loop.
  • Conditional statement.

What Is Happy Number ?

Happy number is a number which finally ends at 1 when it is replaced by the sum of the square of its digits repeatedly.

Example

let’s take an example here 13 is a happy number

,   and      .

On the other hand,The unhappy number will result in a cycle of 4, 16, 37, ….

2 is not a happy number let’s have a look an example of this

and  this process continues to infinite cycle without ever reaching 1

C Program to print all Happy Numbers till N

In this program we will find that given number is happy number or not using for loop.
Firstly we declare required header file and variable and also initiates required values in variable. Next we take value from user at run time and then after we will find that the given value is happy number or not.

Output

C Program to print all Happy Numbers till N

happy number is defined by the following steps:

  • STEP 1: START.
  • STEP 2: SET tmp =0, sum =0.
  • STEP 3: REPEAT STEP 4 to 6 UNTIL (no>0)
  • STEP 4: tmp=no%10.
  • STEP 5: sum = sum + (tmp*tmp)
  • STEP 6: no= no/10.
  • STEP 7: RETURN sum.
  • STEP 8: END.
  • If the number given is greater than 0 (no>0 ),First we find remainder by dividing the number with 10  ( tmp=no %10 ).
  • Calculate square of tmp and add it to sum.
  • Then divide number by 10 ( no=no/10).
  • Repeat the steps from 4 to 6 until the sum of the square of all digits present in number has been calculated.
  • Finally, print  the sum.

So finally we get all the happy number from one to given number by user…..

C Program to print whether given Number is Happy or not

In this tutorial, we will learn to create a C program that will find the given number is a happy or not  using C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Operators.
  • Loop statements.
  • While and for Loop.
  • Conditional statement.

What Is Happy Number ?

Happy number is a number which finally ends at 1 when it is replaced by the sum of the square of its digits repeatedly.

Program to print whether given Number is Happy or not ?

in this program we are going to find that the given number is happy or not ?.As we explained happy number is a number which finally ends at 1 when it is replaced by the sum of the square of its digits repeatedly.In this program we will find that given number is happy number or not using for loop.
Firstly we declare required header file and variable and also initiates required values in variable. Next we take value from user at run time and then after we will find that the given value is happy number or not.

Output

C Program to print whether given Number is Happy or not

A number is said to be happy if it leads to 1 when we calculate the square of each digit present in number and them. if resulting sum is equal to 1 then the given number we can say it is a happy number.

example.

Let’s understand with an example:

In our example we have taken 13 to find that this number is happy or not,we  first break 13 to get the sum of squares of its digits which gives us another number (10), we interchange 13 by 10 to continue this process until we get result as 1.So 13 a happy number.As we discuss  above  happy number is a number that will give 1 at the end on continues after a sequence of steps wherein each step number is replaced by the sum of squares of its digit.
In our example we can see that 13 is a happy number.

C program to enter 5 subjects marks and calculate percentage.

In this tutorial, we will learn to create a C program that  will take five subjects marks and Calculate Percentage   using C programming.

Prerequisites

Before starting with this program  we assume that you are best aware of the following C programming topics:

  • C Operators.
  • Conditional statement.
  • Basic input/output.

Example.

Here we will create a program in which user enter the marks of five subjects and calculate percentage. And you will learn how to find percentage of the five subjects in C language.

C Program to Calculate entered 5 subjects marks and calculate percentage.

First of all we take five number from user and add them to find total  then divide the total by 5 to get our desired percentage .Here is a program for that have a look.

Output

C program to enter 5 subjects marks and calculate percentage.

In the above program, we have first declared and initialized a set variables required in the program.

  • m = holds value of math.
  • p = holds value of physics.
  • c = holds value of chemistry
  • h = holds value of Hindi.
  • e= holds value of English.
  • total = calculate sum of value.
  • per= it will hold percentage.

In this  C program to calculate Percentage of Five Subjects .

The first printf statement will ask the user to enter the 5 subjects number.

then we add all 5 given  value  in variable total ( total =m+p+c+h+e ).
Now, we calculated the percentage (per=total/5).

And we will get the required output.This program allows user to enter five values of five different  subjects. And then calculate the Total and Percentage of those Five Subjects.

C program to enter two angles of a triangle and find the third angle.

In this tutorial, we will learn to create a C program that will find side of a Triangle.If two side of a Triangle are given   using C programming.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • Arithmetic operators
  • Data types
  • Basic input/output.

Basic Properties of triangle

The sum of the interior angle on the same vertex is 180°

 

C Program to enter two angles of a triangle and find the third angle.

Here we take two side of a triangle and with the help of angle property we will find the required third side of a triangle ,Let’s take a look to program.

output

C program to enter two angles of a triangle and find the third angle.

the above program, we have first declared and initialized a set variables required in the program.

  • ang1 =it is for holding the angle value 1.
  • ang2 =it is for holding the angle value 2.
  • ang3 =it is for holding the angle value 3.

One of the common property of a triangles is that all three(3) interior angles (∠a + ∠b +∠c) of a triangle on adding will give to 180 degrees. This is an important theorem of geometry also known as Triangle Angle Sum Theorem(∠a + ∠b +∠c).According to this Theorem, the sum of the three interior angles in a triangle is always 180°.

So using this triangle  angle sum theorem we create a program in which we get two angle from user and by adding them subtract from 180 -(ang1+ang2) we will get our required third angle .

as we can see here by adding two given angle and subtract them from 180 we get the third required angle ..