C Language Tutorial for Beginners

C Language Tutorial for Beginners

  1. WAP in C to perform addition, subtraction, multiplication and division of two numbers.
  2. WAP in C to Swap Two Numbers using Third variable.
  3. WAP in C to check whether a given number is even or odd.
  4. WAP in C to find whether a given year is a leap year or not.
  5. WAP in C to find factorial of any number entered through the keyboard using while loop.
  6. WAP in C to print tables from 1 to 20 using for loop.
  7. WAP in C to print matrix addition and multiplication of two dimension array.
  8. WAP in C to print insert and delete number at particular position in single dimension array.
  9. WAP in C to find the maximum number between two numbers using a pointer.
  10. WAP in C to swap elements using call by reference and call by value.
  11. WAP in C to Print Fibonacci Series using recursion.
  12. WAP in C to print individual characters of string in reverse order.
  13. WAP in C to define a Structure called Cricket that will describe the following information:-

  • Player Name, Team Name, Batting Average  Using a player Array with 5 records and print the result.
  1. WAP in C to calculate the total execution time of a program.

1. WAP in C to perform addition, subtraction, multiplication and division of two numbers.

Example: 1

/* This is Comment Line */
#include<stdio.h>
#include<conio.h>
void main()
{
	int a=50,b=10,c=0,d,e=0,f=0;
	c=a+b;
	d=a-b;
	e=a*b;
	f=a/b;
	/* For Clear The Screen */
	/* system("cls") or clrscr(); */
	  system("cls");
	printf("Addition :- %d\n",c);
	printf("Subtraction :- %d\n",d);
	printf("Multiplication :- %d\n",e);
	printf("Division :- %d\n",f);
	getch();
}

Example: 2

/* This is Comment Line */
#include<stdio.h>
#include<conio.h>
void main()
{
	int a,b,c,d,e,f;
	printf("Enter The Value of a:-");
	scanf("%d",&a);
	printf("Enter The Value of b:-");
	scanf("%d",&b);
	c=a+b;
	d=a-b;
	e=a*b;
	f=a/b;
	/* For Clear The Screen */
	/* system("cls") or clrscr(); */
	  system("cls");
	printf("Addition :- %d\n",c);
	printf("Subtraction :- %d\n",d);
	printf("Multiplication :- %d\n",e);
	printf("Division :- %d\n",f);
	getch();
}

2. WAP in C to Swap Two Numbers using Third variable.

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,temp;
clrscr();
printf("Enter Two Number :");
scanf("%d%d",&a,&b);
temp=a;
a=b;
b=temp;
printf("\nSwap Value of a=%d ",a);
printf("\nSwap Value of b=%d ",b);
getch();
}

3. WAP in C to check whether a given number is even or odd.

Example: 1

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter any Number :");
scanf("%d",&a);
if(a%2==0)
{
printf("\Number is Even");
}
else
{
printf("\nNumber is Odd");
}
getch();
}

Example: 2

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
int num[20],odd=0,even=0,n;
clrscr();
printf("Enter the size of Array");
scanf("%d",&n);
printf("\n");
	for(i=0;i<n;i++)
	{
	printf("Enter the  Array Elements");
	scanf("%d",&num[i]);
	}
	for(i=0;i<n;i++)
	{
	if(num[i]%2==0)
	  {
		even++;
	  }
	else
	  {
		odd++;
	  }
	}
printf("\n The Number of even  in the array :%d",even);
printf("\n The Number of odd   in the array :%d",odd);
getch();
}

4.WAP in C to find whether a given year is a leap year or not.

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter any Year :");
scanf("%d",&year);
if(year%4==0)
{
printf("\nYear is Leap Year");
}
else
{
printf("\nYear is not Lear Year");
}
getch();
}

5.WAP in C to find factorial of any number entered through the keyboard using while loop.

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int num,fact=1,i=1;
clrscr();
printf("Enter any  Digit:");
scanf("%d",&num);
while(i<=num)
{
fact=fact*i;
i++;
}
printf("\n The Factorial of the given no. is=%d",fact);
getch();
}

6.WAP in C to print tables from 1 to 20 using for loop.

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k;
   clrscr();
   for(i=1;i<=20;i++)
   {
   		for(j=1;j<=10;j++)
   		{
   		k=i*j;
                /*Values will be padded with 3 spaces*/
		printf("%3d ",k); 	
		}
   /*After printing table of each number*/
   printf("\n");
   }
getch();
}

7.WAP in C to print matrix addition and multiplication of two dimension array.

Example: 1

/* Addition of Two Matrix */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,x[3][3],y[3][3],z[3][3];
clrscr();
printf("First Matrix\n");
	for(i=0;i<3;i++)
		{
		for(j=0;j<3;j++)
		   {
		   scanf("%d",&x[i][j]);
		   }
		}
	 printf("\n\n Next Matrix\n");
	 for(i=0;i<3;i++)
		{
		for(j=0;j<3;j++)
			{
			scanf("%d",&y[i][j]);
			}
		}
		for(i=0;i<3;i++)
		  {
			for(j=0;j<3;j++)
			    {
			    z[i][j]=x[i][j]+y[i][j];
			    printf("%d   ",z[i][j]);
			    }
			printf("\n");
		  }
}

Example: 2

/*Program For Multply Matrix */
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,k,d=0;

	clrscr();
        for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
		printf("Type Elemet Ist Matrix ");
		scanf("%d",&a[i][j]);
		}
	}

        for(i=0;i<3;i++)
	{
		for(j=0;j<3;j++)
		{
		printf("Type Elemet IInd Matrix ");
		scanf("%d",&b[i][j]);
		}
	}

	for(k=0;k<3;k++)
	{
		for(i=0;i<3;i++)
		{
			for(j=0;j<3;j++)
			{
			d=d+(a[k][j]*b[j][i]);
			}
			printf("%d\t",d);
			d=0;
		}

		printf("\n");
	}
getch();
}

8.WAP in C to print insert and delete number at particular position in single dimension array.

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,c,a,value,position,array[50];
clrscr();
printf("Enter the number of elements in array");
scanf("%d",&n);
printf("Enter the %d elements\n",n);
for(c=0;c<n;c++)
scanf("%d",&array[c]);
printf("1.Insertion\t2.Deletion");
scanf("%d",&a);
if(a==1)
{
printf("Enter the location where you wish to insert element");
scanf("%d",&position);
printf("Enter the value to insert");
scanf("%d",&value);
for(c=n-1;c>=position-1;c--)
array[c+1]=array[c];
array[position-1]=value;
printf("Array is ");
for(c=0;c<=n;c++)
printf("%d\t",array[c]);
getch();
}
else
{
printf("Enter the location where you wish to delete element");
scanf("%d",&position);
if(position>=n+1)
printf("Deletion not possible");
for(c=position-1;c<n;c++)
array[c]=array[c+1];
printf("Array after delition is  ");
for(c=0;c<n-1;c++)
printf("%d\t",array[c]);
getch();
}
}
Output:
Enter the number of elements in array
5
Enter the 5 elements
1
2
3
4
5
1. Interstation	2.Deletion  2
Enter the location where you wish to delete element 3
Array after deletion is   1	  2	 4	5

9. WAP in C to find the maximum number between two numbers using a pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b, *l, *aptr,*bptr;
clrscr();
printf("Enter The Value of a and b :");
scanf("%d%d",&a,&b);
aptr=&a;
bptr=&b;
if(*aptr>*bptr)
l=aptr;
else
l=bptr;
printf("The Largest Nmuber is : %d",*l);
getch();
}

Output:
Enter the Value of a and b  : 25 24
The Largest number is : 25

10.WAP in C to swap elements using call by reference and call by value.

Example: 1

/* Call By Value */
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int a,b;
clrscr();
printf("Enter Two Number :");
scanf("%d%d",&a,&b);
swap(a,b);
}
void swap(int x, int y)
{
int c;
c=x;
x=y;
y=c;
printf("a=%d\n",x);
printf("b=%d\n",y);
}

Example: 2

/* Call By Reference */
#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int a,b;
clrscr();
printf("Enter Two Number :");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("\na=%d",a);
printf("\nb=%d",b);
}
void swap(int *x, int *y)
{
int c;
c=*x;
*x=*y;
*y=c;
}

11.WAP in C to Print Fibonacci Series using recursion.

Example:

#include<stdio.h>
#include<conio.h>
void main()
{
int count,n;
long int fibonacci(int count);
clrscr();
printf("How Many Fibonacci number ? ");
scanf("%d",&n);
printf("\n");
for (count=1;count<=n;++count)
printf("\ni=  =%2d  F=%ld",count,fibonacci(count));
getch();
}
long int fibonacci(int count)
/*Calculate a Fibonacci number using the formulas
  F=1 for i<3, and F=f1+f2 for i>=3 */
{
static long int f1=1, f2=1;
long int f;
f=(count<3) ? 1 : f1 + f2;
f2=f1;
f1=f;
return(f);
}

12.WAP in C to print individual characters of string in reverse order.

Example: 1

#include <stdio.h>
#include <string.h>
void main()
{
   char ch[100];
   printf("Enter any  String");
   gets(ch);
   strrev(ch);
   printf("Reverse Order String: %s\n", ch);
}

Example: 2

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char str[50],revstr[50];
int i=0,j=0;
clrscr();
printf("Enter the String to be reversed :");
scanf("%s",str);
for(i=strlen(str)-1;i>=0;i--)
{
revstr[j]=str[i];
j++;
}
revstr[j]='\0';
printf("Input String : %s",str);
printf("\nOutput String : %s",revstr);
getch();
}

13.WAP in C to define a Structure called Cricket that will describe the following information:-
Player Name, Team Name, Batting Average Using a player Array with 5 records and print the result.
Example:

#include<stdio.h>
#include<conio.h>
void main()
{
struct cricket
{
char playername[20];
char teamname[20];
int avg;
} information[10];
int j;
clrscr();
	for(j=0;j<5;j++)
	{
	printf("Enter the record %d\n",j+1);
	printf("Player Name ");
	scanf("%s",information[j].playername);
	printf("Team Name ");
	scanf("%s",information[j].teamname);
	printf("Average");
	scanf("%d",&information[j].avg);
	}
		printf("Player Name\tTeam Name\tBatting Average\n");
		for(j=0;j<5;j++)
		{
		printf("%s\t\t",information[j].playername);
		printf("%s\t\t",information[j].teamname);
		printf("%d\n",information[j].avg);
		}
}

14.WAP in C to calculate the total execution time of a program.

Example:

#include<stdio.h>
#include<time.h>
void main() 
{
	int j;
	double t_time;
	clock_t start, end;
	start = clock();
	//time count starts 
	srand(time(NULL));
	for (j = 0; j < 20000; j++) 
	{
		printf("Start Time[%d]= %d\n", j + 1, rand());
	}
	end = clock();
	//time count stops 
	t_time = ((double) (end - start)) / CLK_TCK;
	//calulate total time
	printf("\nTotal Execution Time of a Program: %f", t_time);
	
}


C Language E-Book


Basic C Programming
Advance C Programming Part 1
Advance C Programming Part 2
CBNST
About me