Tag Archives: C Program

C Program to Calculate Sum using only One Variable

As we also talked before that the less numbers of variables in the program means less memory used by the program and in return the program also run faster. So it;s always best to reduce the number of variables used in the program, as to increase the execution time of the program.

In this post we are going to make the program to calculate sum of two integers using only a single variable. We will achieve this by making a function.

Let’s have ah Look The Program ►► Continue reading C Program to Calculate Sum using only One Variable

Advertisements

C Program to Sort an Array Using Bubble Sort

Sorting is a procedure in which the given elements are arranged in ascending or descending order. For example if the elements of an array are –

5, 11, 15, 8, 7, 54, 63, 44

After sorting these elements in ascending order, the elements would be –

5, 7, 8, 11, 15, 44, 54, 63

What is Bubble Sort is :

Bubble sort proceeds by scanning the list and exchanging the adjacent elements if they are out of order with respect to each other. It compares each element with its adjacent element and swaps them if they are not in order i.e. arr[j] will be compared with arr[j+1] and if arr[j]>arr[j+1] then they will be swapped.

Let’s See the Program for Bubble Sort ►► Continue reading C Program to Sort an Array Using Bubble Sort

C Program to Find Whether the Number is Even or Odd

We all know, the number which can divide from 2 completely, means if the reminder is zero, then the number is even and which cannot are odd numbers.

The same we will do in our program to know whether the number, which is input through the keyboard, is even or odd.

Remember the modulus operator (%), this operator returns the reminder, when dividing any number. So we will condition the if statement, if the reminder is zero after diving the number by 2, then the number is even otherwise odd.

Let’s have ah Look The Program ►► Continue reading C Program to Find Whether the Number is Even or Odd

C Program to Use Printf(); Function Without The Semicolon

We all know, every statement in C is terminated by semicolon (;). The same apply to printf(); function.

Suppose we are going to print Lightning Code on the screen than statement for it will be printf(“Lightning Code”); but what if we want to print it without the semicolon after printf. Think about it, how can you do it, I know, you know how can but you did’t notice it.

Let me give you a hint, when we use if(); statement to condition something, we never use the semicolon inside the parenthesis. Now I am sure you make it out.

Let’s Have Ah Look at Program ►► Continue reading C Program to Use Printf(); Function Without The Semicolon

C Program to Find Factorial of a Number

Hello Readers 🙂 Hope you are getting valuable posts. If you have any query or want to know about any program, Let Me Know, I’ll love to help you out. And Keep Learning.

Now I am going to write a program to find the factorial of a number. Factorial of a number is the multiplication of all numbers lower than it and greater than zero as factorial of 5 will be 5*4*3*2*1=120.

Let’s Make The Program ►► Continue reading C Program to Find Factorial of a Number

C Program to Print Out Fibonacci Series Using Recursion

As Ih told you about Fibonacci Series in my previous post. Here I am writing the program to print Fibonacci Series Using Recursion. Recursion is a technique in which function call itself. A program that calls itself is called recursive program.

Look at the Program  ►► Continue reading C Program to Print Out Fibonacci Series Using Recursion

C Program to Print Out Fibonacci Series upto N Terms

Really for coding one also must learn the Mathematics. There are many programs out there which are totally based on mathematics questions. And I hate maths 😦 Love to coding only 😉

Well Let Face It. Lets make a program to print out Fibonacci series up to N terms. A series of numbers in which each sequent number is sum of its two previous numbers is known as Fibonacci series and each numbers are called Fibonacci numbers.. For example we have to print up to 5 then the series will be 0,1,1,2,3, here every number is the sum of last two numbers.

Lets Have ah Look at the Program  ►► Continue reading C Program to Print Out Fibonacci Series upto N Terms

C Program to Convert Numbers Into Words

Lets make a program for converting number into words. I em not talking about converting numbers into their ASCII values, what I mean is juss like convert 1234 to “One Two Three Four”. I found this program useful and a better one to boost your logical skills.

Have ah Look at the Program ►►

#include<stdio.h>
#include<conio.h>
 
void main()
{
   int num,n,i=0;
   char *ptr[1000];

   printf("\nEnter any Number : ");
   scanf("%d",&num);

   while(num) {

   n=num%10;
   num/=10;

   switch(n) {

   case 0 :
           ptr[i++]="zero";
           break;

   case 1 :
           ptr[i++]="One";
           break;

   case 2 :
           ptr[i++]="Two";
           break;

   case 3 :
           ptr[i++]="Three";
           break;

   case 4 :
           ptr[i++]="Four";
           break;

   case 5 :
           ptr[i++]="Five";
           break;

   case 6 :
           ptr[i++]="Six";
           break;

   case 7 :
           ptr[i++]="Seven";
           break;

   case 8 :
           ptr[i++]="Eight";
           break;

   case 9 :
           ptr[i++]="Nine";
           break;
} // While close brace

for(int j=(i-1);j>=0;j--)
  printf("%s\t",ptr[j]); 
 
getch();   
}

Explanation ►►

So what is done in the program is first we declared a string pointer (*ptr[1000]), for storing the words of number in the pointer and then print them in reverse order to get the right output. Then conditioned the while loop as while(num) it will execute till the number comes to zero.

After the user entered the number it will store in the variable num, now it will go through the while loop till it does not have the value of zero. In while loop the first statement (n=num%10) will take the last digit of the num and will assign to variable n then we use the statement num/=10; (num=num/10;) to remove the last number from the variable num. It;s like suppose user entered 1234 then by first statement 4 will be stored in n as %(modulus) operator always return the remainder and then the number will be divide by 10 to remove the last digit(4), after the second statement the num value will be 123 and so on.

Then when we got the value in n we go to the switch statement then the variable n is compared with the cases and the matched case will be execute. As the value of n is 4(suppose) the case 4 will be execute and the string “Four” will be stored in char pointer(ptr) following the ptr[i++]=”Four”; statement. We use i++ inside the dimension of the array pointer because after every string stored the value of i will be incremented to point the next location in the char pointer to store any other string. The i++ is the post increment that means after using the variable (assigning the value to the location) it will increment the value of i by 1.

This process will be done for all digits and once the number becomes zero, means there is no digit left, the while loop will be terminate and then we will simply print the words in reverse order(because the digit last in the number is taken first) to make right form of the words.

Here in for loop we assign j as j=i-1, because the last case that will execute will increment the value of i, that means the i will be pointing to the empty location of the char pointer. By taking i-1 it will point to the last location of the char pointer.