Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Sunday, September 2, 2012

Factorial of a Number Using While loop in C program

Factorial of a Number Using While loop in C program

This is program to find out factorial of a number using simple while loop. this logic use to create a function like fact to find out any factorial of number /Integer.

/* Program For Factorial Of a No.*/
#include<stdio.h>

int main()
{
int no,temp=1;
printf("\nEnter no\n");
scanf("%d",&no);

int fact=1;
while(1)
{
if(temp>no)
 break;
fact=fact*temp;
temp++;
}

printf("Factorial of No    %d \t is :%d\n",no,fact);
return 0;


}



Decimal to Binary Conversion Logic in C program

Decimal to Binary Conversion Logic in C program

This is program which is gives you output as Binary of any Integer. Program Store the binary output as Integer just for reference. We can use it further where required. Decimal to binary needs Math.h Library file to use power function.



/* Program For Decimal to Binary Conversions*/
#include<stdio.h>
#include<math.h>

int main()
{
int no,rem;
printf("\nEnter no\n");
scanf("%d",&no);
int bs=no;
int sum=0;
int pw=0;
while(no)
{
rem=no%2;
sum=sum+rem*(pow(10,pw));
no=no/2;
pw++;

}

printf("Decimal to Binary  of this no  %d \t :%d\n",bs,sum);
return 0;


}


Calculator Operation Using Switch Case C program

Calculator Operation Using Switch Case C program

This program specially for Calculator Operation Using Switch Case C program which can do Addition,Subtractions,Multiplication,Division mathematical operations. It also has Menu to do Such Options in Simple Formats

/* Program For Addition,Subtraction,Division,Multiplication*/
#include<stdio.h>

int main()
{
int a,b,c;

while(1)
{
printf("\n****Choose The Following Options****\n");
printf("-Press 1 for Additions\n");
printf("-Press 2 for Subtractions\n");
printf("-Press 3 for Multiplication\n");
printf("-Press 4 for Divisions\n");
printf("-Press 5 for Exit\n");
scanf("%d",&c);
if(c==5)
 break;
printf("Please Enter two nos:");
scanf("%d %d",&a,&b);
switch(c)
{
case 1:printf("\nAdditions is:\t%d",a+b);
 break;
case 2:printf("\nSubtractions is:\t%d",a-b);
 break;
case 3:printf("\nMultiplication is:\t%d",a*b);
 break;
case 4:printf("\nDivision is:\t%d",a/b);
 break;
default:printf("\n Invalid Choice....!\n");
 continue;
}
}

return 0;


}



Addition of Digits of Given No C Program

Addition of Digits of Given No C Program


This program specially for separating the digits from given more than 2 digit no and add it together . This logic mainly useful at the time of reversing the digit of given no.

/* Program For Adding digits of No*/
#include<stdio.h>

int main()
{
int no,temp;
printf("\nEnter no\n");
scanf("%d",&no);
int bs=no;
int count=;
while(no)
{
temp =no%10;
sum=sum+temp;
no=no/10;

}

printf("Sum of digit  of this no  %d \t :%d\n",bs,sum);
return 0;

}

Check Armstrong Number from given Input Integer C program

Check Armstrong Number from given Input Integer C program

This program is to check given input integer is Armstrong Number or Not such that sum of qube of each digit is equal to that no.

/* Program to check  Armstrong No*/
#include<stdio.h>
#include<math.h>

int main()
{
int no,rem;
printf("\nEnter no\n");
scanf("%d",&no);
int bs=no;
int sum=0;
while(no)
{
rem=no%10;
sum=sum+(pow(rem,3));
no=no/10;


}
if(bs==sum)
 printf("The No %d is Armstrongs No.....!!!\n",bs);
else
 printf("The No %d  is not a Armstrongs NO....!!\n",bs);
return 0;

}


Addition of Two Dimensional Matrix in C program

 Addition of Two Dimensional Matrix in C program


The following program is the example of 2 Dimensional Array for adding it and save it to 3rd Array.

/* Program to Addition of two Matrix */

#include<stdio.h>
void printarry(int ap[2][2]);

int main()
{
int n,i,j;
int a[2][2],b[2][2],c[2][2];
printf("Enter The A matrix");
for(i=0;i<2;i++)
{
 for(j=0;j<2;j++)
{
 scanf("%d",&a[i][j]);
}
}

printf("Enter the B Matrix");
for(i=0;i<2;i++)
{
 for(j=0;j<2;j++)
{
 scanf("%d",&b[i][j]);
}
}

for(i=0;i<2;i++)
{
 for(j=0;j<2;j++)
{
 c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nMatrix A:\n");
printarry(a);
printf("\nMatrix B:\n");
printarry(b);
printf("Addition of Matrix is:\n");
printarry(c);
return 0;
}

void printarry(int ap[2][2])
{
int m,n;
for(m=0;m<2;m++)
{
 for(n=0;n<2;n++)
{
 printf("%d ",ap[m][n]);
}printf("\n");
}
}