18 October 2014

C Programming Examples - Part 2

Here is the list of sample programs using C, This is list number 2. There are 21 more programs at this post.
http://www.nsnam.com/2014/10/simple-c-programs-for-beginners.html
C Program 1 – To find the reversal of a given number
Assume the number 1234, after reversal the number becomes 4321.
Input: One number, Ex 1234, declared to num
Output : One number, Ex 4321, rev is the variable
Logic:
  1. separate each digit using modulo operator from the right and bring that number to front
  2. divide the given number by 10, so the right most digit will be discarded.
  3. run till the number becomes <=0
output the result
#include <stdio.h>
#include <conio.h>

int main()
{
    int num,rev=0;
    printf("enter the number to be reversed");
    scanf("%d", &num);

    while(num>0) //till the number is positive, perform the process
    {
    rev=rev*10 + (num%10);
//separate each digit and bring it to the first
    num=num/10;
    }
    printf("The reversal is %d ",rev);
    getch();
    return 0;
}
C Program 2 – Electricity Bill calculation

This program is to display the electricity bill calculation based on the number of units consumed every month
Input : the number of units – variable name – unit
Output – Amount of rupee – variable name – amount
Logic:
Units Rupees
1-50 units 0.75/unit
51-100 0.85/unit
101-200 1.50/unit
201-300 2.20/unit
>300 3.00/unit
In all the cases, there will be service charge of Rs.20 will be collected.
#include <stdio.h>
#include <conio.h>
int main()
{
    float amount=0,units;;
    printf("Enter the number of units");
    scanf("%f", &units);
    if(units <=50)
    {
     amount = units * 0.75;       
    }   
    else if(units >50 && units <=100)
    {
         amount=0.75 * 50 + 0.85*(units-50);
    }
    else if(units >100 && units <200)
    {
         amount=(0.75*50) + (0.85*50 )+ (1.5 *(units-100));
    }
    else if(units >200 && units <300)
    {
         amount=(0.75*50) + (0.85*50 )+ (1.5 *100) + (2.20 *(units-200));
    }
    else
    {
         amount=(0.75*50) + (0.85*50 )+ (1.5 *100) + (2.20 * 100) +(3.0*(units-300));
    }
    amount=amount+(0.2*amount);
    printf("The total electricity bill is %f", amount);
    getch();
    return 0;
}
C Program 3 – To check whether a number is Armstrong Number
The armstrong number is of the form 153= 13 + 53 + 33
The input is : 153 or any other number
output: The number is armstrong or not.
Processing: take 153 as an example, remove 3, 5 and 1 in the reverse order (using % operator) and take the power of 3 and add to the sum variable.
if the total sum and the original number, both are same, then that is the arm strong number.
if else, the number is not an armstrong number
#include <stdio.h>
#include <conio.h>

int main()
{
    int original_num, check, temp, sum=0;
    printf("Enter the number to check for armstrong number");
    scanf("%d", &original_num);
// Get the original number
    temp=original_num;
    while(original_num>0)
//run the loop till the number becomes 0
    {
     check=original_num%10; 
//remove the last digit using modulo operator
     sum=sum+check*check*check; //the last digit is taken power to 3 and added to sum
     original_num=original_num/10; //truncate the last digit and run the loop again
     }
     if(sum==temp)
     printf("This is an armstrong number\n");
     else
     printf("This is not an armstrong number \n");
     getch();
     return 0;
}

C Program 4 – To check whether a given number is prime or not
A prime number can be divided by 1 and itself, there are no other divisors,
Examples are : 2 3, 5, 7, 11, …..
To find out whether a given number is prime or not, here is the logic
1. Get the number
2. divide the given number from 2 to n-1 (Example if 6 is the number divided by 2,3,4,5 will get the remainder respectively 0,0,2,3)
3. increment a counter to 1 if the remainder is 0
4. if there counter variable is 0, then the given number is prime (because we didn’t get any remainder) else non prime
Here is the program
#include <stdio.h>
#include <conio.h>
int main()
{
    int a,i,count=0;
    printf("enter a"); //Let the given number is a
    scanf("%d",&a); //get the number     for(i=2;i<a;i++) //divide the number a from 2 to a-1
    {
    if(a%i==0) 
    count++; //increment a counter if the divisibility is 0     }
    if(count !=0) //if the counter is not zero, then prime
    printf("a is not a prime number");
    else
    printf("a is a prime number");
getch();
return 0;
}

C Program 5 – Arrays with Functions (passing Array elements to function)

/*
Program to demonstrate arrays with function in which the array elements are passed as function parameters
*/
#include <stdio.h>
#include <conio.h>
void display(int); //function prototype
int main()
{
    int a[10],i;
    for(i=0;i<10;i++) //getting the array elements
    {
    scanf("%d",&a[i]);
    }
    for(i=0;i<10;i++)
        display(a[i]); //call the function inside the loop, this function called for 10 times     getch();
    return 0;
}
void display(int a) //function implementation to print the elements, here the parameter is an integer only(means we are passing the array elements)
{
     printf("%d",a);
}
C Program 6 – Passing an entire array to a function
/*
This program is to pass the entire array to a function
*/
#include <stdio.h>
#include <conio.h>
int maximum(int[],int); //function prototype, 1st parameter is array and the second is integer
int main()
{
    int a[100],i,n,max=0;
    printf("Enter the numbers"); 
    scanf("%d",&n); //get the index number
    for(i=0;i<n;i++) //get the array elements
    {
    scanf("%d",&a[i]);
    }
    max=maximum(a,n); //function call
    printf("Maximum number is %d",max);
    getch();
    return 0;
}
int maximum(int b[],int a) //function implementation
{
    int i,max=b[0];
    for(i=0;i<a;i++)
    if(b[i]>max)
    max=b[i];
    return max;
}
In the function call ie
max=maximum(a,n) where a is the array and n is the number of array elements
the above line can be written as
max = maximum(&a[0],n)
since the array is just a memory address, it is enough to mention the base address (address of the first element) of the array
so the array name is just equivalent to the base address
in the above example
a means &a[0]

C Program 7 – To sort a Given set of numbers in ascending order (BubbleSort)
/* Program to sort the given set of numbers in
ascending order, this sorting is called as bubble sort algorithm
*/
#include <stdio.h>
#include <conio.h>
int main()
{
    int a[10],i,j,temp=0;
    printf("Enter all the 10 numbers");
    for(i=0;i<10;i++)
    scanf("%d",&a[i]);
    for(i=0;i<10;i++)  //This loop is for total array elements (n)
    {
    for(j=0;j<9;j++) //this loop is for total combinations (n-1)
    {
                    if(a[j]>a[j+1]) //if the first number is bigger then swap the two numbers
                    {
                    temp=a[j];
                    a[j]=a[j+1];
                    a[j+1]=temp;
                    }
    }
    }
printf("The ordered array is");
for(j=0;j<10;j++) //Finally print the ordered array
printf("%d \t",a[j]);
getch();
return 0;
}


C Program 8 – To search a number in a given array
//This program is to search a given number in an array
#include <stdio.h>
#include <conio.h>
int main()
{
    int a[10],i,num;
    printf("enter the array elements");
    for(i=0;i<10;i++) //get all the numbers
    scanf("%d",&a[i]);
    printf("Enter the number to search");
    scanf("%d",&num);
    for(i=0;i<10;i++)
    {
                     if(a[i]==num) //given num is matched in the array
                     {
                     printf("The number is found in the %d position",i+1);
                     getch();
                     exit(0); //to go the end of the program
                     }
    }
printf("The number is not found"); //if num not found, this will be displayed
getch();
return 0;
}

C Program 9 – Adding two matrices
/* Program to add two matrices */
#include <stdio.h>
#include <conio.h>
int main()
{
    int a[10][10], b[10][10],c[10][10],i,j;
    printf("Enter a");
    for(i=0;i<2;i++)  //get the matrix A
        for(j=0;j<2;j++)
    scanf("%d",&a[i][j]);
    printf("Enter b");
    for(i=0;i<2;i++) //get the matrix B
        for(j=0;j<2;j++)
    scanf("%d",&b[i][j]);
   
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
        c[i][j] = a[i][j] +b[i][j];    //adding two matrices
        }
    }
    printf("Added Matrix is \n");
    for(i=0;i<2;i++)
        for(j=0;j<2;j++)
    printf("%d ",c[i][j]);
        getch();
        return 0;
}


C Program–Multiplying two matrices
/* Program to multiply two matrices */
#include <stdio.h>
#include <conio.h>
int main()
{
    int a[2][3],b[3][2],c[2][2],k,j,i;
    printf("enter a");
    for(i=0;i<2;i++)   //Get array A
    {
    for(j=0;j<3;j++)
    {
    scanf("%d",&a[i][j]); 
    }
    }
    printf("enter b");
    for(i=0;i<3;i++) //Get array B
    {
    for(j=0;j<2;j++)
    {
    scanf("%d",&b[i][j]);
    }
    }

    for(i=0;i<2;i++) 
    {
    for(j=0;j<2;j++)
    {
    c[i][j]=0;  //to Hold a temporary multiplication
    for(k=0;k<3;k++)
    {
    c[i][j]=c[i][j]+a[i][k]*b[k][j];   //Multiplication algorithm
    }
    }
    }
    printf("C is ");
    for(i=0;i<2;i++)
    for(j=0;j<2;j++)
    printf(" c[%d][%d] - %d \n",i,j,c[i][j]); 
 
getch();  
    return 0;
}

 T S Pradeep Kumar