C Language - Programming
Sunday, 13 June 2021
Programs in turboc3 : Files
Thursday, 27 May 2021
Programs in turboc3: Functions - Types of Functions
{
Body of the loop
}
Characteristics of a function :
1.Function declaration or proto type
2.Calling function sum();
3.Called function can be divided into 4 categories:
(i) Function with no return and no argument
(ii) Function with no return and with argument
(iii) Function with return and with no argument
(iv) Function with return and with argument
2. The parameters which are used under called functions are called Dummy parameters.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void sum(void); /*Function Declaration or Proto Type*/
void main()
{
clrscr();
sum(); /*Calling Function*/
getch();
}
Void sum(void) /*called function*/
{
int a,b,c;
printf(“enter a,b values:”);
scanf(“%d%d”,&a,&b);
c=a+b;
printf(“sum is %d”,c);
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void mul(void); /*Function Declaration or Proto Type*/
{
clrscr();
mul(); /*calling function*/
getch();
}
void mul(void) /*called function*/
{
int a,b,c;
printf(“enter a,b values:”);
scanf(“%d%d”,&a,&b);
c=a*b;
printf(“multiplication is %d”,c);
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void sum(int,int); /*Function Declaration or Proto Type*/
void main()
{
int a,b;
clrscr();
printf(“enter a,b values:”);
scanf(“%d%d”,&a,&b);
sum(a,b); /*calling function*/ // actual parameters
getch();
}
{
int r;
r=p+q;
printf(“sum is %d”,r);
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
void mul(int,int); /*Function Declaration or Proto Type*/
void main()
{
int a,b;
clrscr();
printf(“enter a,b values:”);
scanf(“%d%d “,&a,&b);
mul(a,b); /*calling function*/
getch();
}
void mul(int p,intq) /*called function*/
{
int r;
r=p*q;
printf(“multiplication is %d”,r);
}
/* Function with return and with no argument*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
int sum(void); /*Function Declaration or Proto Type*/
void main()
{
int c;
clrscr();
c=sum(); /*calling function*/
printf(“sum is %d”,c);
getch();
}
int sum(void) /* called function*/
{
int p,q,r;
printf(“enter p,q values:”);
scanf(“%d%d”,&p,&q);
r=p+q;
return r;
}
#include<conio.h>
#include<string.h>
#include<math.h>
int mul(void); /*Function Declaration or Proto Type*/
void main()
{
int c;
clrscr();
c=mul(); /*calling function*/
printf(“multiplication is %d”,c);
getch();
}
{
int p,q,r;
printf(“enter p,q values”);
scanf(“%d%d”,&p,&q);
r=p*q;
return r;
}
output:
#include<conio.h>
#include<math.h>
#include<string.h>
int sum(int,int); /*Function Declaration or Proto Type*/
void main()
{
int a,b,c;
clrscr();
printf(“enter a,b values”);
scanf(“%d%d”,&a,&b);
c=sum(a,b); /*calling function*/ // actual parameters
printf(“sum is %d “,c);
getch();
}
int sum (int p,int q) /*called function*/ // dummy parameters
{
int z;
z=p+q;
return z;
}
#include<conio.h>
#include<string.h>
#include<math.h>
int mul(int,int); /*Function Declaration or Proto Type*/
void main()
{
int a,b,c;
clrscr();
printf(“enter a,b values”);
scanf(“%d%d”,&a,&b);
c=mul(a,b);
printf(“multiplication is %d “,c);
getch();
}
int mul(int p,int q)
{
int z;
z=p+q;
return z;
}
Tuesday, 25 May 2021
Programs in turboc3: Recursions - factorial,multiplication,power,GCD,Fibonacci,Towers of hanoi
/*Factorial of a number 'n' i.e below 8 using recursion */
#include<conio.h>
#include<string.h>
#include<math.h>
int fact(int n); /* function declaration */
void main()
{
int n,f;
clrscr();
printf(“enter any value:”);
scanf(“%d”,&n);
f=fact(n); /* calling function */
printf(“\n factorial of %d is %d”,n,f);
getch();
}
{
int f;
if(n==1)
return 1;
f=n*fact(n-1);
return (f);
}
Output:
#include<conio.h>
#include<string.h>
#include <math.h>
int mult (int,int) /* function declaration */
void main()
{
int a,b,c;
clrscr();
printf(“enter any two numbers:”);
scanf(“%d%d”,&a,&b);
c=mult(a,b); /* calling function */
printf(“multiplication of %d and %d is %d”,a,b,c);
getch();
}
{
if(b==1)
return a;
c=b+mult(a,b-1);
return (c);
}
Output:
/*Power of two numbers using recursion*/
#include<conio.h>
#include<string.h>
#include<math.h>
int a,b,c;
int pow(int,int);
void main()
{
int c;
clrscr();
printf(“enter any two numbers:”);
scanf(“%d%d,&a,&b);
c=pow(a,b);
printf(“\n power of %d and %d is %d”,a,b,c);
getch();
}
{
int c;
if(b==0)
return 1;
c=a*pow(a,b-1);
return (c);
}
Output:
/*GCD of two numbers using recursion*/
/* Fabinacci series using recursion*/
Output:
Friday, 14 May 2021
Programs in turboc3 : Strong ,Armstrong,Perfect ,Magic, Palindrome Number including min and max ranges &Reverse,Fibonacci series
/ * Strong Number * /
Definition :A number is called strong number if sum of it's factorials of it's digits is equal to number itself.
Example : 145
= 1! + 4! + 5!
= 1+24+120
=145
#include<stdio.h>
#include<conio.h>
void main( )
{
int n, i, f, r, sum=0, tmp;
clrscr( ) ;
printf(" Enter any number ") ;
scanf("%d", &n) ;
tmp=n;
while(n) or while (n! =0)
{
i=1 ;
f=1 ;
r=n%10 ;
while (i<=r)
{
f=f * i ;
i ++ ;
}
sum=sum+f ;
n=n/10 ;
}
if(sum==tmp)
printf("\n%d is Strong number ", tmp) ;
else
printf(“\n%d is not a strong number", tmp) ;
getch ( );
}
/ * Strong number range in between min &max */
#include<conio.h>
void main( )
{
long int min, max, n, i, f, r, sum,tmp;
clrscr( ) ;
printf(" \nEnter minimum number ") ;
scanf("%ld",&min) ;
printf("\n Enter maximum number ") ;
scanf("%ld",&max) ;
printf("\nStrong numbers are:") ;
for(n=min;n<=max;n++)
{
tmp=n;
sum=0;
while (tmp)
{
i=1 ;
f=1 ;
r=tmp%10 ;
while (i<=r)
{
f=f * i ;
i ++ ;
}
sum=sum+f ;
tmp=tmp/10 ;
}
if(sum==n)
printf("\n%ld is Strong number", n);
}
getch ( );
}
Output:
1 2 145 40585
/* Armstrong Number */
Definition:- Those number whose sum of its digits to power of number of its digits is equal to that number is known as Armstrong number.
For example : 153, 370, 371
Total Digits in 153 is 3
And 13 +53+33 =1+125+27=153
For example : 1634
Total Digits in 1634 is 4
And 14+64+34+44=1+1296+81+256 =1634
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,r, sum=0,tmp ;
clrscr( ) ;
printf(“Enter any number:”) ;
scanf(“%d”,&n) ;
tmp=n ;
while(n)
{
r=n%10 ;
n=n/10 ;
sum=sum+(r*r*r*) ;
}
if(sum==tmp)
printf(“\n%d is an Armstrong number”,tmp) ;
else
printf(“\n%d is not an Armstrong number”,tmp) ;
getch( ) ;
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int min,max,n,sum=0,r,tmp ;
clrscr( ) ;
printf(“\nEnter min number:”) ;
scanf(“%d”,&min) ;
printf(“\nEnter max number:”) ;
scanf(“%d”,&max) ;
printf(“\n Armstrong number is the given range :”) ;
for( n=min ; n<=max ; n++)
{
tmp=n ;
sum=0 ;
while(tmp!=0)
{
r=tmp%10 ;
tmp=tmp/10 ;
sum=sum+(r*r*r) ;
}
if(sum==n)
printf(“\n%d is Armstrong number”,n) ;
}
getch( ) ;
}
/*Perfect Number*/
Definition:-Perfect number is a positive number whose sum of all positive divisors excluding that number is equal to that number is known as Perfect number.
For example: 6 is a perfect number , since divisor of 6 are 1,2 and 3. Sum of it's divisor is 1+2+3= 6
28=> 1,2,4,7,14 1+2+4+7+14 = 28
496,
8128.
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,i=1,sum=0,tmp ;
clrscr( ) ;
printf(“Enter any number:”) ;
scanf(“%d”,&n) ;
tmp=n ;
while(i<n)
{
if (n%i ==0)
sum=sum+i ;
i++;
}
if(sum==tmp)
printf(“\n%d is an Perfect number”,tmp);
else
printf(“\n%d is not an Perfect number”,tmp);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int min,max,n,sum=0,i,tmp ;
clrscr() ;
printf(“\nEnter min number:”) ;
scanf(“%d”,&min) ;
printf(“\nEnter max number:”) ;
scanf(“%d”,&max) ;
printf(“\nPerfect number is the given range:”) ;
for(n=min;n<=max;n++)
{
i=1 ;
sum=0 ;
while(i<n)
{
if(n%i==0)
sum =sum+i ;
i++ ;
}
if (sum ==n)
printf("\n%d is Perfect number”, n) ;
}
getch ( ) ;
}
/*Prime number*/
Definition:- A natural number greater than one has not any other divisors except 1 and itself.In other word we can say which has only two divisors 1 and number itself.
For example:- 5 divisiors are 1 and 5.
#include<conio.h>
void main( )
{
int n,i=1,count=0 ;
clrscr( ) ;
printf("Enter any number") ;
scanf("%d",&n) ;
while(i <= n)
{
if( (n%i==0) && (n%n==0) )
count++ ;
i++ ;
}
if(count==2)
printf("\n%d is a Prime number",n) ;
else
printf("%d is not a prime number",n) ;
getch( ) ;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int min,max,n,i,count;
clrscr( );
printf("\nEnter min number");
scanf("%d",&min);
printf("\nEnter max number");
scanf("%d",&max);
printf("\nPrime number in the given range:");
for(n=min ; n<=max ; n++)
{
i=1;
count=0;
while(i<=n)
{
if( (n%i==0) && (n%n==0) )
count++;
i++;
}
if(count == 2)
pf("\n%d is Prime number”,n);
}
getch( );
}
/*Magic number*/
Definition:-A number is said to be a magic number if the reverse of the square of the number is equal to the given number reverse and its square.
#include<stdio.h>
#include<conio.h>
void main ()
{
int , i ,j,k,n ;
clrscr( ) ;
printf(“Enter any number”) ;
scanf(“%d”, &n) ;
while (i>n)
{
i=n*n ;
j=0 ;
while(i>0)
{
j=j * 10 + i % 10 ;
i = i / 10 ;
}
i=n ;
k=0 ;
while(i>0)
{
k=k*10+i%10 ;
i=i/10 ;
}
if(k*k==j)
printf (“\nMagic number”) ;
else
printf (“\nNot a Magic number” ) ;
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main ( )
{
int i,j,k,n,min,max ;
clrsccr() ;
printf (“\nEnter min number “) ;
scanf(“%d”,&min );
printf (“\nEnter max number” ) ;
scanf(“%d”,&max) ;
for (n=min; n<=max ; n++)
{
i=n*n ;
j=0 ;
while(i>0)
{
j=j * 10 + i % 10 ;
i = i / 10 ;
}
i=n ;
k=0 ;
while(i>0)
{
k=k*10+i%10 ;
i=i/10 ;
}
if (k * k == j)
printf (“\n%d is a Magic number” , n );
}
getch() ;
}
/*Palindrome number */
Definition: A number is caller palindrome number if it remain some when it's digits are reversed
For Example : 121 is palindrome number, when we will increase ln digit it will remain same number i. e 121
#include <stdio.h>
#include <conio.h>
void main( )
{
int n, r, tmp, sum=0 ;
clrscr( ) ;
printf(" Enter any number ") ;
scanf("%d ", &n) ;
tmp=n ;
while (tmp)
{
r=n%10 ;
r=n/10 ;
sum= sum * 10 +r ;
}
if (sum ==tmp)
printf("\n%d is a Palindrome number ", tmp) ;
else
printf("\n%d is not a Palindrome number ",) ;
getch( ) ;
}
#include<stdio.h>
#include<conio.h>
void main ( )
{
int min, max, n, r,sum=0, tmp ;
clrscr( ) ;
printf ("\nEnter min number: ") ;
scanf(“%d”,&min) ;
printf(“\nEnter max number:”) ;
scanf ("%d", &max) ;
printf (" \nPalindrome number in the given range ") ;
for ( n=min, n<=max; n++)
{
tmp=n ;
sum=0 ;
while (tmp)
{
r=tmp%10 ;
tmp=tmp/10 ;
sum=sum * 10 + r ;
}
if (sum==n)
printf ("\n%d is Palindrome number ", n) ;
}
getch( ) ;
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,r,tmp,rev=0,sum=0;
clrscr( );
printf("Enter any number");
scanf("%d",&n);
tmp=n;
while(n)
{
r=n%10;
n=n/10;
rev=rev*10+r;
sum=sum+r;
}
printf("\n%d Reverse number is %d:",tmp,rev);
printf("\n sum of the reverse number is %d:",sum);
getch( );
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int n,f=1,tmp;
clrscr( );
printf("Take any number");
scanf("%d",&n);
tmp=n;
while(n)
{
f=f*n;
n--;
}
printf("\n%d factorial is %d",tmp,f);
getch( );
}
output :- Enter any number : 4
4 factorial is 24
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n;i,past=0,present=1,future;
clrscr();
printf(“enter any number: “);
scanf(“%d”,&n);
printf(“%d%d”,past,present);
future=past+present;
printf(“%d”,future);
for(i=0;i<n-3;i++)
{
past=present;
present=future;
future = past + present;
printf("\t %d", future );
}
getch();
}
Input: n=5
Output: 0 1 1
Past present future
0 1 1 2
Past present future
0 1 1 2 3
Past present future
Programs in turboc3 : Files
File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...
-
File Handling in C File Handling concept in C language is used for store a data permanently in computer. Using this concept we can store our...
-
Campus Drive Questions and Answers Q) What are main characteristics of C language? Ans) C is a procedural language. The m...
-
C TOKENS: The Smallest individual units are known as ‘C’ Tokens. ‘C’ as six types of tokens: 1. KEY WORDS 2. IDENTIFIERS 3. CONSTANTS 4.VARI...