Some Programs
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <math.h>
void main( )
{
int i , len, deci, z, d, j,flg1,flg2;
char a[25], c;
clrscr( );
flg1=0;
flg2=0;
do {
printf("Enter Hexadecimal number: ");
scanf("%s",&a);
flushall();
i=0;
len=0;
while (a[i]!='\0')
{
len++;
i++;
}
len--;
deci=0;
d=0;
z=0;
for (i=len; i>=0; i--)
{
c=tolower(a[i]);
z=pow(16,d);
j=a[i]-48;
if((c >='a' && c <='f')|| (j <= 9))
{
switch (c)
{
case 'a':deci=deci+10*z; break;
case 'b':deci=deci+11*z; break;
case 'c':deci=deci+12*z; break;
case 'd':deci=deci+13*z; break;
case 'e':deci=deci+14*z; break;
case 'f':deci=deci+15*z; break;
}
switch(j)
{
case 1:deci=deci+1*z; break;
case 2:deci=deci+2*z; break;
case 3:deci=deci+3*z; break;
case 4:deci=deci+4*z; break;
case 5:deci=deci+5*z; break;
case 6:deci=deci+6*z; break;
case 7:deci=deci+7*z; break;
case 8:deci=deci+8*z; break;
case 9:deci=deci+9*z; break;
}
d++;
}
else
{
printf("\nInvalid Hexadecimal number\n");
flg1=1;
break;
}
}
if(flg1==0)
{
printf("\nDecimal equivalent of Hexadecimal %s is %d ",a,deci);
}
printf("\n\nEnter the Octal number: ");
scanf("%s",&a);
i=0;
len=0;
while(a[i]!='\0')
{
len++;
i++;
}
len--;
deci=0;
d=0;
z=0;
for (i=len; i>=0; i--)
{
z=pow(8,d);
j=a[i]-48;
if (j<=7)
{
switch (j)
{
case 1:deci=deci+1*z; break;
case 2:deci=deci+2*z; break;
case 3:deci=deci+3*z; break;
case 4:deci=deci+4*z; break;
case 5:deci=deci+5*z; break;
case 6:deci=deci+6*z; break;
case 7:deci=deci+7*z; break;
}
}
else
{
flg2=1;
printf("Invalid Octal Number\n");
break;
}
d++;
}
if(flg2==0)
{
printf("\nDecimal equivalent of Octal %s is %d ",a,deci);
printf("\n");
}
printf("\nPress 'Y' to continue any other key to terminate: ");
scanf ("%c",&c);
c=getchar( );
printf("\n\n");
}while (c == 'Y');
#include <stdio.h>
#include <conio.h>
void main()
{
char strn[100];
int i, len;
printf("Enter the string\n");
scanf("%[^\n]",&strn);
i=0;
len=0;
/*It even counts the number of spaces included in the string*/
while(strn[i] != '\0')
{
len++;
i++;
}
printf("Length of the entered string is : %d ",len);
}
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main ()
{
char strn [100];
int i;
i = 0;
printf ("Enter any string\n");
scanf("%[^\n]",strn);
while (strn[i]!= '\0')
{
isupper(strn[i])?(strn[i]=tolower(strn[i])):(strn[i]=toupper(strn[i]));
i++;
}
printf ("Coverted string is: %s\n",strn );
getch();
}
#include <stdio.h>
#include <conio.h>
void main( )
{
int i,j,flg=0;
char string1[100], string2[25];
clrscr( );
printf("Enter the first string\n");
scanf("%[^\n]", &string1);
flushall( );
printf("Enter the second string\n");
scanf("%[^\n]", &string2);
flushall( );
i=0;
j=0;
while (string1[i]!='\0'||string2[j]!='\0')
{
if(string1[i]!= string2[j])
{
flg=1;
break;
}
i++;
j++;
}
if(flg==0)
{
printf("The strings are equal \n");
}
else
{
printf("The strings are not equal \n");
}
getch();
}
#include <stdio.h>
#include <conio.h>
void main( )
{
int i,j;
char string1[100], string2[25];
clrscr( );
printf("Enter the first string\n");
scanf("%[^\n]", &string1);
flushall( );
printf("Enter the second string\n");
scanf("%[^\n]", &string2);
flushall( );
i=0;
j=0;
i=0;
while (string1[i] !='\0')
{
i++;
}
j=0;
string1[i]=' ';
i++;
while (string2[j] !='\0')
{
string1[i]= string2[j];
j++;
i++;
}
string1[i] ='\0';
printf("The resultant string after concatenation is: %s \n", string1);
getch( );
}
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main()
{
char strn[100],ch;
int i, vowel;
i=0;
vowel=0;
clrscr();
printf("Enter the string\n");
scanf("%[^\n]",&strn);
while(strn[i] != '\0')
{
ch=tolower(strn[i]);
switch(ch)
{
case 'a':
vowel++;
break;
case 'e':
vowel++;
break;
case 'i':
vowel++;
break;
case 'o':
vowel++;
break;
case 'u':
vowel++;
break;
}
i++;
}
printf("Number of vowels present in the entered string is : %d",vowel);
getch();
}
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main ()
{
char strn[100];
int i;
i = 0;
printf ("Enter any string\n");
scanf("%[^\n]",&strn);
strn[i]=toupper(strn[i]);
while (strn[i]!= '\0')
{
if(strn[i]==' ' && strn[i+1]!=' ')
{
strn[i+1]=toupper(strn[i+1]);
}
i++;
}
printf ("Coverted string is: %s\n",strn );
getch();
}
#include <stdio.h>
#include <conio.h>
void main()
{
char strn[100],temp[100];
int i, j,flg;
flg=0;
printf("Enter the string\n");
scanf("%[^\n]",&strn);
i=0;
while(strn[i] != '\0')
{
i++;
}
i--;
j=0;
while( i >=0)
{
temp[j]=strn[i];
j++;
i--;
}
temp[j]='\0';
i=0;
printf("\nReversed string is --> %s \n",temp);
while(strn[i] != '\0')
{
if (strn[i] != temp[i])
{
flg=1;
break;
}
i++;
}
If(flg==0)
printf("\nEntered string %s ---> is a palindrome\n",strn);
else
printf("\nEntered string %s ---> is not a palindrome\n",strn);
}
/* Program to add and multiply two complex numbers*/
#include <stdio.h>
#include <conio.h>
struct complex
{
int real;
int img;
};
void main()
{
struct complex c1,c2,c3;
struct complex complex_add();
struct complex complex_mult();
printf("Enter the real and imaginary parts of first complex number: ");
scanf("%d%d",&c1.real,&c1.img);
printf("Enter the real and imaginary parts of second complex number: ");
scanf("%d%d",&c2.real,&c2.img);
c3=complex_add(c1,c2);
printf("\nResult of addition of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
c3=complex_mult(c1,c2);
printf("\nResult of multiplication of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
getch();
}
struct complex complex_add(cmpl1, cmpl2)
struct complex cmpl1,cmpl2;
{
struct complex cmpl3;
cmpl3.real=cmpl1.real + cmpl2.real;
cmpl3.img=cmpl1.img + cmpl2.img;
return(cmpl3);
}
struct complex complex_mult(cmpl1, cmpl2)
struct complex cmpl1,cmpl2;
{
struct complex cmpl3;
cmpl3.real=cmpl1.real*cmpl2.real - cmpl1.img*cmpl2.img;
cmpl3.img=cmpl1.real*cmpl2.img + cmpl1.img*cmpl2.real;
return(cmpl3);
}
/* Program to add and multiply two complex numbers using structure pointers */
struct complex
{
int real;
int img;
};
void main()
{
struct complex c1,c2,c3;
void complex_add();
void complex_mult();
printf("Enter the real and imaginary parts of first complex number: ");
scanf("%d%d",&c1.real,&c1.img);
printf("Enter the real and imaginary parts of second complex number: ");
scanf("%d%d",&c2.real,&c2.img);
complex_add(&c1,&c2,&c3);
printf("\nResult of addition of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
complex_mult(&c1,&c2,&c3);
printf("\nResult of multiplication of two complex numbers\n");
printf("\nReal and Imaginary parts of the complex number is:");
printf("%d+i%d\n",c3.real,c3.img);
getch();
}
void complex_add(cmpl1, cmpl2, cmpl3)
struct complex *cmpl1,*cmpl2, *cmpl3;
{
cmpl3->real=cmpl1->real + cmpl2->real;
cmpl3->img=cmpl1->img + cmpl2->img;
}
void complex_mult(cmpl1, cmpl2,cmpl3)
struct complex *cmpl1,*cmpl2, *cmpl3;
{
cmpl3->real=cmpl1->real*cmpl2->real - cmpl1->img*cmpl2->img;
cmpl3->img=cmpl1->real*cmpl2->img + cmpl1->img*cmpl2->real;
}
Standard file pointers:
stdin Standard | Input device | keyboard |
stdout Standard | Output device | Monitor |
stdaux Standard |
Auxiliary device | Serial Port |
stdprn Standard | Printing device | Parallel Port |
stderr Standard | Error device | Monitor |
See the chapter Command Line Parameters to learn the way of executing the program with command line parameters.
Instead of writing i=i+1 you can use a short hand assignment operator and write the same statement as i +=1; Similarly i=i-5 can be written as i-=5; This can be used for all the arithmetic operators.
So if we have a statement i += j+2; it is equivalent to i = i+(j+2)
The most usefull operators of C are increment ( ++) and decrement ( -- ). The operator ++ adds 1 to the operand and -- subtracts 1 from the operand.
Prefix and Postfix increment and decrement operators:
In the above example the value of both i and m will be 2.
Postfix operator first assigns the value of the operand to the variable on the left and then increments the operand.
Dta type | Range | Bytes | Format |
---|---|---|---|
signed char or char | -128 to +127 | 1 | %c |
unsigned char | 0 to255 | 1 | %c |
signed int or int | -32768 to +32767 | 2 | %d |
unsigned int | 0 to 65535 | 2 | %u |
signed long int or long int | -2147483648 to +2147483647 | 4 | %ld |
unsigned long int | 0 to 4294967295 | 4 | %lu |
Float | 3.4E-38 to 3.4E+38 | 4 | %f |
Double | 1.7E-308 to 1.7E+308 | 8 | %lf |
long double | 3.4E-4932 to +1.1E+4932 | 10 | %Lf |
Command Line Parameters<< Previous
Our aim is to provide information to the knowledge seekers.
Support us generously