Addition, Subtraction, Division and Multiplication
#include
main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);
return 0;
}
Binary, Octal, hexadecimal
#include
#include
void main()
{
int i,num,*bin,*oct,bin_len,oct_len,hex_len;
char *hex,ch;
clrscr();
do
{
printf("\n Enter the number to be converted :");
scanf("%d",&num);
//printf("\n%c\n",(char)48);
printf("\n");
//binary
bin_len=binary(bin,num);
printf("\nBinary : ");
for(i=0;i
printf("%d",bin[i]);
printf("\n");
//octal
oct_len=octal(oct,num);
printf("\nOctal : ");
for(i=0;i
printf("%d",oct[i]);
printf("\n");
//hexadecimal
hex_len=hexadecimal(hex,num);
printf("\nHexadecimal : ");
for(i=0;i
printf("%c",hex[i]);
printf("\n");
printf("\nEnter another number(y/n)?");
ch=getche();
getch();
}
while(ch!='n');
}
int binary(int *bin,int num)
{
int rem=0,i=0;
do
{
rem=num%2;
num=num/2;
bin[i]=rem;
i++;
}
while(num>0);
array_reverse_i(bin,i);
return i;
}
int octal(int *oct,int num)
{
int rem=0,i=0;
do
{
rem=num%8;
num=num/8;
oct[i]=rem;
i++;
}
while(num>0);
array_reverse_i(oct,i);
return i;
}
int hexadecimal(char *hex,int num)
{
int rem=0,i=0;
do
{
rem=num%16;
num=num/16;
switch (rem)
{
case 10 : hex[i]='A';
i++;
break;
case 11 : hex[i]='B';
i++;
break;
case 12 : hex[i]='C';
i++;
break;
case 13 : hex[i]='D';
i++;
break;
case 14 : hex[i]='E';
i++;
break;
case 15 : hex[i]='F';
i++;
break;
default :hex[i]=(char)(rem+48);
i++;
}
}
while(num>0);
array_reverse_c(hex,i);
return i;
}
array_reverse_i(int *arr,int len)
{
int i,temp;
for(i=0;i
{
temp=arr[i];
arr[i]=arr[len-1-i];
arr[len-1-i]=temp;
}
return 0;
}
array_reverse_c(char *arr,int len)
{
int i;
char temp;
for(i=0;i
{
temp=arr[i];
arr[i]=arr[len-1-i];
arr[len-1-i]=temp;
}
return 0;
}
BINARY SEARCH
#include
#include
void main()
{
int n,i,search,f=0,low,high,mid,a[20];
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the number in ascending order a[%d]=",i);
scanf("%d",&a[i]);
}
printf("Enter the search element:");
scanf("%d",&search);
low=1;
high=n;
while(low<=high)
{
mid=(low+high)/2;
{
high=mid-1;
}
else if(search>a[mid])
{
low=mid+1;
}
else
{
f=1;
printf("obtainedin the position %d:",mid);
getch();
exit();
}
}
if(f==0)
{
printf("not present");
}
getch();
}
CONCATENATE STRING
#include
#include
#include
main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
getch();
return 0;
}
EVEN ODD
#include
#include
void main()
{
int i;
clrscr();
printf("enter no ");
scanf("%d",&i);
if(i%2==0)
printf("no is even");
else
printf("no is not even");
getch();
}
FACTORIAL
//WAP To Print n!.
#include
void Factorial(int);
void main()
{
int num;
clrscr();
printf("Enter a Number");
scanf("%d",&num);
Factorial(num);
getch();
}
void Factorial(int num)
{
int i;
long fact=1;
for(i=1;i<=num;i++)
fact*=i;
printf("\nFactorial of %d is %d",num,fact);
}
FIBONACCI
#include
#include
void main()
{
int n;
long int i;
long int fibo(int n);
clrscr();
printf("Enter the limit:\n");
scanf("%d",&n);
i=fibo(n);
printf("\nThe %dth Fibonacci number is %ld",n,i);
getch();
}
long int fibo(int n)
{
int old_no,currnt_no,sum,i;
i=1;
old_no=0;
currnt_no=1;
while(i<=n)
{
sum=old_no+currnt_no;
old_no=currnt_no;
currnt_no=sum;
i++;
printf("\n%d",sum);
}
return(sum);
}
fprintf() and fscanf() Function
#include
#include
#include
int main(void)
{
FILE *fp;
char s[80];
int t;
if((fp=fopen("test", "w")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
printf("Enter a string and a number: ");
fscanf(stdin, "%s%d", s, &t); /* read from keyboard */
fprintf(fp, "%s %d", s, t); /* write to file */
fclose(fp);
if((fp=fopen("test","r")) == NULL) {
printf("Cannot open file.\n");
exit(1);
}
fscanf(fp, "%s%d", s, &t); /* read from file */
fprintf(stdout, "%s %d", s, t); /* print on screen */
return 0;
}
LEAP YEAR
#include
int main()
{
int year;
int min_year,max_year;
printf("enter lowest year");
scanf("%d", &min_year);
printf("enter highest year");
scanf("%d", &max_year);
printf("leap year in given range");
for( year=min_year; year<=max_year; year++)
{
if (((year%4==0) && (year<=100!=0)) || (year%400==0))
printf("%d",year);
}
return 0;
}
LENGTH OF STRING
#include
int StringLength(char[]);
void main()
{
char n[20];
int length;
clrscr();
printf("Enter a String: ");
gets(n);
length=StringLength(n);
printf("\nLength of String \"%s\": %d",n,length);
getch();
}
int StringLength(char n[])
{
int cnt=0;
while(n[cnt]!='\0')
cnt++;
//cnt=strlen(n);
return cnt;
}
LINKED LIST
Program to demonstrate linked list operations
# include
# include
# include "malloc.h"
struct node
{
int data;
struct node *link;
};
void main()
{
int a=111,b=2,c=3,will,wish,num;
struct node *ptr,*ptr2,*result,*temp;
void add(struct node **,int );
struct node * search(struct node *);
void display(struct node *);
void invert(struct node *);
void del(struct node *,int);
struct node * concat(struct node *,struct node *);
ptr=NULL;
ptr2=NULL;
result=NULL; //result for storing the result of concatenation
clrscr();
will=1;
while(will==1)
{
printf("
Main Menu
1. Add element
2.Delete element
3.Search element
4Linked List concatenation
5.Invert linked list
6. Display elements
Please enter the choice");
scanf("%d",&wish);
switch(wish)
{
case 1:
printf("
Enter the element you want to add ");
scanf("%d",&num);
add(&ptr,num);
display(ptr);
break;
case 2:
printf("
Enter the element to delete ");
scanf("%d",&num);
del(ptr,num);
break;
case 3:
printf("
Now demonstrating search ");
temp = search(ptr);
printf("
Address of first occurence is %u ",temp);
break;
case 4:
/* Inputs given internally for demo only */
printf(" Now demonstrating linked list concatenation
Press any key to continue...");
add(&ptr2,2);
add(&ptr2,4);
add(&ptr2,6);
getch();
printf("
Displaying second Linked List
");
display(ptr2);
getch();
result = concat(ptr,ptr2);
clrscr();
printf("
Now Displaying the result of concatenation");
display(result);
getch();
break;
case 5:
printf("
Inverting the list ...
Press any key to continue...");
invert(ptr);
break;
case 6:
display(ptr);
break;
default:
printf("
Illegal choice
");
}
printf("
DO you want to continue ( press 1 for yes ");
scanf("%d",&will);
} //end of while
}
void add(struct node **q,int num)
{
struct node *temp;
temp = *q;
if(*q==NULL)
{
*q=malloc(sizeof(struct node));
temp = *q;
}
else
{
while((temp->link)!=NULL)
{
temp=temp->link;
}
temp->link = malloc(sizeof(struct node));
temp=temp->link;
}
temp->data = num;
temp->link = NULL;
}
void display(struct node *pt)
{
while(pt!=NULL)
{
printf("
Data : %d",pt->data);
printf("
Link : %d",pt->link);
pt=pt->link;
}
}
void invert(struct node *ptr)
{
struct node *p,*q,*r;
p=ptr;
q=NULL;
while(p!=NULL)
{
r=q;
q=p;
p=p->link;
q->link=r;
}
ptr = q;
display(ptr);
}
// CONCATENATION OF LINKED LISTS
struct node * concat(struct node *p,struct node *q)
{
struct node *x,*r;
if (p==NULL)
r=q;
if (q==NULL)
r=p;
else
{
x=p;
r=x;
while(x->link!=NULL)
x=x->link;
x->link=q;
}
return(r);
}
// SEARCHING AN ELEMENT IN THE LINKED LIST
// THIS FUNCTION FINDS THE FIRST OCCURENCE OF
// A DATA AND RETURNS A POINTER TO ITS ADDRESS
struct node * search(struct node *p)
{
struct node *temp;
int num;
temp = p;
printf("
Enter the data that you want to search ");
scanf("%d",&num);
printf("
Link of temp %u", temp->link);
while(temp->link!=NULL)
{
printf("
In while ");
if(temp->data == num)
return(temp);
temp=temp->link;
}
return(NULL);
}
// DELETING DATA FROM THE LINKED LIST//
void del(struct node *p,int num)
{
struct node *temp,*x;
temp=p;
x= NULL;
while (temp->link !=NULL)
{
if(temp->data == num)
{
if (x==NULL)
{
p = temp->link;
free(temp);
return;
}
else
{
x->link = temp->link;
free(temp);
return;
}
} //end of outer if
x=temp;
temp=temp->link;
} //end of while
printf("
No such entry to delete ");
} //end of fn.
Multiplication table
#include
int main(){
int r,i,j,k;
printf("Enter the number range: ");
scanf("%d",&r);
for(i=1;i<=r;i++){
for(j=1;j<=10;j++)
printf("%d*%d=%d ",i,j,i*j);
printf("\n");
}
return 0;
}
Pascal triangle
#include
long fact(int);
int main(){
int line,i,j;
printf("Enter the no. of lines: ");
scanf("%d",&line);
for(i=0;i
for(j=0;j
printf(" ");
for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
return 0;
}
long fact(int num){
long f=1;
int i=1;
while(i<=num){
f=f*i;
i++;
}
return f;
}
PASCAL TRIANGLE
#include
#include
void main()
{
int i,n,j,k,l;
clrscr();
printf("Enter a value of n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=(40-i);j++)
{
printf(" ");
for(k=1;k<=i;k++)
{
printf("%d",k);
}
for(l=80;l<=(i-1);i--)
{
printf("%d",l);
}
}
printf("\n");
}
getch();
}
PRIME NUMBER
#include
#include
#include
void main()
{
int a,i,r;
clrscr();
printf("enter num.");
scanf("%d",&a);
i=2;
SMALLEST ELEMENT IN ARRAY
#include
main()
{
int array[100], *minimum, size, c, location = 1;
printf("Enter the number of elements in array\n");
scanf("%d",&size);
printf("Enter %d integers\n", size);
for ( c = 0 ; c < size ; c++ )
scanf("%d", &array[c]);
minimum = array;
*minimum = *array;
for ( c = 1 ; c < size ; c++ )
{
if ( *(array+c) < *minimum )
{
*minimum = *(array+c);
location = c+1;
}
}
printf("Minimum element is present at location number %d and it's value is %d.\n", location, *minimum);
return 0;
}
SUM OF FACTORIAL
#include
#include
long int calfact(int x);
void main()
{
int i,x,limit;
float sum=0;
char c;
clrscr();
repeat :
printf("enter the limit:\n");
scanf("%d",&limit);
for(i=1;i<=limit;i++)
{
sum=sum+((float)i/calfact(i));
printf("\nsum of %d is %f\n",i,sum);
}
printf("\nfinally the sum of %d is %f",limit,sum);
fflush(stdin);
printf("\n\nDo you wish to Continue(y/n)");
scanf("%c",&c);
if(c=='y'||c=='Y')
goto repeat;
getch();
}
long int calfact(int x)
{
if(x<=1)
{
return(1);
}
else
{
x=x*calfact(x-1);
return(x);
}
}
No comments:
Post a Comment
You can ask your queries here and we will try to get back to you as soon as we will be done working with that..
Note: Only a member of this blog may post a comment.