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

How to make parent process wait till Completion of Child Process (Joining parent with child process)

How to make the parent process wait till the completion of execution of child process. The following C program makes the parent process to wait till the completion of its child process.

#include<stdio.h>
main()
{

How to make Child process an Orphan Process

C program in Linux or Unix to make a child process orphan:

Making child as orphan

#include<stdio.h>
main()
{

C Program to Display SCAN and ASCII Codes of Keys on Keyboard Using 8086 Interrupt

output of C program source code to find scan code and ASCII code of any key pressed on keyboard asci ascii A 65 97 32 30 keyboard code using 8086 interrupt 16 0x16 16h INT16 int86 int86x
Output (ASCII and SCAN code) when a is pressed
This program displays SCAN code and ASCII code of any key pressed on the keyboard. We use inregs is made zero in the program. Then interrupt 16h is called using int86() function in dos.h. The arguments are interrupt number (in hexadecimal ; in C 0x16) pointer to input register and pointer to output register.

C and C++ Programs to Reverse a Number

This post includes a C program and a C++ program to reverse a given number. That is, to reverse the order of digits. If 543 is entered, it will be displayed as 345.

C Program:

#include<stdio.h>
main()
{

Conditional Operator in C and C++

Conditional Operator is an operator which is substitutive for if-else statements. It is a ternary operator (operator which operates on 3 operands). It is often called ?: operator. The operands are expression1, expression2 and expression3. The syntax is as follows:

expression1 ? expression2 : expression3

The expression expression1  will be evaluated always. Execution of expression2 and expression3 depends on the outcome of expression1. expression1 is checked whether true or not. It is considered like a boolean variable. The outcome of an expression is true if it has a non zero value. If its value is zero, the outcome of expression is false. If the expression1 is true, then expression2 is evaluated. If the expression1 is false, then expression3 is evaluated. Consider the following example:

Linear Search in C and C++ Languages

Linear search simply means 'looking for an element in the whole array from one end to the other'. The searched element is compared with the first element of array first, then with the second, then with the third, so on upto the end of the array. This is time consuming when the array is pretty enough. The time complexity is highest.The C and C++ programs for linear search is given below.

C Program:

#include<stdio.h>

void main()
{

C and C++ Programs to Check Whether a Number is Strong Number or Not

This post contains C and C++ program to check whether a given number is Strong number or not. A strong number is a number for which the sum of factorials of its digits is equal to the number itself. The first one is C program to check whether the input number is a strong number or not. An example is 145. 1!+4!+5!=1+24+120=145 Therefore 145 is a strong number.

C Program:

#include<stdio.h>
long int factorial(int n)

{

C Program for Subtraction Without Using Minus Sign

The following is a program to subtract a number from another without using minus symbol. Instead of doing a normal subtract operation, the two's complement of the subtrahend is added to the minuend. (In 3-4, 3 is minuend and 4 is subtrahend.) In other words, the 1's complement and 1 is added to minuend.

#include<stdio.h>
void main()
{
int a,b,sum;
printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
sum = a + ~b + 1;
printf("Difference of two integers: %d",sum);
}


C and C++ Programs to Find Sum of Digits of a Given Number

The following are C and C++ programs to find sum of digits of any given integer.

C Program

#include<stdio.h>
#include<conio.h>
void main()
{

C and C++ Program for Swapping Two Numbers Without Third Variable

This post contains C and C ++ programs for swapping two given numbers without using any temporary variable. Only the two variables are required, not a third.

C Program
#include<stdio.h>
void main()
{

C and C++ swapping numbers

The following are C and C++ programs to swap the values of two variables using a third variable.

C Program:
#include<stdio.h>
void main()
{
int x,y,temp;
printf("Enter the value of xand y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\n x= %d\n y = %d\n",x,y);
temp = x;
x= y;
y = temp;
printf("After Swapping\n x= %d\ny= %d\n",x,y);
}

C and C++ Programs to Check Whether a Given Number is Armstrong Number or Not

This post contains two programs, a C++ program and a C program. These are programs to check whether a given number is an Armstrong number or not. An Armstrong number is a positive integer for which the sum of 'n'th power of all of its digits is equal to the number itself. For example, 153 is a 3 digit Armstrong number because 13+53+33=153.

C ++ program:

# include <iostream.h>

# include <conio.h>

# include <math.h>

void main ()

{

clrscr();

int a,b=0,sum=0;

long int n;

cout<<"Enter the number to be checked\n ";

cin>>n;

if(n<1)
{
cout<<"\nThe number should be greater than 0";
}
else
{
a=n;
//counting the digits
while (a>0) 
{
a=a/10; 
b++;
}
a=n;
//adding up bth power of digits
while(a>0)
{
sum=sum+pow(( a%10) ,b);
a=a/10;
}
if(sum==n)
cout<<"\nThe number is an ARMSTRONG number";
else
cout<<"\nThe number is NOT an ARMSTRONG number";
}
getch();
}


C Program:
# include <stdio.h>

# include <conio.h>

# include <math.h>

void main ()

{

clrscr();

int a,b=0,sum=0;

long int n;

printf("Enter the number to check\n ");

scanf("%i",&n);
if(n<1)
{
printf ("\nThe number should be greater than 0");
}
else
{
a=n;
//counting the digits
while (a>0) 
{
a=a/10; 
b++;
}
a=n;
//adding up bth power of digits
while(a>0)
{
sum=sum+pow(( a%10) ,b);
a=a/10;
}
if(sum==n)
printf ("\nThe number is an ARMSTRONG number");
else
printf ("\nThe number is NOT an ARMSTRONG number");
}
getch();
} 

C Program to Check Whether a Given String is Palindrome or Not

Here i am writing two different programs two check whether an input string is palindrome or not. The first program checks the string manually character by chatter from both ends. The other program make use of a string function strrev() (under string.h) to reverse the string. Therefore, the second program is more compact.

Program 1:

#include<stdio.h>

#include<string.h>

void main()

{

char a[20];

int i, len;

printf("\nEnter the string:\n");

gets(a);

len = strlen(a);

for (i = 0; i < len / 2; i++)

{

if (a[i] != a[len - i - 1])

break;

}

Sizeof operator in C

Sizeof operator is a unary operator used in both C and C++ programming languages. This operator is used to get the size of a data type, variable, object of a class, structure variable etc in bytes. In some programs it may be necessary to use the size of certain variable or data type. Such situations often occur in file handling programs. But the size of data type may vary from machine to machine. In most of the 32 bit systems, int has a size of 4 bytes. But in some systems it is 2 bytes. So, if a program is written by assuming the size to be known, it will affect the portability of the program since a change in size of a data type may result in unexpected output. In such cases, the sizeof operator helps to obtain the size of data type or variable in bytes. The argument of sizeof can be a variable of primitive days types (int, char etc), an object (of class), a structure variable, union variable, pointer variables etc. The sizeof operator is very useful in dynamic memory allocation.

C program to show working of sizeof operator:

#include<stdio.h>

struct student

{

char name[5];

int roll;

int mysize;

};

main()
{
unsigned long int b;
printf("size of variable b: %d",sizeof b);
printf("\nsize of int:%d",sizeof(int));
printf("\nsize of float:%d",sizeof(float));
printf("\nsize of char:%d",sizeof(char));
printf("\nsize of long:%d",sizeof(long));
printf("\nsize of double:%d",sizeof(double));
printf("\nsize of short:%d",sizeof(short));
struct student s1;
s1.mysize=sizeof(s1.name)+sizeof(s1.roll)+sizeof(s1.mysize);
printf("\nSizeof structure student :%d",s1.mysize);
/*see the difference shown in size of structure */
printf("\nsize of structure student : %d",sizeof(struct student));
}

Output:

C Program to Calculate Determinant of Matrix of Order Upto 3

In this post i am sharing a C program to calculate determinant of matrices of order upto 3 (3x3).

#include<stdio.h>


void main()

{int a[10][10],i,m,j;

int det=0; 

printf("\nEnter the order of matrix:\n "); 

scanf("%i",&m);

printf("\nenter elements\n");

for(i=0;i<m;i++)

{
for(j=0;j<m;j++)
scanf("%d",&a[i][j]); }

printf("\nThe matrix is\n");

for(i=0;i<m;i++)

{
printf("\n");

for(j=0;j<m;j++)
printf("%3d",a[i][j]);

}

printf("\nthe transpose is\n");

for(i=0;i<m;i++)
{
for(j=0;j<m;j++)

printf("%3i",a[j][i]);

printf("\n");

}

if(m==1)

{
det=a[0][0];

printf("det=%i",det);

}