Write a a program to find the area of a triangle.
Formula of area of any triangle:
Area = √(s*(s-a)*(s-b)*(s-c))
Where s = (a + b + c)/2
Output:
Enter size of each sides of the triangle: 2 4 5
Area of triangle is: 3.800
Code:
#include<stdio.h>
#include<math.h>
int main(){
float a,b,c;
float s,area;
printf("Enter size of each sides of triangle");
scanf("%f%f%f",&a,&b,&c);
s = (a+b+c)/2;
area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area of triangle is: %.3f",area);
return 0; }
Martes, Oktubre 16, 2012
Write a program c program to convert binary to octal.
Output:
Enter any number any binary number: 1101
Equivalent hexadecimal value: 15
Code:
#include<stdio.h>
int main(){
long int binaryNumber,octalNumber=0,j=1,remainder;
printf("Enter any number any binary number: ");
scanf("%ld",&binaryNumber);
while(binaryNumber!=0){
remainder=binaryNumber%10;
octalNumber=octalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
printf("Equivalent octal value: %lo",octalNumber);
return 0;
}
Output:
Enter any number any binary number: 1101
Equivalent hexadecimal value: 15
Code:
#include<stdio.h>
int main(){
long int binaryNumber,octalNumber=0,j=1,remainder;
printf("Enter any number any binary number: ");
scanf("%ld",&binaryNumber);
while(binaryNumber!=0){
remainder=binaryNumber%10;
octalNumber=octalNumber+remainder*j;
j=j*2;
binaryNumber=binaryNumber/10;
}
printf("Equivalent octal value: %lo",octalNumber);
return 0;
}
Write a program for addition of binary numbers.
Enter any second binary number: 1101
Sum of two binary numbers: 1110000
Code:
#include<stdio.h>
int main(){
long int binary1,binary2;
int i=0,remainder = 0,sum[20];
printf("Enter any first binary number: ");
scanf("%ld",&binary1);
printf("Enter any second binary number: ");
scanf("%ld",&binary2);
while(binary1!=0||binary2!=0){
sum[i++] = (binary1 %10 + binary2 %10 + remainder ) % 2;
remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
binary1 = binary1/10;
binary2 = binary2/10;
}
if(remainder!=0)
sum[i++] = remainder;
--i;
printf("Sum of two binary numbers: ");
while(i>=0)
printf("%d",sum[i--]);
return 0;
}
Output:
Enter any first binary number: 1100011 Enter any second binary number: 1101
Sum of two binary numbers: 1110000
Code:
#include<stdio.h>
int main(){
long int binary1,binary2;
int i=0,remainder = 0,sum[20];
printf("Enter any first binary number: ");
scanf("%ld",&binary1);
printf("Enter any second binary number: ");
scanf("%ld",&binary2);
while(binary1!=0||binary2!=0){
sum[i++] = (binary1 %10 + binary2 %10 + remainder ) % 2;
remainder = (binary1 %10 + binary2 %10 + remainder ) / 2;
binary1 = binary1/10;
binary2 = binary2/10;
}
if(remainder!=0)
sum[i++] = remainder;
--i;
printf("Sum of two binary numbers: ");
while(i>=0)
printf("%d",sum[i--]);
return 0;
}
Write a program to convert decimal to roman number.
Output:
Enter any natural number: 87
Roman number will be: LXXXVII
Code:
#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;
}
Output:
Enter any natural number: 87
Roman number will be: LXXXVII
Code:
#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;
}
Write a program in c to print 1 to 100 without using loop.
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Code:
#include<stdio.h>
int main(){
int num = 1;
print(num);
return 0;
}
int print(num){
if(num<=100){
printf("%d ",num);
print(num+1);
}
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
Code:
#include<stdio.h>
int main(){
int num = 1;
print(num);
return 0;
}
int print(num){
if(num<=100){
printf("%d ",num);
print(num+1);
}
}
Write a program that will count the digits of a given number in c language using recursion.
Output:
Enter a number: 1234567
Total digits is: 7
Code:
#include<stdio.h>
int countDigits(num);
int main(){
int num,count;
printf("Enter a number: ");
scanf("%d",&num);
count = countDigits(num);
printf("Total digits is: %d",count);
return 0;
}
int countDigits(int num){
static int count=0;
if(num!=0){
count++;
countDigits(num/10);
}
return count; }
Output:
Enter a number: 1234567
Total digits is: 7
Code:
#include<stdio.h>
int countDigits(num);
int main(){
int num,count;
printf("Enter a number: ");
scanf("%d",&num);
count = countDigits(num);
printf("Total digits is: %d",count);
return 0;
}
int countDigits(int num){
static int count=0;
if(num!=0){
count++;
countDigits(num/10);
}
return count; }
Write a program to find out the sum of digits in c using recursion.
Output:
Enter a number: 45
Sum of digits of number: 9
Output:
#include<stdio.h>
int getSum(int);
int main(){
int num,sum;
printf("Enter a number: ");
scanf("%d",&num);
sum = getSum(num);
printf("Sum of digits of number: %d",sum);
return 0;
}
int getSum(int num){
static int sum =0,r;
if(num!=0){
r=num%10;
sum=sum+r;
getSum(num/10);
}
return sum; }
Output:
Enter a number: 45
Sum of digits of number: 9
Output:
#include<stdio.h>
int getSum(int);
int main(){
int num,sum;
printf("Enter a number: ");
scanf("%d",&num);
sum = getSum(num);
printf("Sum of digits of number: %d",sum);
return 0;
}
int getSum(int num){
static int sum =0,r;
if(num!=0){
r=num%10;
sum=sum+r;
getSum(num/10);
}
return sum; }
Write a c program to find out the sum of series 1 + 2 + …. + n.
Output:
Enter the n i.e. max values of series: 5
Sum of the series: 1 + 2 + 3 + 4 + 5 = 15
Mathematical Formula: Sum of the series 1 + 2 + 3 + … + n = n (n+1)/2
Code:
#include<stdio.h>
int main(){
int n,i;
int sum=0;
printf("Enter the n i.e. max values of series: ");
scanf("%d",&n);
sum = (n * (n + 1)) / 2;
printf("Sum of the series: ");
for(i =1;i <= n;i++){
if (i!=n)
printf("%d + ",i);
else
printf("%d = %d ",i,sum);
}
return 0; }
Output:
Enter the n i.e. max values of series: 5
Sum of the series: 1 + 2 + 3 + 4 + 5 = 15
Mathematical Formula: Sum of the series 1 + 2 + 3 + … + n = n (n+1)/2
Code:
#include<stdio.h>
int main(){
int n,i;
int sum=0;
printf("Enter the n i.e. max values of series: ");
scanf("%d",&n);
sum = (n * (n + 1)) / 2;
printf("Sum of the series: ");
for(i =1;i <= n;i++){
if (i!=n)
printf("%d + ",i);
else
printf("%d = %d ",i,sum);
}
return 0; }
Write a bubble sort using c program.
Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9 After sorting: 0 2 6 9 11
Code:
#include<stdio.h>
int main(){
int s,temp,i,j,a[20];
printf("Enter total numbers of elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=s-2;i>=0;i--){
for(j=0;j<=i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0; }
Output:
Enter total numbers of elements: 5
Enter 5 elements: 6 2 0 11 9 After sorting: 0 2 6 9 11
Code:
#include<stdio.h>
int main(){
int s,temp,i,j,a[20];
printf("Enter total numbers of elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
for(i=s-2;i>=0;i--){
for(j=0;j<=i;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("After sorting: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0; }
Write a simple code for linear search in c programming language.
Output:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0 The number is found
Code:
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");
return 0; }
Output:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0 The number is found
Code:
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");
return 0; }
Write a c program which takes password from user.
Output:
Enter the password: *******
You have entered: fgt67m,
Code:
#include<stdio.h>
#define MAX 500
int main(){
char password[MAX];
char p;
int i=0;
printf("Enter the password:");
while((p=getch())!= 13){
password[i++] = p;
printf("*");
}
password[i] = '\0';
if(i<6)
printf("\nWeak password");
printf("\nYou have entered: %s",password);
return 0; }
Output:
Enter the password: *******
You have entered: fgt67m,
Code:
#include<stdio.h>
#define MAX 500
int main(){
char password[MAX];
char p;
int i=0;
printf("Enter the password:");
while((p=getch())!= 13){
password[i++] = p;
printf("*");
}
password[i] = '\0';
if(i<6)
printf("\nWeak password");
printf("\nYou have entered: %s",password);
return 0; }
Write a program to print Pascal triangle which you might have studied in Binomial
Theorem in Mathematics. Number of rows of Pascal triangle to print is
entered by the user. First four rows of Pascal triangle are shown below.
Output:
Output:
1 1 1 1 2 1 1 3 3 1
Code:
#include<stdio.h>
long factorial(int);
main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d",&n);
for ( i = 0 ; i < n ; i++ )
{
for ( c = 0 ; c <= ( n - i - 2 ) ; c++ )
printf(" ");
for( c = 0 ; c <= i ; c++ )
printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));
printf("\n");
}
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for( c = 1 ; c <= n ; c++ )
result = result*c;
return ( result );
}
Write a program to add digits of a number using modulus operator.
Output:
Enter a number
12358
Sum of digits entered number = 19
Code:
Output:
Enter a number
12358
Sum of digits entered number = 19
Code:
#include <stdio.h>
main()
{
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
return 0;
}
Write a program to convert decimal to hexadecimal number.
Code:
Output:
Enter the number: 31
Conversion of Decimal to Hexadecimal number
1FCode:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
Int x, y=30, z;
clrscr();
printf(“Enter the number:”);
scanf(“%d”, &x);
printf(“\n Conversion of Decimal to Hexadecimal number\n”);
for(;;)
{
if(x= =0)
exit(1);
z=x%16;
x=x/16;
gotoxy(y--,5);
switch(z)
{
case 10:
printf(“A”);
break;
case 11:
printf(“%c”, „B?);
break;
case 12:
printf(%c”, „C”);
break;
case 13:
printf(“D”);
break;
case 14:
printf(“E”);
break;
case 15:
printf(“F”);
default:
printf(“%d”, z);
}
}
getch();
}
Write a program to reverse a number entered by the user and then prints the reversed number on the screen.
Output:
Enter the number to reverse
1234
Code:
Output:
Enter the number to reverse
1234
Code:
#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);
return 0;
}
Write a program to print a diamond pattern of stars.
Output:
Output:
* *** ***** *** *
Code:
#include <stdio.h>
int main()
{
int n, c, k, space = 1;
printf("Enter number of rows\n");
scanf("%d", &n);
space = n - 1;
for (k = 1; k <= n; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space--;
for (c = 1; c <= 2*k-1; c++)
printf("*");
printf("\n");
}
space = 1;
for (k = 1; k <= n - 1; k++)
{
for (c = 1; c <= space; c++)
printf(" ");
space++;
for (c = 1 ; c <= 2*(n-k)-1; c++)
printf("*");
printf("\n");
}
return 0;
}
Write a program to determine the leap year.Year will be entered by the user.
Output:
Output:
Enter a year to check if it is a leap year
2012
2012 is a leap year.
Code:
#include <stdio.h>
int main()
{
int year;
printf("Enter a year to check if it is a leap year\n");
scanf("%d", &year);
if ( year%400 == 0)
printf("%d is a leap year.\n", year);
else if ( year%100 == 0)
printf("%d is not a leap year.\n", year);
else if ( year%4 == 0 )
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
Write a program to perform basic arithmetic operations example addition , subtraction,
multiplication and division of two numbers. Numbers are assumed to be
integers and will be entered by the user.
Output:
Enter two integers
5 3
Sum = 8
Difference = 2
Multiplication = 15
Division = 1.67
Code:
Output:
Enter two integers
5 3
Sum = 8
Difference = 2
Multiplication = 15
Division = 1.67
Code:
#include <stdio.h>
int 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;
}
Write a program to print the following pattern.
*
***
*****
*******
*********
Code:
#include<stdio.h>
main()
{
int row, c, n, temp;
printf("Enter The Number of Rows You Wish To Print ");
scanf("%d",&n);
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c < temp ; c++ )
printf(" ");
temp--;
for ( c = 1 ; c <= 2*row - 1 ; c++ )
printf("*");
printf("\n");
}
return 0;
}
Mag-subscribe sa:
Mga Komento (Atom)