Saturday, August 20, 2011

count the array elements


Write a C Program to count the array elements



#include<conio.h>
#include<stdio.h>
void main()
{
int a[10],i,c=0;
clrscr();
printf("enter array elements=");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
c++;
}
printf("counting elements of an array is:%d",c);
getch();
}



Program Coded by: Ashish Garg, Patiala (India)

Friday, August 19, 2011

Subtraction of Two Matrices


C Program for subtraction of two matrices 

#include<stdio.h>
#include<conio.h>

//Read Matrix
void read_mat(float a[][10],int m,int n)
{
int i,j;
printf("\n\nEnter %d X %d matrix below:\n",m,n);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
}

//Write Matrix
void write_mat(float a[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%10.2f",a[i][j]);
printf("\n");
}
}

//Subtract matrices
void sub_mat(float a[][10],float b[][10],float c[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j] - b[i][j];
}

//main function
void main()
{
float x[10][10],y[10][10],z[10][10];
int m,n;
clrscr();

//Accept two matrices
printf("\n\nEnter order of matrix \n");
scanf("%d %d",&m,&n);
read_mat(x,m,n);
read_mat(y,m,n);

//call sub_mat() function
sub_mat(x,y,z,m,n);
printf("\n\nSUBTRACTION OF THE GIVEN MATRICES IS:\n");
write_mat(z,m,n);
getch();
}

Find Inverse of a Given Matrix


Write a Program to find inverse of a given matrix

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

//Read Matrix
void read_mat(float a[][10],int n)
{

int i,j;
printf("\n\nEnter %d X %d matrix below:\n",n,n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);

}

//Write Matrix
void write_mat(float a[][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%10.2f",a[i][j]);
printf("\n");
}
}

//Swap Rows
void swap_rows(float a[][10],int n,int i,int j)
{
int k,temp;
for(k=0;k<n;k++)
{
temp=a[i][k];
a[i][k]=a[j][k];
a[j][k]=temp;
}
}

//Multiplication of rows
void row_mult(float a[][10],int n,int i,float x)
{
int k;
for(k=0;k<n;k++)
a[i][k]*=x;
}

// Subtraction of rows
void row_sub(float a[][10],int n,int i,int j,float x)
{
int k;
for(k=0;k<n;k++)
a[j][k]-=x*a[i][k];
}

//Inverse of matrix
void inverse(float a[][10],float ia[][10],int n)
{
int i,j;
for(i=0;i1.0e-6)
break;
if(j==n)
{
printf("Inverse does not exist");
getch();
exit(0);
}
swap_rows(a,n,i,j);
swap_rows(ia,n,i,j);
}
row_mult(ia,n,i,1/a[i][i]);
row_mult(a,n,i,1/a[i][i]);
for(j=0;j<n;j++)
if(i!=j)
{
row_sub(ia,n,i,j,a[j][i]);
row_sub(a,n,i,j,a[j][i]);
}
}
}

//main function
void main()
{
float a[10][10],b[10][10];
int n;
clrscr();

//Accept Matrix
printf("\n\nEnter order of the square matrix \n");
scanf("%d",&n);
read_mat(a,n);

//inverse the matrix
inverse(a,b,n);
printf("\n \nInverse of the given square matrix is : \n");
write_mat(b,n);
getch();
}

Factorial off a number using "do while" loop


#include<stdio.h>
#include<conio.h>
void main()
{
int n,f=1;
clrscr();
printf("enter any number=");
scanf("%d",&n);
do
{
f=f*n;
n--;
}
while(n>0);
printf("factorial number is=%d",f);
getch();
}
Courtesy: Ashish Garg
Patiala, India

Add numbers using command line arguments (CLA)

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main(int argc,char *argv[])
{
int sum=0,i;
//Compare if proper number of arguments have been entered
if(argc<3)

{
printf("Insufficient number of arguments:\n");
getch();
return 0;
}

//Add all the numbers entered using atoi function
for(i=1;i<argc;i++)
{
sum+=atoi(argv[i]);
}

//print the sum
printf("Ans=%d",sum);
getch();
}


Factorial of Number Using While Loop

#nclude <stdio.h>
#include <conio.h>
void main()
{
int i=1,n,fact=1;
clrscr();
printf("enter a number");
scanf("%d",&n);
while(i<=n)
{
fact=fact*i;
printf("%d",i);
i++;
}
printf("fact=%d",fact);
getch();
}

Thursday, August 18, 2011

Multiply and swap 2 nmbers using bitwise operators



#include<stdio.h>
#include<conio.h>
void main(void)
{
int a,b;
clrscr();
printf("Input value of A: ");
scanf("%d",&a);
printf("Input value of B: ");
scanf("%d",&b);
a=a^b;
b=b^a;
a=b^a;
printf("After Swapping:\n");
printf("Value of A: %d",a);
printf("\nValue of B: %d",b);
getch();
}

Wap to reverse words



‎#include <stdio.h>
#include <conio.h>
#include <string.h>
const int opt = 50;
int main(){
int i=0,j=0,c=0,vali[opt] = {0};
char str1[opt],str2[opt]="",str3[opt]="",ch;
printf("please enter your sentence :");
gets(str1);
for(i=0;str1[i] != '\0';i++){
if(str1[i] == ' '){
vali[j+1] = i;
j++;
}
}
c = j;
for(i=0;i<=j && c!=0;i++){
strcat(str2,&str1[vali[c]+1]);
strcpy(&str1[vali[c]],str3);
strcat(str2," ");
c--;
}
strcat(str2,str1);
printf("\nyour reversed sentence is :");
puts(str2);
getch();
return 0;
}

Pyramid using nested for loops




#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=5;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
}






OUTPUT


5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

Whether the given no. Is palindrome or not



  #include<stdio.h>
  #include<conio.h>
  void main()
  {
   int n,r,t,sum=0;
   clrscr();
   printf("    OUTPUT:\n");
   printf("\tEnter a no.");
   scanf("%d",&n);
   t=n;
   while(n!=0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
   if(sum==t)
   printf("\tThe no. %d is a pallindrome",t);
   else
   printf("\tThe no. %d is not a pallindrome",t);
   getch();
  }

Whether the given no. is armstrong or not


  #include<stdio.h>
  #include<conio.h>
  void main()
   {
    int n,r,t,sum=0;
    clrscr();
    printf("  OUTPUT :\n");
    printf("\tEnter a no.");
    scanf("%d",&n);
    t=n;
    while(n!=0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
   if(sum==t)
   printf("The no. %d is armstrong",t);
   else
   printf("The no. %d is not an armstrong",t);
   getch();
  }

Concatenate Two Strings



  #include<stdio.h>
  #include<conio.h>
  #include<string.h>
   void main()
    {
     char c[100];
     char a[50];
     char b[50];
     clrscr();
     printf("Enter a string1:");
     gets(a);
     printf("Enter a string2:");
     gets(b);
     strcat( a,b);
     printf("%s",a);
     getch();
    }

Print Second Largest Among Given Three No.s



  #include<stdio.h>
  #include<conio.h>
  void main()
   {
    int a,b,c;
    clrscr();
    printf("   OUTPUT :\n");
    printf("Enter any three no.s:");
    scanf("%d%d%d",&a,&b,&c);
    if(a>b&&a>c)


if(b>c)
printf("%d is the second largest no.",b);
else
printf("%d is the second largest no.",c);


    if(b>a&&b>c)


if(a>c)
printf("the second largest no. is % d",a);
else
printf(" the second largest no. is %d",c);


    if(c>a&&c>b)


if(b>a)
printf("second largest no. is %d",b);
else
printf("second largest no. is %d",a);


    getch();
   }

Count no. of students above,below and average students



  #include<stdio.h>
  #include<conio.h>
   void main()
    {
     int a[10];
     int aa=0,ba=0,ae=0,i;
     clrscr();
     printf("Enter the marks:\n");
     for(i=0;i<10;i++)
       {
scanf("%d",&a[i]);
if(a[i]>55)
aa++;
else if(a[i]<55)
ba++;
else
ae++;
       }
     printf("No. OF AVG STUDENTS ARE:%d\n",ae);
     printf("No. OF ABOVE AVERAGE STUDENTS:%d\n",aa);
     printf("No. OF BELOW AVERAGE STUDENTS ARE:%d",ba);
    getch();
   }

Print Armstrong numbers Less Than 1000



  #include<stdio.h>
  #include<conio.h>
  void main()
   {
    int q,a,b,c,z,x,n=1;
    clrscr();
    printf("  OUTPUT :");
    while(n<1000)
{
a=n%10;
q=n/10;
b=q%10;
c=q/10;
z=((c*100)+(b*10)+a);
x=(a*a*a)+(b*b*b)+(c*c*c);
if(x==z)
printf("\n\t%d",n);
n++;
}
    getch();
   }

Add Pointers



#include<stdio.h>
#include<conio.h>
 void main()
 {
 int a[10],sum=0;
 int *j;
 int i,n;
 clrscr();
 printf("Enter how many elements u want to add:");
 scanf("%d",&n);
 printf("Enter the elements:");
 for(i=0;i<n;i++)
 {
 scanf("%d",&a[i]);
 }
  j=&a[0];
 for(i=0;i<n;i++)
     {


       sum=sum+*j;
       j++;
     }
 printf("sum=%d",sum);
}

Add two matrices and store the result



#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("Enter the elements into matrix A\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the elements into matrix B\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
for(i=0;i<3;i++)
{
printf("\t\t");
for(j=0;j<3;j++)
printf("%d\t",c[i][j]);
printf("\n\a");
}


getch();
}

Find address of char, string, integer



#include<stdio.h>
#include<conio.h>
 main()
  {
   char *chp,*sp;
   int i;
   char ch,s[10];
   int *ip;
   clrscr();
   printf("Enter a char:");
   scanf("%c",ch);
   printf("Enter a string:");
   scanf("%s",s);
   printf("Enter a integer:");
   scanf("%d",&i);
   chp=&ch;
   sp=s;
   ip=&i;
   printf("\nchar\tadd\tstring\t\tstringadd\tint\tint add\n");
   printf("%c\t%u\t%s\t\t%u\t\t%d\t%u",ch,&chp,s,&s,i,&i);
   printf("\nchar pointer value is:%u",chp);
   printf("\nstring pointer value is:%u",sp);
   printf("\nint pointer value is:%u",ip);
   getch();
  }

Print Second Largest Among Given Three No.S



#include<stdio.h>
  #include<conio.h>
  void main()
   {
    int a,b,c;
    clrscr();
    printf("   OUTPUT :\n");
    printf("Enter any three no.s:");
    scanf("%d%d%d",&a,&b,&c);
    if(a>b&&a>c)


if(b>c)
printf("%d is the second largest no.",b);
else
printf("%d is the second largest no.",c);


    if(b>a&&b>c)


if(a>c)
printf("the second largest no. is % d",a);
else
printf(" the second largest no. is %d",c);


    if(c>a&&c>b)


if(b>a)
printf("second largest no. is %d",b);
else
printf("second largest no. is %d",a);


    getch();
   }

Print all permutations of a given string


# include <stdio.h>
# include <conio.h>

/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
    char temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

/* Function to print permutations of string
   This function takes three parameters:
   1. String
   2. Starting index of the string
   3. Ending index of the string. */
void permute(char *a, int i, int n)
{
   int j;
   if (i == n)
     printf("%s\n", a);
   else
   {
        for (j = i; j <= n; j++)
       {
          swap((a+i), (a+j));
          permute(a, i+1, n);
          swap((a+i), (a+j)); //backtrack
       }
   }
}

/* Driver program to test above functions */
int main()
{
   char a[] = "ABC";
   permute(a, 0, 2);
   getchar();
   return 0;
}

Wednesday, August 10, 2011

C Program find Positive Negative with Switch Case Without Conditional Operator


write a program in c to check whether the given num is Positive or negative using switch case without conditional operator

 ‎#include<stdio.h>
#include<conio.h>
void main(void)
{
char num;
clrscr();
printf("Enter a number +ve or -ve : ");
scanf("%c",&num);
switch(num)
{
case '-':
printf("Negative number");
break;
default:
printf("Positive number");
}
getch();
}

Example of Using Strings in C


Example of Using Strings in C

# include<stdio.h>
# include<conio.h>
# include<string.h>

void main()
{
char *a;
printf("Enter your name=");
gets(a);
printf("%s",a);
getch();

ATM programing


ATM C programing language Program code

 ‎/*Note Pin code is 1234*/
#include<stdio.h>
#include<conio.h>

void main(void)
{ unsigned long amount=1000,deposit,withdr​aw;
int choice,pin=0,k=0;
char another='y';

while(pin!=1234)
{ clrscr();
gotoxy(30,25);
printf("Enter pin:");
scanf("%d",&pin);
}
clrscr();
do
{

printf("********Welcome to ATM Service**************\n");
printf("1. Check Balance\n");
printf("2. Withdraw Cash\n");
printf("3. Deposit Cash\n");
printf("4. Quit\n");
printf("******************​**************************​*\n\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nYour Balance is Rs : %lu ",amount);
break;
case 2:
printf("\nEnter the amount to withdraw: ");
scanf("%lu",&withdraw);
if(withdraw%100!=0)
{
printf("\nPlease enter

amount in multiples of 100");
}else if(withdraw>(amount-500))
{

printf("\nInsufficient Funds");
}else
{

amount=amount-withdraw;

printf("\n\nPlease collect cash");

printf("\nYour balance is %lu",amount);
}
break;
case 3:
printf("\nEnter amount to deposit");
scanf("%lu",&deposit);
amount=amount+deposit;
printf("Your balance is %lu",amount);
break;
case 4:
printf("\nThank you for using ATM");
break;
default:
printf("\nInvalid Choice");
}
printf("\n\n\nDo you want another transaction?(y/n): ");
fflush(stdin);
scanf("%c",&another);
if(another=='n'||another==​'N')
k=1;
}while(!k);
printf("\n\nHave a nice day");
getch();

}

Greatest of two numbers using relational conditional operators

program to find greatest of 2 numbers without using relational and conditional operators

‎#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
int a,b;
clrscr();
printf("Enter a number: ");
scanf("%d",&a);
printf("Enter another number: ");
scanf("%d",&b);
printf("The greatest number is %d",(a+b+abs(a-b))/2);
getch();
}

how 2 find a given number is positive or negative number in C without using relational and conditional operator


‎# include<stdio.h>
# include<conio.h>
# include<math.h>
void main()
{
clrscr();
int p,n;
printf("Enter two no=");
scanf("%d",&p);
switch(n=abs(p)+(-p))
{
case 0:
{
printf("Positive number");
break;
}
default:
{
printf("Negative number");
break;
}
}
getch();
}

find a given number is positive or negative number in C without if statement, relational and conditional operator

# include <math.h>
# include <stdio.h>
# include <conio.h>
void main()
{
clrscr();
int p,n;
printf("Enter two no=");
scanf("%d",&amp;p);
switch(n=abs(p) +(-p))
{
case 0:
{
printf("Positiv e number");
break;
}
default:
{
printf("Negativ e number");
break;
}
}
getch();
}

Facebook Page

we have recently created Facebook Page for free-c-programs.blogspot.com. On this page we will be updating the information and posts about this websites. If you are on facebook, you can like this page just by clicking on LIKE button on the right box of this website, which displays on every page of this site. C Programming Facebook Page will provide you Material about Progarmming Language and other related websites

Join us on Facebook Now :)