C Programs: Program To Demostrate Arithmetic Operators
C Programs: Program To Demostrate Arithmetic Operators
# include <stdio.h>
int main()
{
int i;
i = 5;
printf("The Value of i is : %d", i);
i = i + 5;
printf("\nThe Value of i is : %d", i);
i = i - 2;
printf("\nThe Value of i is : %d", i);
i = i * 2;
printf("\nThe Value of i is : %d", i);
i = i / 4;
printf("\nThe Value of i is : %d", i);
i++;
printf("\nThe Value of i is : %d", i);
i++;
printf("\nThe Value of i is : %d", i);
i --;
printf("\nThe Value of i is : %d", i);
}
#include <stdio.h>
main()
{
printf(" short int is %2d bytes \n", sizeof(short int));
printf(" int is %2d bytes \n", sizeof(int));
printf(" int * is %2d bytes \n", sizeof(int *));
printf(" long int is %2d bytes \n", sizeof(long int));
printf(" long int * is %2d bytes \n", sizeof(long int *));
printf(" signed int is %2d bytes \n", sizeof(signed int));
printf(" unsigned int is %2d bytes \n", sizeof(unsigned int));
printf("\n");
printf(" float is %2d bytes \n", sizeof(float));
printf(" float * is %2d bytes \n", sizeof(float *));
printf(" double is %2d bytes \n", sizeof(double));
printf(" double * is %2d bytes \n", sizeof(double *));
printf(" long double is %2d bytes \n", sizeof(long double));
printf("\n");
printf(" signed char is %2d bytes \n", sizeof(signed char));
printf(" char is %2d bytes \n", sizeof(char));
printf(" char * is %2d bytes \n", sizeof(char *));
printf("unsigned char is %2d bytes \n", sizeof(unsigned char));
}
#include<stdio.h>
int main()
{
int num1,num2,opt;
printf("Enter the first Integer:\n");
scanf("%d",&num1);
printf("Enter the second Integer:\n");
scanf("%d",&num2);
for(;;)
{
printf("\n\n\n\nEnter the your option:\n");
printf("1-Addition.\n2-Substraction.\n3-Multiplication.\n4-Division.\n5-Exit.\n");
scanf("%d",&opt);
switch(opt)
{
case 1:printf("\nAddition of %d and %d is: %d",num1,num2,num1+num2);
break;
case 2:printf("\nSubstraction of %d and %d is: %d",num1,num2,num1-num2);
break;
case 3:printf("\nMultiplication of %d and %d is: %d",num1,num2,num1*num2);
break;
case 4:
if(num2==0)
{
printf("OOps Devide by zero\n");
}
else{
printf("\n Division of %d and %d is: %d",num1,num2,num1/num2);
}
break;
case 5: return 0;
break;
default:printf("\n Enter correct option\n");
break;
}
}
}
PROGRAM TO DEMOSTRATE TERNARY OPERATOR
#include<stdio.h>
int main()
{
int min,x,y;
printf("Enter two numbers\n");
scanf("%d%d",&x,&y);
min=x <= y ? x : y;
printf("\n Small number is: %d",min);
return 0;
}
#include<stdio.h>
int add( int, int); /* Function declaration */
main()
{
int i=1;
printf("i starts out life as %d.", i);
i = add(1, 1); /* Function call */
printf(" And becomes %d after function is executed.\n", i);
}
#include <stdio.h>
int main() {
int num, i = 1;
printf("\n Enter any Number:");
scanf("%d", &num);
printf("Multiplication table of %d: \n", num);
while (i <= 10) {
printf("\n %d x %d = %d", num, i, num * i);
i++;
}
return 0;
}
PROGRAM TO FIND THE SUM OF ALL DIGITS OF AN INTEGER NUMBER
#include <stdio.h>
void main()
{
long num, temp, digit, sum = 0;
printf("Enter the number\n");
scanf("%ld", &num);
temp = num;
while(num > 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("Given number =%ld\n", temp);
printf("Sum of the digits %ld =%ld\n", temp, sum);
}
#include <stdio.h>
#include <math.h>
void main()
{
float side, surfArea, volume;
printf("Enter the length of a side\n");
scanf("%f", &side);
surfArea = 6.0 * side * side;
volume = pow (side, 3);
printf("Surface area = %6.2f and Volume = %6.2f\n", surfArea, volume);
}
#include<stdio.h>
int main()
{
int r, c,t=5;
for ( r = 1 ; r <= 5 ; r++ )
{
for ( c = 1 ; c < t ; c++ )
printf(" ");
t--;
for ( c = 1 ; c <= 2*r - 1 ; c++ )
printf("*");
printf("\n");
}
}
#include<stdio.h>
#include<math.h>
main()
{
int a,b,c;
float disc,r1,r2;
for(;;)
{
printf("\nEnter the non Zero co-efficients a, b,c\n");
scanf("%d%d%d",&a,&b,&c);
if((a==0) || (b==0) || (c==0))
{
printf("\nPlease enter non zero co-efficients \n");
}else{
disc=((b*b)-(4*a*c));
if(disc>0)
{
printf("Roots are Real:\n");
r1=(-b-(sqrt(disc)))/(2.0*a);
r2=(-b+(sqrt(disc)))/(2.0*a);
printf("Roots are:%f and %f \n", r1,r2);
}
else if(disc<0)
{
printf("Roots are Imaginary:\n");
r1=-b/(2.0*a);
printf("First root: %lf +%fi \n", r1,sqrt(-disc)/(2.0*a));
printf("Second root:%lf -%lfi\n", r1,sqrt(-disc)/(2.0*a));
}
else
{
printf("Roots are Equal\n");
r1= -b/(2.0*a);
printf("Equal; roots are:%f and %f \n", r1,r1);
}
return 0;
}
}
}
PROGRAM TO INPUT REAL NUMBERS AND FIND THE MEAN, VARIANCE AND
STANDARD DEVIATION
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float avrg, var, SD, sum=0, sum1=0;
clrscr();
printf("Enter the value of N\n");
scanf("%d", &n);
printf("Enter %d real numbers\n",n);
for(i=0; i<n; i++)
{
scanf("%f", &x[i]);
}
/* Compute the sum of all elements */
for(i=0; i<n; i++)
{
sum = sum + x[i];
}
avrg = sum /(float) n;
/* Compute varaience and standard deviation */
for(i=0; i<n; i++)
{
sum1 = sum1 + pow((x[i] - avrg),2);
}
var = sum1 / (float) n;
SD = sqrt(var);
printf("Average of all elements = %.2f\n", avrg);
printf("Varience of all elements = %.2f\n", var);
printf("Standard deviation = %.2f\n", SD);
}
#include <stdio.h>
#include <conio.h>
void main()
{
float p, r, si;
int t;
clrscr();
printf("Enter the values of p,r and t\n");
scanf ("%f %f %d", &p, &r, &t);
si = (p * r * t)/ 100.0;
printf ("Amount = Rs. %5.2f\n", p);
printf ("Rate = Rs. %5.2f%\n", r);
printf ("Time = %d years\n", t);
printf ("Simple interest = %5.2f\n", si);
}
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define PI 3.142
void main()
{
float radius, area;
clrscr();
printf("Enter the radius of a circle\n");
scanf ("%f", &radius);
area = PI * pow (radius,2);
printf ("Area of a circle = %5.2f\n", area);
}
#include<stdio.h>
#include<math.h>
int main()
{
int a, b, c;
float s, area;
printf("Enter the values of the sides of the triangle: \n");
scanf("%d %d %d", &a, &b, &c);
if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0)) {
s = (a + b + c) / 2.0;
area = sqrt((s * (s - a) * (s - b) * (s - c)));
if (a == b && b == c) {
printf("Equilateral Triangle. \n");
printf("Area of Equilateral Triangle is: %f", area);
}
else if (a == b || b == c || a == c) {
printf("Isosceles Triangle. \n");
printf("Area of an Isosceles Triangle: %f", area);
}
else {
printf("Scalene Triangle. \n");
printf("Area of Scalene Triangle: %f", area);
}
}
else {
printf("Triangle formation not possible");
}
return 0;
}
#include <stdio.h>
#include <conio.h>
main()
{
int n, i, m,flag=0;
clrscr();
printf("Enter a positive integer: ");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;++i)
{
if(n%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
getch();
}
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter the value for a:\n");
scanf("%d", &a);
printf("Enter the value for b:\n");
scanf("%d", &b);
printf("Enter the value for c:\n");
scanf("%d", &c);
if ((a > b) && (a > c)) {
printf("\n The big one is a= %d", a);
} else if (b > c) {
printf("\n The big one is b= %d", b);
} else {
printf("\n The big one is c= %d", c);
}
return 0;
}
#include <stdio.h>
#include <conio.h>
void main()
{
union number
{
int n1;
float n2;
};
union number x;
clrscr() ;
printf("Enter the value of n1: ");
scanf("%d", &x.n1);
printf("Value of n1 =%d", x.n1);
printf("\nEnter the value of n2: ");
scanf("%d", &x.n2);
printf("Value of n2 = %d\n",x.n2);
}
#include <stdio.h>
#define DAYSINWEEK 7
void main()
{
int ndays, year, week, days;
printf("Enter the number of days\n");
scanf("%d",&ndays);
year = ndays/365;
week = (ndays % 365)/DAYSINWEEK;
days = (ndays%365) % DAYSINWEEK;
printf ("%d is equivalent to %d years, %d weeks and %d days\n",
ndays, year, week, days);
}
PERFECT NUMBER: Perfect number is a positive number which sum of all positive divisors
excluding that number is equal to that number. For example 6 is perfect number since divisor of
6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
#include<stdio.h>
int main()
{
int n,i=1,sum=0;
printf("Enter a number: ");
scanf("%d",&n);
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
PROGRAM TO FIND PERFECT NUMBERS
#include<stdio.h>
int main()
{
int n,i,sum;
int min,max;
printf("Enter the minimum range: ");
scanf("%d",&min);
printf("Enter the maximum range: ");
scanf("%d",&max);
printf("Perfect numbers in given range is: ");
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("%d ",n);
}
return 0;
}
#include<stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num!=0){
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf("%d is an Armstrong number",temp);
else
printf("%d is not an Armstrong number",temp);
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int num,rem,rev=0;
clrscr();
printf("nEnter any no to be reversed : ");
scanf("%d",&num);
while(num>=1)
{
rem = num % 10;
rev = rev * 10 + rem;
num = num / 10;
}
printf("nReversed Number : %d",rev);
getch();
}
#include<stdio.h>
int main()
{
int num,i,f,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num){
i=1,f=1;
r=num%10;
while(i<=r){
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp);
else
printf("%d is not a strong number",temp);
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius,fahrenheit;
clrscr();
printf("nEnter temp in Celsius : ");
scanf("%f",&celsius);
fahrenheit = (1.8 * celsius) + 32;
printf("nTemperature in Fahrenheit : %f ",fahrenheit);
getch();
}
#include<stdio.h>
#include<string.h>
int main()
{
int num,i,count=0;
char str1[10],str2[10];
printf("nEnter a number:");
scanf("%d",&num);
sprintf(str1,"%d",num); // Convert Number to String
strcpy(str2,str1); // Copy String into Other
strrev(str2); // Reverse 2nd Number
count = strcmp(str1,str2);
if(count==0)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;
}
PROGRAM WHICH PASSES ONE DIMENSION ARRAY TO FUNCTION.
#include <stdio.h>
#define N 5
void fstore1D(int a[], int a_size);
void fretrieve1D(int a[], int a_size);
void fedit1D(int a[], int a_size);
int main()
{
int a[N];
printf("Input data into the matrix:\n");
fstore1D(a, N);
fretrieve1D(a, N);
fedit1D(a, N);
fretrieve1D(a, N);
return 0;
}
void fstore1D(int a[], int n)
{
int i;
for ( i = 0; i < n; ++i )
scanf("%d", &a[i]);
}
void fretrieve1D(int a[], int n)
{
int i;
for ( i = 0; i < n; ++i )
printf("%6d ", a[i]);
printf("\n");
}
void fedit1D(int a[], int n)
{
int i, q;
for ( i = 0; i < n; ++i ){
printf("Prev. data: %d\nEnter 1 to edit 0 to skip.", a[i]);
scanf("%d", &q);
if ( q == 1 ){
printf("Enter new value: ");
scanf("%d", &a[i]);
}
}
}
#include<stdio.h>
#include<conio.h>
main()
{
int a[20],i,j,k,n;
clrscr();
printf("nEnter array size : ");
scanf("%d",&n);
printf("nAccept Numbers : ",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
clrscr();
printf("nOriginal array is : ");
for(i=0;i<n;i++)
printf(" %d",a[i]);
printf("nUpdated array is : ");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;)
{
if(a[j]==a[i])
{
for(k=j;k<n;k++)
a[k]=a[k+1];
n--;
}
else
j++;
}
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
#include<stdio.h>
int main()
{
int k,r;
long int i=0l,j=1,f;
//Taking maximum numbers form user
printf("Enter the number range:");
scanf("%d",&r);
printf("FIBONACCI SERIES: ");
printf("%ld %ld",i,j); //printing firts two values.
for(k=2;k<r;k++)
{
f=i+j;
i=j;
j=f;
printf(" %ld",j);
}
return 0;
}
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[50],sum,n;
printf("nEnter no of elements :");
scanf("%d",&n);
/* Reading values into Array */
printf("nEnter the values :");
for(i=0;i < n;i++)
scanf("%d",&a[i]);
/* computation of total */
sum=0;
for(i=0;i < n;i++)
sum=sum+a[i];
/* printing of all elements of array */
for(i=0;i < n;i++)
printf("na[%d]=%d",i,a[i]);
/* printing of total */
printf("nSum=%d",sum);
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n,smallest;
printf("n Enter no of elements :");
scanf("%d",&n);
/* read n elements in an array */
for(i=0 ; i < n ; i++)
scanf("%d",&a[i]);
smallest = a[0];
for(i = 0 ;i < n ; i++)
{
if ( a[i] < smallest )
smallest = a[i];
}
/* Print out the Result */
printf("nSmallest Element : %d",smallest);
getch();
}
#include<stdio.h>
int main()
{
int num,i=1,j,k;
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num){
k=0;
if(num%i==0){
j=1;
while(j<=i){
if(i%j==0)
k++;
j++;
}
if(k==2)
printf("\n%d is a prime factor",i);
}
i++;
}
return 0;
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],i,n,largest;
printf("n Enter no of elements :");
scanf("%d",&n);
/* read n elements in an array */
for(i=0 ; i < n ; i++)
scanf("%d",&a[i]);
largest = a[0];
for(i = 0;i<n;i++)
{
if(a[i] > largest )
largest = a[i];
}
/* Print out the Result */
printf("nLargest Element : %d",largest);
getch();
}
#include<stdio.h>
int main()
{
int i;
for(i=0;i<=255;i++)
printf("ASCII value of character %c: %d\n",i,i);
return 0;
}
#include<stdio.h>
int main()
{
int num,temp,factor=1;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(temp){
temp=temp/10;
factor = factor*10;
}
printf("Each digits of given number are: ");
while(factor>1){
factor = factor/10;
printf("%d ",num/factor);
num = num % factor;
}
return 0;
}
#include<stdio.h>
int main()
{
int number;
printf("Enter any integer: ");
scanf("%d",&number);
if(number % 2 ==0)
printf("%d is even number.",number);
else
printf("%d is odd number.",number);
return 0;
}
#include<stdio.h>
int main()
{
int n,r,ncr;
printf("Enter any two numbers->");
scanf("%d %d",&n,&r);
ncr=fact(n)/(fact(r)*fact(n-r));
printf("The NCR factor of %d and %d is %d",n,r,ncr);
return 0;
}
int fact(int n)
{
int i=1;
while(n!=0)
{
i=i*n;
n--;
}
return i;
}
#include<stdio.h>
long fact(int);
int main()
{
int line,i,j;
printf("Enter the no. of lines: ");
scanf("%d",&line);
for(i=0;i<line;i++){
for(j=0;j<line-i-1;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;
}
#include<string.h>
#include<stdio.h>
int main()
{
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
if(strcmp(rev,str))
printf("\nThe string is not a palindrome");
else
printf("\nThe string is a palindrome");
return 0;
}
#include<stdio.h>
int stringToInt(char[] );
int main()
{
char str[10];
int intValue;
printf("Enter any integer as a string: ");
scanf("%s",str);
intValue = stringToInt(str);
printf("Equivalent integer value: %d",intValue);
return 0;
}
int stringToInt(char str[])
{
int i=0,sum=0;
while(str[i]!='\0'){
if(str[i]< 48 || str[i] > 57){
printf("Unable to convert it into integer.\n");
return 0;
}
else{
sum = sum*10 + (str[i] - 48);
i++;
}
}
return sum;
}
#include<stdio.h>
int main()
{
int n1,n2,x,y;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
x=n1,y=n2;
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("L.C.M=%d",x*y/n1);
return 0;
}
HFC is also called greatest common divisor (gcd). HCF of two numbers is a largest positive
numbers which can divide both numbers without any remainder. For example HCF of two
numbers 4 and 8 is 2 since 2 is the largest positive number which can dived 4 as well as 8
without a remainder.
#include<stdio.h>
int main(){
int x,y,m,i;
printf("Insert any two number: ");
scanf("%d%d",&x,&y);
if(x>y)
m=y;
else
m=x;
for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nHCF of two number is : %d",i) ;
break;
}
}
return 0;
}
HFC is also called greatest common divisor (gcd). HCF of two numbers is a largest positive
numbers which can divide both numbers without any remainder. For example HCF of two
numbers 4 and 8 is 2 since 2 is the largest positive number which can dived 4 as well as 8
without a remainder.
In HCF we try to find any largest number which can divide both the number.
For example: HCF or GCD of 20 and 30
Both number 20 and 30 are divisible by 1, 2,5,10.
HCF=max (1, 2, 3, 4, 10) =10
#include<stdio.h>
int main(){
int x,y,m,i;
printf("Insert any two number: ");
scanf("%d%d",&x,&y);
if(x>y)
m=y;
else
m=x;
for(i=m;i>=1;i--){
if(x%i==0&&y%i==0){
printf("\nHCF of two number is : %d",i) ;
break;
}
}
return 0;
}
#include<stdio.h>
int main(){
int a=5,b=10;
//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d b= %d",a,b);
//process two
a=5;b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);
//process three
a=5;b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d b= %d",a,b);
//process four
a=5;b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d b= %d",a,b);
//process five
a=5,b=10;
a=b+a,b=a-b,a=a-b;
printf("\na= %d b= %d",a,b);
return 0;
}
printf("n");
no_of_spaces--;
}
return 0;
}
#include<stdio.h>
int main(){
int a[10],b[10],c[10],i;
printf("Enter First array->");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\nEnter Second array->");
for(i=0;i<10;i++)
scanf("%d",&b[i]);
printf("Arrays before swapping");
printf("\nFirst array->");
for(i=0;i<10;i++){
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++){
printf("%d",b[i]);
}
for(i=0;i<10;i++){
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
printf("\nArrays after swapping");
printf("\nFirst array->");
for(i=0;i<10;i++){
printf("%d",a[i]);
}
printf("\nSecond array->");
for(i=0;i<10;i++){
printf("%d",b[i]);
}
return 0;
}
#include<stdio.h>
int main(){
int number,i=0,j,digit;
char * word[1000];
printf("Enter any integer: ");
scanf("%d",&number);
while(number){
digit = number %10;
number = number /10;
switch(digit){
case 0: word[i++] = "zero"; break;
case 1: word[i++] = "one"; break;
case 2: word[i++] = "two"; break;
case 3: word[i++] = "three"; break;
case 4: word[i++] = "four"; break;
case 5: word[i++] = "five"; break;
case 6: word[i++] = "six"; break;
case 7: word[i++] = "seven"; break;
case 8: word[i++] = "eight"; break;
case 9: word[i++] = "nine"; break;
}
}
for(j=i-1;j>=0;j--){
printf("%s ",word[j]);
}
return 0;
}
#include<stdio.h>
void predigits(char c1,char c2);
void postdigits(char c,int n);
char roman_Number[1000];
int i=0;
int main()
{
int j;
long int number;
printf("Enter any natural number: ");
scanf("%d",&number);
if(number <= 0){
printf("Invalid number");
return 0;
}
while(number != 0){
if(number >= 1000){
postdigits('M',number/1000);
number = number - (number/1000) * 1000;
}
else if(number >=500){
if(number < (500 + 4 * 100)){
postdigits('D',number/500);
number = number - (number/500) * 500;
}
else{
predigits('C','M');
number = number - (1000-100);
}
}
else if(number >=100){
if(number < (100 + 3 * 100)){
postdigits('C',number/100);
number = number - (number/100) * 100;
}
else{
predigits('L','D');
number = number - (500-100);
}
}
else if(number >=50){
if(number < (50 + 4 * 10)){
postdigits('L',number/50);
number = number - (number/50) * 50;
}
else{
predigits('X','C');
number = number - (100-10);
}
}
else if(number >=10){
if(number < (10 + 3 * 10)){
postdigits('X',number/10);
number = number - (number/10) * 10;
}
else{
predigits('X','L');
number = number - (50-10);
}
}
else if(number >=5){
if(number < (5 + 4 * 1)){
postdigits('V',number/5);
number = number - (number/5) * 5;
}
else{
predigits('I','X');
number = number - (10-1);
}
}
else if(number >=1){
if(number < 4){
postdigits('I',number/1);
number = number - (number/1) * 1;
}
else{
predigits('I','V');
number = number - (5-1);
}
}
}
printf("Roman number will be: ");
for(j=0;j<i;j++)
printf("%c",roman_Number[j]);
return 0;
}
void predigits(char c1,char c2){
roman_Number[i++] = c1;
roman_Number[i++] = c2;
}
void postdigits(char c,int n)
{
int j;
for(j=0;j<n;j++)
roman_Number[i++] = c;
}
#include<stdio.h>
#include<string.h>
int digitValue(char);
int main()
{
char roman_Number[1000];
int i=0;
long int number =0;
printf("Enter any roman number (Valid digits are I, V, X, L, C, D, M): \n");
scanf("%s",roman_Number);
while(roman_Number[i])
{
if(digitValue(roman_Number[i]) < 0){
printf("Invalid roman digit : %c",roman_Number[i]);
return 0;
}
if((strlen(roman_Number) -i) > 2)
{
if(digitValue(roman_Number[i]) < digitValue(roman_Number[i+2])){
printf("Invalid roman number");
return 0;
}
}
#include<stdio.h>
#include<string.h>
void toWord(int,int);
char * getPositionValue(int);
char * digitToWord(int);
char word[100][30];
int i =0;
int main()
{
int j,k,subnumer;
unsigned long int number;
printf("Enter any postive number: ");
scanf("%lu",&number);
if(number ==0)
{
printf("Zero");
return 0;
}
while(number)
{
if(i==1)
{
toWord(number %10,i);
number = number/10;
}
else
{
toWord(number %100,i);
number = number/100;
}
i++;
}
printf("Number in word: ");
*word[i-1] = *word[i-1] - 32;
for(j=i-1;j>=0;j--)
{
printf("%s",word[j]);
}
return 0;
}
void toWord(int number,int position)
{
char numberToword[100]={" "};
if(number ==0)
{
}
else if (number < 20 ||number %10==0){
strcpy(numberToword,digitToWord(number));
}
else
{
strcpy(numberToword,digitToWord((number/10)*10));
strcat(numberToword,digitToWord(number%10));
}
strcat(numberToword,getPositionValue(position));
strcpy(word[i],numberToword);
}
char * getPositionValue(int postion){
static char positionValue[10]=" ";
switch(postion)
{
case 1: strcpy(positionValue,"hundreds "); break;
case 2: strcpy(positionValue,"thousand "); break;
case 3: strcpy(positionValue,"lakh "); break;
case 4: strcpy(positionValue,"crore "); break;
case 5: strcpy(positionValue,"arab "); break;
case 6: strcpy(positionValue,"kharab "); break;
case 7: strcpy(positionValue,"neel "); break;
case 8: strcpy(positionValue,"padam "); break;
}
return positionValue;
}
char * digitToWord(int digit){
static char digitInWord[10]=" ";
switch(digit)
{
case 1: strcpy(digitInWord , "one "); break;
case 2: strcpy(digitInWord , "two "); break;
case 3: strcpy(digitInWord , "three "); break;
case 4: strcpy(digitInWord , "four "); break;
case 5: strcpy(digitInWord , "five "); break;
case 6: strcpy(digitInWord , "six "); break;
case 7: strcpy(digitInWord , "seven "); break;
case 8: strcpy(digitInWord , "eight "); break;
case 9: strcpy(digitInWord , "nine ");break;
case 10: strcpy(digitInWord , "ten "); break;
case 11: strcpy(digitInWord , "eleven "); break;
case 12: strcpy(digitInWord , "twelve "); break;
case 13: strcpy(digitInWord , "thirteen "); break;
case 14: strcpy(digitInWord , "fourteen "); break;
case 15: strcpy(digitInWord , "fifteen "); break;
case 16: strcpy(digitInWord , "sixteen "); break;
case 17: strcpy(digitInWord , "seventeen "); break;
case 18: strcpy(digitInWord , "eighteen "); break;
case 19: strcpy(digitInWord , "nineteen "); break;
case 20: strcpy(digitInWord , "twenty "); break;
case 30: strcpy(digitInWord , "thirty "); break;
case 40: strcpy(digitInWord , "fourty "); break;
case 50: strcpy(digitInWord , "fifty "); break;
case 60: strcpy(digitInWord , "sixty "); break;
case 70: strcpy(digitInWord , "seventy "); break;
case 80: strcpy(digitInWord , "eighty "); break;
case 90: strcpy(digitInWord,"ninety "); break;
}
return digitInWord;
}
#include<stdio.h>
#include<math.h>
int main()
{
char fromUnit,toUnit;
char *fUnit,*tUnit;
long double fromValue,meterValue,toValue;
int power =0;
printf("To convert the 12 Inch to Foot\n");
printf("enter the the unit in the format : dc12\n");
printf("Ell: a\n");
printf("Femi: b\n");
printf("Foot: c\n");
printf("Inch: d\n");
printf("Light year: e\n");
printf("Metre: f\n");
printf("Mile: g\n");
printf("Nano meter: h\n");
printf("Pace: i\n");
printf("Point: j\n");
printf("Yard: k\n");
printf("Mili meter: l\n");
printf("Centi meter: m\n");
printf("Deci meter: n\n");
printf("Deca meter: o\n");
printf("Hecto meter: p\n");
printf("Kilo meter: q\n");
scanf("%c%c%Lf",&fromUnit,&toUnit,&fromValue);
switch(fromUnit)
{
case 'a': meterValue = fromValue * 1.143; fUnit="ell"; break;
case 'b': meterValue = fromValue ; power = -15; fUnit="fm"; break;
case 'c': meterValue = fromValue * 0.3048; fUnit="ft"; break;
case 'd': meterValue = fromValue * 0.0254; fUnit="in"; break;
case 'e': meterValue = fromValue * 9.4607304725808; power =15; fUnit="ly"; break;
case 'f': meterValue = fromValue; fUnit="m"; break;
case 'g': meterValue = fromValue * 1609.344; fUnit="mi"; break;
case 'h': meterValue = fromValue; fUnit="nm"; power = -9; break;
case 'i': meterValue = fromValue * 0.762 ; fUnit="pace"; break;
case 'j': meterValue = fromValue * 0.000351450; fUnit="pt"; break;
case 'k': meterValue = fromValue * 0.9144; fUnit="yd"; break;
case 'l': meterValue = fromValue * 0.001; fUnit="mm"; break;
case 'm': meterValue = fromValue * 0.01; fUnit="cm"; break;
case 'n': meterValue = fromValue * 0.1; fUnit="deci meter"; break;
case 'o': meterValue = fromValue * 10; fUnit="deca meter"; break;
case 'p': meterValue = fromValue * 100; fUnit="hm"; break;
case 'q': meterValue = fromValue * 1000; fUnit="km"; break;
default:
printf("Invalid input"); exit(0);
}
switch(toUnit)
{
case 'a': toValue = meterValue/1.143; tUnit="ell"; break;
case 'b': toValue = meterValue; tUnit="fm"; break;
case 'c': toValue = meterValue/0.3048; tUnit="ft"; break;
case 'd': toValue = meterValue/0.0254; tUnit="in"; break;
case 'e': toValue = meterValue/9.4607304725808; tUnit="ly"; break;
case 'f': toValue = meterValue; tUnit="m";break;
case 'g': toValue = meterValue/1609.344; tUnit="mi"; break;
case 'h': toValue = meterValue; tUnit="nm"; break;
case 'i': toValue = meterValue/0.762; tUnit="pace"; break;
case 'j': toValue = meterValue/0.000351450; tUnit="pt"; break;
case 'k': toValue = meterValue/0.9144; tUnit="yd"; break;
case 'l': toValue = meterValue/0.001; tUnit="mm"; break;
case 'm': toValue = meterValue/0.01; tUnit="cm"; break;
case 'n': toValue = meterValue/0.1; tUnit="deci meter"; break;
case 'o': toValue = meterValue/10; tUnit="deca meter"; break;
case 'p': toValue = meterValue/100; tUnit="hm"; break;
case 'q': toValue = meterValue/1000; tUnit="km"; break;
default:
printf("Invalid input"); exit(0);
}
if(power==0)
printf("%.4Lf %s = %.4Lf %s",fromValue,fUnit,toValue,tUnit);
else{
while(tovalue > 10
printf("%.4Lf %s = %.4Lf*10^%d %s",fromValue,fUnit,toValue,power,tUnit);
}
return 0;
}
#include<stdio.h>
int totalThousand =1000;
int totalFiveFundred =1000;
int totalOneHundred =1000;
int main()
{
unsigned long withdrawAmount;
unsigned long totalMoney;
int thousand=0,fiveHundred=0,oneHundred=0;
printf("Enter the amount in multiple of 100: ");
scanf("%lu",&withdrawAmount);
if(withdrawAmount %100 != 0){
printf("Invalid amount;");
return 0;
}
totalMoney = totalThousand * 1000 + totalFiveFundred* 500 + totalOneHundred*100;
if(withdrawAmount > totalMoney){
printf("Sorry,Insufficient money");
return 0;
}
thousand = withdrawAmount / 1000;
if(thousand > totalThousand)
thousand = totalThousand;
withdrawAmount = withdrawAmount - thousand * 1000;
if (withdrawAmount > 0){
fiveHundred = withdrawAmount / 500;
if(fiveHundred > totalFiveFundred)
fiveHundred = totalFiveFundred;
withdrawAmount = withdrawAmount - fiveHundred * 500;
}
if (withdrawAmount > 0)
oneHundred = withdrawAmount / 100;
printf("Total 1000 note: %d\n",thousand);
printf("Total 500 note: %d\n",fiveHundred);
printf("Total 100 note: %d\n",oneHundred);
return 0;
}
We can write a c program without using main function. We can compile it but we cannot execute
a program without a main function. Since in C execution of any program start from main
function. Examples of c program without a main is all the c library functions. Function printf is
an example of library function which has been written and complied without using main
function. To use or run such functions we write other program using main function and call those
functions.
#include<stdio.h>
#include<string.h>
int main(){
char str[20];
int i;
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf("\nThe string in lower case is->%s",str);
return 0;
}
#include<stdio.h>
int main(){
char str[20];
int i;
printf("Enter any string->");
scanf("%s",str);
printf("The string is->%s",str);
for(i=0;i<=strlen(str);i++){
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf("\nThe string in lowercase is->%s",str);
return 0;
}
#include <stdio.h>
int isvowel(char chk);
int main(){
char text[1000], chk;
int count;
count = 0;
while((text[count] = getchar()) != '\n')
count++;
text[count] = '\0';
count = 0;
while ((chk = text[count]) != '\0'){
if (isvowel(chk)){
if((chk = text[++count]) && isvowel(chk)){
putchar(text[count -1]);
putchar(text[count]);
putchar('\n');
}
}
else
++count;
}
return 0;
}
int isvowel(char chk){
if(chk == 'a' || chk == 'e' || chk == 'i' || chk == 'o' || chk == 'u')
return 1;
return 0;
}
#include<stdio.h>
int main(){
int i,j,n;
char str[20][20],temp[20];
puts("Enter the no. of string to be sorted");
scanf("%d",&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){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("The sorted string\n");
for(i=0;i<=n;i++)
puts(str[i]);
return 0;
}
#include<stdio.h>
#include<conio.h>
main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
clrscr();
printf("\nEnter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o)
{
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of second matrix");
}
else
{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<p;j++)
{
printf("%d\t",c[i][j]);
}
}
getch();
}
If more than two variables of same type is to be returned then we can use array .
Store each and every value to be returned in an array and return base address of that
array.
Construct a Structure containing values to be returned and then return the base address of
the structure to the calling function.