Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts
Monday, October 15, 2012
Sunday, October 14, 2012
Print Armstrong numbers Less Than 1000
/*****<soeasyprograms.blogspot.in>*****/
#include<stdio.h>
#include<conio.h>
void main()
{
int q,a,b,c,z,x,n=1;
clrscr();
printf(" OUTPUT :");
while(n<1000)
{
a=n%10;
q=n/10;
b=q%10;
c=q/10;
z=((c*100)+(b*10)+a);
x=(a*a*a)+(b*b*b)+(c*c*c);
if(x==z)
printf("\n\t%d",n);
n++;
}
getch();
}
Thursday, October 11, 2012
Decimal to Binary Conversion
/*****<soeasyprograms.blogspot.in>*****/#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int no,r,sum=0,i=0;
clrscr();
printf(“\nenter the number\n”);
scanf(“%d”,&no);
while(no>0)
{
r=no%2;
sum=sum+pow(10,i)*r;
no=no/2;
i++;
}
printf(“\nthe binary value is %d”,sum);
getch();
}
Output:
enter the number
8
the binary value is 1000
Sunday, October 7, 2012
Print all permutations of a given string
/******<soeasyprograms.blogspot.in>*****/
# include <conio.h>
/* Function to swap values at two pointers */
void swap (char *x, char *y)
{
char temp;
temp = *x;
*x = *y;
*y = temp;
}
/* Function to print permutations of string
This function takes three parameters:
1. String
2. Starting index of the string
3. Ending index of the string. */
void permute(char *a, int i, int n)
{
int j;
if (i == n)
printf("%s\n", a);
else
{
for (j = i; j <= n; j++)
{
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j)); //backtrack
}
}
}
/* Driver program to test above functions */
int main()
{
char a[] = "ABC";
permute(a, 0, 2);
getchar();
return 0;
}
Bubble sort in string array
/******<soeasyprograms.blogspot.in>*****/
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX 50
#define N 2000
void sort_words(char *x[], int y);
void swap(char **, char **);
int main(void)
{
char word[MAX];
char *x[N];
int n = 0;
int i = 0;
for(i = 0; scanf("%s", word) == 1; ++i)
{
if(i >= N)
printf("Limit reached: %d\n", N), exit(1);
x[i] = calloc(strlen(word)+1, sizeof(char));
strcpy(x[i], word);
}
n = i;
sort_words(x, n);
for(i = 0; i < n; ++i)
printf("%s\n", x[i]);
return(0);
}
void sort_words(char *x[], int y)
{
int i = 0;
int j = 0;
for(i = 0; i < y; ++i)
for(j = i + 1; j < y; ++j)
if(strcmp(x[i], x[j]) > 0)
swap(&x[i], &x[j]);
}
void swap(char **p, char **q)
{
char *tmp;
tmp = *p;
*p = *q;
*q = tmp;
}
#include <conio.h>
#include <string.h>
#define MAX 50
#define N 2000
void sort_words(char *x[], int y);
void swap(char **, char **);
int main(void)
{
char word[MAX];
char *x[N];
int n = 0;
int i = 0;
for(i = 0; scanf("%s", word) == 1; ++i)
{
if(i >= N)
printf("Limit reached: %d\n", N), exit(1);
x[i] = calloc(strlen(word)+1, sizeof(char));
strcpy(x[i], word);
}
n = i;
sort_words(x, n);
for(i = 0; i < n; ++i)
printf("%s\n", x[i]);
return(0);
}
void sort_words(char *x[], int y)
{
int i = 0;
int j = 0;
for(i = 0; i < y; ++i)
for(j = i + 1; j < y; ++j)
if(strcmp(x[i], x[j]) > 0)
swap(&x[i], &x[j]);
}
void swap(char **p, char **q)
{
char *tmp;
tmp = *p;
*p = *q;
*q = tmp;
}
Concatenate Two Strings
/*****<soeasyprograms.blogspot.in>*****/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char c[100];
char a[50];
char b[50];
clrscr();
printf("Enter a string1:");
gets(a);
printf("Enter a string2:");
gets(b);
strcat( a,b);
printf("%s",a);
getch();
}
Thursday, October 4, 2012
Count no. of students above,below and average students
/******<soeasyprograms.blogspot.in>*****/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int aa=0,ba=0,ae=0,i;
clrscr();
printf("Enter the marks:\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
if(a[i]>55)
aa++;
else if(a[i]<55)
ba++;
else
ae++;
}
printf("No. OF AVG STUDENTS ARE:%d\n",ae);
printf("No. OF ABOVE AVERAGE STUDENTS:%d\n",aa);
printf("No. OF BELOW AVERAGE STUDENTS ARE:%d",ba);
getch();
}
#include<conio.h>
void main()
{
int a[10];
int aa=0,ba=0,ae=0,i;
clrscr();
printf("Enter the marks:\n");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
if(a[i]>55)
aa++;
else if(a[i]<55)
ba++;
else
ae++;
}
printf("No. OF AVG STUDENTS ARE:%d\n",ae);
printf("No. OF ABOVE AVERAGE STUDENTS:%d\n",aa);
printf("No. OF BELOW AVERAGE STUDENTS ARE:%d",ba);
getch();
}
Saturday, September 22, 2012
WAP to count occurrences of values in an array
/*********<soeasyprograms.blogspot.in>***********/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void print_arr(int grades[], int elements);
int count_passes(int grades[], int elements,int value);
int main(void)
{
int grades[10] = {70,80,95,65,35,85,54,78,45,68};
int result;
print_arr(grades,10);
result = count_passes(grades,10,70);
if(result == 1)
printf("There was %d pass.\n",result);
else
printf("There were %d passes.\n",result);
return 0;
}
void print_arr(int grades[], int elements)
{
int i;
for(i = 0;i < elements;i++)
{
printf("%d ",grades[i]);
}
printf("\n");
}
int count_passes(int grades[], int elements,int value)
{
int i ,passes = 0 ;
for(i = 0;i < elements;i++)
{
if(grades[i] >= value)
passes++;
}
return(passes);
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void print_arr(int grades[], int elements);
int count_passes(int grades[], int elements,int value);
int main(void)
{
int grades[10] = {70,80,95,65,35,85,54,78,45,68};
int result;
print_arr(grades,10);
result = count_passes(grades,10,70);
if(result == 1)
printf("There was %d pass.\n",result);
else
printf("There were %d passes.\n",result);
return 0;
}
void print_arr(int grades[], int elements)
{
int i;
for(i = 0;i < elements;i++)
{
printf("%d ",grades[i]);
}
printf("\n");
}
int count_passes(int grades[], int elements,int value)
{
int i ,passes = 0 ;
for(i = 0;i < elements;i++)
{
if(grades[i] >= value)
passes++;
}
return(passes);
}
WAP convert a Roman numeral to its decimal equivalent
/*********<soeasyprograms.blogspot.in>***********/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
int *a,len,i,j,k;
char *rom;
clrscr();
printf("Enter the Roman Numeral:");
scanf("%s",rom);
len=strlen(rom);
for(i=0;i<len;i++)
{
if(rom[i]=='I')
a[i]=1;
else if(rom[i]=='V')
a[i]=5;
else if(rom[i]=='X')
a[i]=10;
else if(rom[i]=='L')
a[i]=50;
else if(rom[i]=='C')
a[i]=100;
else if(rom[i]=='D')
a[i]=500;
else if(rom[i]=='M')
a[i]=1000;
else
{
printf("\nInvalid Value");
getch();
exit(0);
}
}
k=a[len-1];
for(i=len-1;i>0;i--)
{
if(a[i]>a[i-1])
k=k-a[i-1];
else if(a[i]==a[i-1] || a[i]<a[i-1])
k=k+a[i-1];
}
printf("\nIts Decimal Equivalent is:");
printf("%d",k);
getch();
}
Decimal to Binary Conversion
/*********<soeasyprograms.blogspot.in>***********/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int no,r,sum=0,i=0;
clrscr();
printf(“\nenter the number\n”);
scanf(“%d”,&no);
while(no>0)
{
r=no%2;
sum=sum+pow(10,i)*r;
no=no/2;
i++;
}
printf(“\nthe binary value is %d”,sum);
getch();
}
Output:
enter the number
8 the binary value is 100
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int no,r,sum=0,i=0;
clrscr();
printf(“\nenter the number\n”);
scanf(“%d”,&no);
while(no>0)
{
r=no%2;
sum=sum+pow(10,i)*r;
no=no/2;
i++;
}
printf(“\nthe binary value is %d”,sum);
getch();
}
Output:
enter the number
8 the binary value is 100
Thursday, September 13, 2012
Greatest of two Numbers - Conditional Operator
/*******<soeasyprograms.blogspot.in>********/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter the 2 numbers”);
scanf(“%d%d”,&a,&b);
(a>b?printf(“a is greater”):printf(“b is greater”));
getch();
}
Output:
enter the two numbers
6
3 a is greater
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter the 2 numbers”);
scanf(“%d%d”,&a,&b);
(a>b?printf(“a is greater”):printf(“b is greater”));
getch();
}
Output:
enter the two numbers
6
3 a is greater
A simple example showing some comparison operators
/*******<soeasyprograms.blogspot.in>********/
#include<stdio.h>
#include<conio.h>
int main()
{
int number1 , number2;
printf("Enter the number1 number to compare.\n");
scanf("%d",&number1);
printf("Enter the number2 number to compare.\n");
scanf("%d",&number2);
printf("number1 > number2 has the value %d\n", number1 > number2);
printf("number1 < number2 has the value %d\n", number1 < number2);
printf("number1 == number2 has the value %d\n", number1 == number2);
return 0;
}
#include<stdio.h>
#include<conio.h>
int main()
{
int number1 , number2;
printf("Enter the number1 number to compare.\n");
scanf("%d",&number1);
printf("Enter the number2 number to compare.\n");
scanf("%d",&number2);
printf("number1 > number2 has the value %d\n", number1 > number2);
printf("number1 < number2 has the value %d\n", number1 < number2);
printf("number1 == number2 has the value %d\n", number1 == number2);
return 0;
}
Monday, August 27, 2012
Calculate Electric Energy Bill
/*****<soeasyprograms.blogspot.in>*****/
#include<stdio.h>
#include<conio.h>
void main()
{
float r,a=2.5,b=3.5,c=1.5;
clrscr();
printf(“enter the readings\n”);
scanf(“%f”,&r);
if(r>=200)
printf(“rupees=%f”,r*b);
else if((r>=100)&&(r<200))
printf(“rupees=%f”,r*a);
else
printf(“rupees=%f”,r*c);
getch();
}
Output:
enter the readings
140
rupees=350
#include<stdio.h>
#include<conio.h>
void main()
{
float r,a=2.5,b=3.5,c=1.5;
clrscr();
printf(“enter the readings\n”);
scanf(“%f”,&r);
if(r>=200)
printf(“rupees=%f”,r*b);
else if((r>=100)&&(r<200))
printf(“rupees=%f”,r*a);
else
printf(“rupees=%f”,r*c);
getch();
}
Output:
enter the readings
140
rupees=350
Construct Pyramid of Numbers
/*****<soeasyprograms.blogspot.in>*****/
/* Write a C program to construct a pyramid of numbers. */
/* Write a C program to construct a pyramid of numbers. */
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,y,x=35;
clrscr();
printf("\nEnter the number to generate the pyramid:\n");
scanf("%d",&num);
for(y=0;y<=num;y++)
{
/*(x-coordinate,y-coordinate)*/
gotoxy(x,y+1);
/*for displaying digits towards the left and right of zero*/
for(i=0-y;i<=y;i++)
printf("%3d",abs(i));
x=x-3;
}
getch();
}ARRANGE THE ELEMENTS IN ARRAY IN DESSENDING ORDER
/*****<soeasyprograms.blogspot.in>*****/
{
int a[100],i,n,j,search,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;i
scanf("%d",&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
getch();
}
#include<stdio.h>
#include<conio.h>
main()#include<conio.h>
{
int a[100],i,n,j,search,temp;
printf("\n how many no's in array");
scanf("%d",&n);
printf("\n enter %d elements in array",n);
for(i=0;i
scanf("%d",&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
printf("%4d",a[i]);
}
getch();
}
Thursday, May 3, 2012
Whether the given no. Is palindrome or not
/*****<soeasyprograms.blogspot.in>*****/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,t,sum=0;
clrscr();
printf(" OUTPUT:\n");
printf("\tEnter a no.");
scanf("%d",&n);
t=n;
while(n!=0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
if(sum==t)
printf("\tThe no. %d is a pallindrome",t);
else
printf("\tThe no. %d is not a pallindrome",t);
getch();
}
Output of this Program is:
Dont forget to give any Suggestion or Comments
Whether the given no. is armstrong or not
/*****<soeasyprograms.blogspot.in>*****/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,t,sum=0;
clrscr();
printf(" OUTPUT :\n");
printf("\tEnter a no.");
scanf("%d",&n);
t=n;
while(n!=0)
{
r=n%10;
sum=sum+r*r*r;
n=n/10;
}
if(sum==t)
printf("The no. %d is armstrong",t);
else
printf("The no. %d is not an armstrong",t);
getch();
}
Output of this Program is:
Dont forget to give any Suggestion or Comments
Program in C to Concatenation of two Strings
/*****<soeasyprograms.blogspot.in>*****/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char c[100];
char a[50];
char b[50];
clrscr();
printf("Enter a string1:");
gets(a);
printf("Enter a string2:");
gets(b);
strcat( a,b);
printf("%s",a);
getch();
}
Output of this Program is:
Dont forget to give comments
Write a program to check whether the year is leap or not
Subscribe to:
Posts (Atom)