Archive for the ‘C’ Category

09
Mar

The  code given  is a calculator in  C. The scanf which scans the input as a number symbol just as in case of normal calculator.

#include<stdio.h>
float add(float,float);
float sub(float,float);
float product(float,float);
float divide(float,float);

void main()
{
float n1,n2;
char sym,choice;
printf("This Program is a program for calculator\n\n");
scanf("%f%c%f",&n1,&sym,&n2);
if(sym=='+')
printf("\n%f",add(n1,n2));
if(sym=='-')
printf("\n%f",sub(n1,n2));
if(sym=='*')
printf("\n%f",product(n1,n2));
if(sym=='/')
printf("%f",divide(n1,n2));
printf("\nDo you wish to continue[y/n]");
scanf("%s",&choice);
if(choice=='y'||choice=='Y')
main();
}

float add(float m1,float m2)
{
return(m1+m2);
}

float sub(float m1,float m2)
{
return(m1-m2);
}

float product(float m1,float m2)
{
return(m1*m2);
}

float divide(float m1,float m2)
{
return(m1/m2);
}

01
Mar
io.input( [file] )

Example:
local tmp = io.input()    -- save current file handle
io.input( "newfile.txt" ) -- open new file in text mode
print( io.read() )        -- read and display the file
io.input():close()        -- close the file
io.input( tmp )           -- restore the previous file

09
Oct

Here is the code for swaping two numbers without using temporary variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter the two numbers to swap");
scanf("%d%d",&a,&b);
a = a+b;
b = a-b;
a = a-b;
printf("After swapping the two numbers");
printf("a=%d",a);
printf("b=%d",b);
}

05
Oct

This is the simple program in c to perform multiplication without using multiply(*) symbol in c.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,k=0,m,n;
clrscr();
printf("Enter the first and second number to multiply");
scanf("%d%d",&m,&n);
for(i=1;i<=m;i++)
{
k = n + k;
}
printf("%d",k);
getch();
}

17
Sep

Have you ever seen a statement “return ~0″ in C Programming Language.
~ is a bitwise not/complement, it changes all 0′s to 1′s and vice-versa.
~0 is a value with all bits set to 1.