WELCOME

Welcome to KoDes4U

Pages

Showing posts with label c-programs. Show all posts
Showing posts with label c-programs. Show all posts

Tuesday, March 15, 2011

distance conversion(basic C program)

/*The distance between two cities (in km) is entered through the keyboard. Write a program to convert and print this distance in meters, feet ,inches and centimeters. Code for Kodes4u.blogspot.com*/

#include
#include

void main()
{
float dis_km;
float dis_met,dis_feet,dis_inch,dis_cm;
clrscr();
printf("\nEnter a distance:");
scanf("%f",&dis_km);
dis_met=1000.00*dis_km;
printf("\nDistance in meter=%f",dis_met);
dis_feet=3280.839*dis_km;
printf("\nDistance in feet=%f",dis_feet);
dis_inch=39370.0787*dis_km;
printf("\nDistance in inches=%f",dis_inch);
dis_cm=100000.00*dis_km;
printf("\nDistance in cm=%f",dis_cm);
getch();
}

gross salary(basic C program)

/*Anil's basic salary is entered through the keyboard. His dearness allowance is 38% of basic salary, and house rent allowance is 20% of basic salary. Code a C program to find out his gross salary. coded for kodes4u.blogspot.com*/

#include
#include

void main()
{
float basic_salary,dearness_allow,house_rent,gross_salary;
clrscr();
printf("\Enter the basic salary of Anil:");
scanf("%f",&basic_salary);
dearness_allow=0.38*basic_salary;
house_rent=0.20*basic_salary;
gross_salary=basic_salary+dearness_allow+house_rent;
printf("\nAnil's gross salary is :%f",gross_salary);
getch();
}

Monday, March 14, 2011

prime number(c program)

/*Write a program to find whether a user entered number is prime or not. Coded for KoDes4U.blogspot.com*/

#include
#include

void main()
{
int num,i=2;
clrscr();
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num)
{
if(num%i==0)
{
printf("\nNot a prime number.");
break;
}
else
{
if(i==num/2)
printf("Prime number");
break;
}
i++;

}
getch();
}

Thursday, March 3, 2011

program to print ascii characters



/*Write a C program to print all the ASCII characters available in C
with an interval of 10 ASCII values at each enter hit.
coded by KoDes4U.blogspot.com*/


#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
  {
    int i;
    clrscr();
    for(i=1;i<=255;i++)
      {
        if(i%10==0)
          {
            getch();
            printf("\n");
          }
        else
          printf("%d=%c\t",i,i);
          delay(1500);
      }
    getch();
  }

accepting numbers and storing it on an array

/*Write a C program to accept n numbers from user and store these numbers in
an array. Find the min and max numbers....................................
Coded by KoDes4U.blogspot.com*/






#include<stdio.h>
#include<conio.h>
void main()
{
int num[5],i,min,max;
clrscr();
printf("Accepting of 5 numbers from user:\n");
for(i=0;i<5;i++)
{
printf("Enter the number num[%d]:",i);
scanf("%d",&num[i]);
}
printf("\nDisplay of numbers that was stored in the array num[]\n");
for(i=0;i<5;i++)
{
printf("%d  ",num[i]);
}
max=num[0];
for(i=0;i<5;i++)
{
if(max<num[i])
max=num[i];
}
printf("\nMaximum number is %d",max);
min=num[0];
for(i=0;i<5;i++)
{
if(min>num[i])
min=num[i];
}
printf("\nMinimum number is %d",min);
getch();
}

Even Number printig Code(C program)

/*Write a program to find the even numbers from 1 to 100
Coded for KoDes4U.blogspot.com*/
#include
#include
int main()
{
int i;
clrscr();
printf("Even numbers from 1 to 20\n");
for(i=1;i<=20;i++)
{
if(i%2==0)
printf("%d\n",i);
}
getch();
return 0;
}

Use of math.h library functions(C program)

/*Write a program to find cosine of 0 to 180 degree.
Coded for KoDes4U.blogspot.com*/
#include
#include
#include
int main()
{
int degree;
float pi=3.14,x,y;
clrscr();
printf("Degree\tCosine\n");
for(degree=0;degree<=180;degree=degree+10)
{
x=(pi/180)*degree;
y=cos(x);
printf("%d\t%f",degree,y);
printf("\n");
}
getch();
return(0);
}

Quadratic Equation Code(C program)

/*Write a program to find a quadratic equation ax2+bx+c=0.

Coded for KoDes4U.blogspot.com*/

#include

#include

#include

void main()

{

int a,b,c,disc;

float alpha,beta;

clrscr();

printf("\nEnter the value of a,b and c:");

scanf("%d%d%d",&a,&b,&c);

disc=(b*b)-(4*a*c);

if(disc<0)

printf("\nRoots are imaginary.");

else

{

alpha=(-b+sqrt(disc))/(2*a);

beta=(-b-sqrt(disc))/(2*a);

printf("\n1st Root=%f\t2nd Root=%f",alpha,beta);

}

getch();

}

Sunday, February 27, 2011

decimal to binary conversion



/*Write a C program to convert given decimal into its euivalent binary.
Coded by KoDes4u.blogspot.com*/


#include<stdio.h>
#include<conio.h>
#define MAX 10
void convert(int dec_num,int bin_num[]);
void main()
{
 int dec_num,bin_num[MAX];
 clrscr();
 printf("\nEnter a decimal number:");
 scanf("%d",&dec_num);
 convert(dec_num,bin_num);
 getch();
}
void convert(int dec_num,int bin_num[])
{
int i=0,j;
 while(dec_num!=0)
 {
  bin_num[i]=dec_num%2;
  dec_num=dec_num/2;
  i++;
 }
 printf("\nThe Binary equivalent is:");
 for(j=i-1;j>=0;j--)
 printf("%d",bin_num[j]);
}

Friday, February 25, 2011

string operation without using library function(c programs)



/*Write a program on string operations without using library functions
   coded by KoDes4U.blogspot.com*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
int length(char str[]);
void copy(char str[]);
int compare(char str1[],char str2[]);
void reverse(char str[]);
int pallindrme(char str[]);
void concat(char str1[],char str2[]);
int search(char str1[],char str2[]);
void count(char str[]);


void main()
{
 char str1[50],str2[50];
 int res,op;
 clrscr();
  do
 {
  printf("\n-------------------------------MENU-----------------------------");
  printf("\n1)Length\n2)Copy\n3)Compare\n4)Reverse\n5)Pallindrome");
  printf("\n6)Concatenate\n7)Search\n8)Count\n9)Exit");
  printf("\nEnter your choice:");
  scanf("%d",&op);
  flushall();
  switch(op)
  {
   case 1:printf("\nEnter any string:");
 gets(str1);
 res=length(str1);
 printf("\nLength of the string is: %d",res);break;
   case 2:printf("\nEnter any string:");
 gets(str1);
 copy(str1);
 break;
   case 3:printf("\nEnter 1st string:");
 gets(str1);
 printf("\nEnter 2nd string:");
 gets(str2);
 res=compare(str1,str2);
 if(res>0)
 printf("\n1st string is greater.");
 else if(res<0)
 printf("\n2nd string is greater.");
 else
 printf("\nBoth are same.");
 break;
   case 4:printf("\nEntre any string:");
 gets(str2);
 reverse(str1);
 break;
   case 5:printf("\nEnter any string:");
 gets(str1);
 res=pallindrome(str1);
 if(res==0)
 printf("\nPallindrome.");
 else
 printf("\nNot a pallindrome.");
 break;
   case 6:printf("\nEnter main string:");
 gets(str1);
 printf("\nEnter substring:");
 gets(str2);
 concat(str1,str2);break;
   case 7:printf("\nEnter the main string:");
 gets(str1);
 printf("\nEnter substrring:");
 gets(str2);
 res=search(str1,str2);
 printf("\nFound at location:%d",res);
 break;
   case 8:count(str1);
 break;
   case 9:break;
  }
 }              while(op!=9);
}


int length(char str[])
{
 int i=0;
 while(str[i]!='\0')
 i++;
 return(i);
}
void copy(char str[])
{
 char a[50];
 int i=0;
  printf("\nOriginal string is.%s",str);
 while(str[i]!='\0')
 {
 a[i]=str[i];
 i++;
 }
 printf("\nCopied string is:%s",a);
}
int compare(char str1[],char str2[])
{
 int i=0;
 while(str1[i]!='\0')
 {
  if(str1[i]>str2[i])
  return(1);
  if(str1[i]<str2[i])
  return(-1);
  i++;
 }
 return(0);
}


void reverse(char str[])
{
 int i=0,j=0;
 char temp[50];
 while(str[i]!='\0')
 i++;
 i--;
// n=i;
 //i=0;
 printf("\nOriginal string is:%s",str);
 //i=0;
 while(i>=0)
 {
  temp[j]=str[i];
  i--;
  j++;
 }
  printf("\nReversed string is:%s",temp);
}


void concat(char str1[],char str2[])
{
 int i=0,j=0;
 while(str1[i]!='\0')
 i++;
 printf("\nBefore concatenation:");
 printf("\n1st string is:%s",str1);
 printf("\n2nd string is:%s",str2);
 while(str2[j]!='\0')
 {
  str1[i]=str2[j];
  i++;j++;
 }
 printf("\nConcatenated string is:%s",str1);
}


int pallindrome(char str[])
{
 int i=0,j=0;
 while(str[j]!='\0')
 j++;
 j--;
 while(j>i)
 {
  if(str[i]!=str[j])
  return(1);
  i++;
  j--;
 }
  return(0);
}
int search(char str1[],char str2[])
{
 int i,j,lstr1,lstr2;
 lstr1=length(str1);
 lstr2=length(str2);
 for(i=0;i<=lstr1-lstr2+1;i++)
  for(j=0;str1[i+j]==str2[j]&&str2[j]!='\0';j++)
   if(str2[j]=='\0')
   return(i+1);
   return(0);
}
void count(char str[])
{
 int spcnt=0,chcnt=0,i;
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]==' ')
  spcnt++;
  chcnt++;
 }
 printf("\nNo. of spaces=%d",spcnt);
 printf("\nNo. of words=%d",spcnt+1);
 printf("\nNo. of characters=%s",chcnt);
}

matrix operation using array(c program)



/*write a program on matrix operation using array
 coded by KoDes4U.com*/




// list of include files
#include<stdio.h>
#include<conio.h>
#define SIZE 10


//Function definition for reading the matrix


void ReadMatrix(int mat[SIZE][SIZE],int *row,int *col)
{
 int i,j;
  for(i=0;i<*row;i++)
for(j=0;j<*col;j++)
 {
printf("\nEnter the element no:[%d][%]:",i,j);
  scanf("%d",&mat[i][j]);
 }
}
//Function definition for printing the matrix


void PrintMatrix(int mat[SIZE][SIZE],int row,int col)
{
 int i,j;
   for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
  printf("  %d",mat[i][j]);
  printf("\n\a");
}
}


//Function definition for addition of matrix


int AddMatrix(int mat1[SIZE][SIZE],int mat2[SIZE][SIZE],int mat3[SIZE][SIZE],int row1,int row2,int col1,int col2,int *row3,int *col3)
{
  int i,j;
  if(row1==row2&&col1==col2)   //size should be same
   {
for(i=0;i<row1;i++)
for(j=0;j<col1;j++)
 mat3[i][j]=mat1[i][j]+mat2[i][j];
*row3=row1;
  *col3=col1;
return(1);
   }
else
 {
printf("\nAddition is no possible");
return(-1);
 }
}


//Function of multiplication


int MulMatrix(int mat1[SIZE][SIZE],int mat2[SIZE][SIZE],int mat3[SIZE][SIZE],int row1,int row2,int col1,int col2,int *row3,int *col3)
{
 int i,j,k;
  if(col1==row2)
{
  for(i=0;i<row1;i++)
for(j=0;j<col2;j++)
{
  mat3[i][j]=0;
  for(k=0;k<col1;k++)
mat3[i][j]=mat3[i][i]+mat1[i][k]*mat2[k][j];
}
*row3=row1;
*col3=col2;
return(1);
}
  else
{
 printf("\nMultiplication is not possible:");
 return(-1);
}
}


//Function definition for transpose of the matrix
void Transpose(int mat1[SIZE][SIZE],int row1,int col1,int mat3[SIZE][SIZE],int *row3,int *col3)
{
  int i,j;
   for(i=0;i<col1;i++)
{
 for(j=0;j<row1;j++)
  {
mat3[i][j]=mat1[j][i];
  }
}
*row3=col1;
*col3=row1;
}


//Main Function definition
 void main()
 {
  int mat1[SIZE][SIZE],mat2[SIZE][SIZE],mat3[SIZE][SIZE];
int row1,row2,row3,col1,col2,col3;
  int choice,val;
clrscr();
  printf("\nEnter the rows and columns of 1st matrix:");
scanf("%d %d",&row1,&col1);
printf("\nRead matrix 1");
ReadMatrix(mat1,&row1,&col1);
printf("\nPrint matrix 1");
  PrintMatrix(mat1,row1,col1);
 printf("\nEnter the rows and columns of 2nd matrix:");
   scanf("%d %d",&row2,&col2);
 printf("\nRead matrix 2:");
ReadMatrix(mat2,&row2,&col2);
printf("\nprint matrix 2:");
PrintMatrix(mat2,row2,col2);
do
 {
  printf("\n1)ADD\n2)Multiplication\n3)Transpose\n4)Exit");
  printf("\nEnter your choice:");
  scanf("%d",&choice);
switch(choice)
{
 case 1:val=AddMatrix(mat1,mat2,mat3,row1,row2,col1,col2,&row3,&col3);
if(val!=-1)
 {
printf("Addition matrix is:");
PrintMatrix(mat3,row3,col3);
 }
break;
 case 2:val=MulMatrix(mat1,mat2,mat3,row1,row2,col1,col2,&row3,&col3);
  if(val!=-1)
  {
printf("\nMultiplication of matrices is:");
PrintMatrix(mat3,row3,col3);
  }
break;
 case 3:Transpose(mat1,row1,col1,mat3,&row3,&col3);
printf("\ntranspose matrix is:");
 PrintMatrix(mat3,row3,col3);
 break;
 case 4:break;
}


 }
while(choice!=4);
 }

swapping numbers(c programs)



/*Write a C program to swap two numbers without using third variable
  Coded by koDes4U,blogspot.com*/
#include<stdio.h>
#include<conio.h>
void main()
{
 int a=4,b=6;
 clrscr();
 printf("\nBefore swaping a=%d\tb=%d",a,b);
 a=a+b;
 b=a-b;
 a=a-b;
 printf("\nAfter swaping a=%d\tb=%d",a,b);
 getch();
}

alphabetical pattern printing( c-programs)









/*Write a C program to print the following pattern
ABCDEFGFEDCBA
ABCDEF FEDCBA                                                                
ABCDE   EDCBA                                                                
ABCD     DCBA                                                                
ABC       CBA                                                                
AB         BA                                                                
A           A


coded by KoDes4U.blogspot.com */


#include<stdio.h>
#include<conio.h>
void main()
{
  int i,j,k;
  clrscr();
  for(i=71;i>=65;i--)
    {
      for(j=65;j<=i;j++)
        {
           printf("%c",j);
        }
      for(k=71;k>j-1;k--)
        {
          printf(" ");
        }
      for(k=i;k<70;k++)
        printf(" ");
        for(j=j-1;j>=65;j--)
          {
            if(j==71)
              {
       j=70;
       printf("%c",j);
              }
            else
              printf("%c",j);
          }
        printf("\n");
    }
  getch();
}

pattern printing( c-programs)





/*Write a C program to print the following pattern
                          *
                      * *
* * *
                     * * * *         coded by KoDes4u.Blogspot.com*/

#include<stdio.h>
#include<conio.h>
void main()
{
  int i,j,k;
  clrscr();
  for(i=3;i>=0;i--)
    {
      for(j=0;j<i;j++)
        {
          printf(" ");
        }
      for(k=0;k<4-i;k++)
        {
          printf(" *");
        }
        printf("\n");
    }
  getch();
}