Search This Blog

C Program to Find Sum of Digits of a Given Number

The following C program calculates sum of digits of any given integer.

C Program

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

clrscr();
int n,sum=0;
printf("Enter a number");
scanf("%d",&n);
while(n>0)
{
sum=sum+n%10; //adding last digit
n=n/10; //removing last digit
}
printf("Sum of digits of the number = %d",sum);
getch();
}

C Program to Swap Two Numbers Without Third Variable

This post contains C program to swap two given numbers without using any temporary variable. Only the two variables are required, not a third.

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

int a,b;
printf("Enter first number, a:");
scanf("%d",&a);
printf("Enter second number, b:");
scanf("%d",&b);
printf("values of a and b before swapping is a=%d,b=%d",a,b);
/*swapping*/
a=a+b;
b=a-b;
a=a-b;
printf("value of a and b after swapping is a=%d,b=%d"a,b);
}


C Program to Sort Strings using Selection Sorting

Here i am writing programs in c and c++ languages to sort a given number of strings. Sorting is carried out based on the ASCII values of the letters in the strings. The string comparing function strcmp() is used in the program. These programs sort a given set of strings in ascending order.To change it into descending order just replace > with < in comparison line. The comparison line in the code is marked by comment.

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

int i,j,n;
char str[20][20],temp[20];
puts("\nEnter the number of strings to be sorted\n");
scanf("%d",&n);
printf("\nEnter the strings one by one\n");
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0) /* Comparison line */
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
printf("\nThe sorted strings:\n");
for(i=0;i<=n;i++)
puts(str[i]);
getch();
}

C++ program:


#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
char str[20][20],temp[20];
cout<<"\nEnter the number of strings to be sorted\n";
cin>>n;
cout<<"\nEnter the strings one by one\n";
for(i=0;i<n;i++)
gets(str[i]);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j])>0) /* Comparison line */
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
cout<<"\nThe sorted strings:\n";
for(i=0;i<n;i++)
puts(str[i]);
getch();
}

C Program to Reverse a Number - Reverse Digits of a Number using C Program

This post includes 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()
{
int n, reverse = 0;
printf("Enter a number to reverse:\n");
scanf("%d",&n);
while (n != 0)
    {
    reverse = reverse * 10;
    reverse = reverse + n%10;
n = n/10;
    }
printf("Reverse of entered number is = %d\n", reverse);
}

C program to Find Index of a Substring in a String

Here, we will see a function written in C language, which helps to find the index (starting index) of any substring in a given string. The function returns the index of first letter of the first occurrence substring inside the original string. The function in C language is as follows:


int indexof(char *subString,int fromIndex,char *MainString)
{
 int Mainlength,subLength,i,j,retIndex=-1;
 Mainlength=strlen(MainString);
 subLength=strlen(subString);
 if(Mainlength<1||subLength<1||Mainlength-fromIndex<subLength)
  return(-1);
 for(i=fromIndex;Mainlength-i>=subLength;i++)
  {
  if(*(MainString+i)==*(subString))
   {
   retIndex=i;
   for(j=0;j<subLength;j++)
    {
    if(*(MainString+i+j)!=*(subString+j))
     {
     retIndex=-1;
     break;
     }
    }
   if(retIndex!=-1)
    return retIndex;
   }
  }
 return (-1);
}

The function returns -1, if the substring cannot be found within the main string.

Caterpillar Eating Leaf - Computer Graphics Lab Animation in C

This is a CG lab program in C using graphics.h library to animate a caterpillar eating a leaf. As it eats, the caterpillar (or worm) should grow proportionally in size. The leaf should also be reduced in size. The program is given below:
 The ouput is:


DDA Line Drawing Program in C : Computer Graphics Lab Program

Digital Differential Analyzer Algorithm or DDA algorithm is the simplest line drawing algorithm. The algorithm is taught in computer graphics subjects. The algorithm is implemented in CG labs (computer graphics labs). Borland Graphics Interface (BGI) in C is commonly used for implementing computer graphics algorithms in computer graphics labs. Here i am adding c program to draw any line with any slope using the DDA line drawing algorithm. The DDA line drawing program is as follows:

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>

void dda(int x1,int y1,int x2,int y2)
{
float xincr,yincr,x,y,dx,dy,s,i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
	s=abs(dx);
else
	s=abs(dy);
xincr=dx/s;
yincr=dy/s;
x=x1;
y=y1;
for(i=0;i<s;i++)
	{
	putpixel(x,y,2);
	x+=xincr;
	y+=yincr;
	}
}

void main()
{
int gd=DETECT,gm,theta=0;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI\\");
while(!kbhit())
	{
	theta=(theta+1)%361;
	dda(300,150,300+50*sin(3.14f*theta/180),150+50*cos(3.14f*theta/180));
	delay(50);
	cleardevice();
} }


In this program, the dda line drawing algorithm is implemented as a function named dda which takes the co-ordinates of the two end points as parameters. In the main program, i have written a code which tests the DDA algorithm by drawing lines of almost all slopes.