Vous êtes sur la page 1sur 16

C program to print Armstrong numbers from 1 to 500

#include<stdio.h>
int main(){
int num,r,sum,temp;
for(num=1;num<=500;num++){
temp=num;
sum = 0;

while(temp!=0){
r=temp%10;
temp=temp/10;
sum=sum+(r*r*r);
}
if(sum==num)
printf("%d ",num);

return 0;
}

Program to print table of the numbers from 1 to 10


//Program to print table of the numbers from 1 to 10
#include<stdio.h>
#include<conio.h>
int main()
{
int row, col;
clrscr();
printf("Table of 1 to 10 is as\n");
for(row=1;row<=10;row++)
{
for(col=1;col<=10;col++)
{
printf("%4d",row*col);
}
printf("\n");
}
getch();
return 0;
}

Program to print table between the given range of integers


//Program to print table between the given range
#include<stdio.h>
#include<conio.h>
int main()
{
int row, col,y,k;
clrscr();
printf("Enter the two numbers for getting the table between their range\n");
scanf("%d%d",&row,&col);
k=row;

printf("Table as\n");
for(y=1;y<=10;y++)
{
for(row;row<=col;row++)
{
printf("%4d",row*y);
}
row=k;
printf("\n");
}
getch();
return 0;
}

Accept characters till the user enters EOF and count the number of lines entered. Also
display the length of the longest line.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str[100];
int i = 0, l = 0, f = 1;
clrscr();
puts("Enter any string\n");
gets(str);
for(i = 0; str[i] !='\0'; i++)
{
l = l + 1;
}
printf("The number of characters in the string are %d\n", l);
for(i = 0; i <= l-1; i++)
{
if(str[i] == ' ')
{
f = f + 1;
}
}
printf("The number of words in the string are %d", f);
getch();
}

/**
* C program to print all Perfect numbers between 1 to n
*/
#include <stdio.h>
int main()
{
int i, j, n, sum = 0;
/* Reads upper limit to print perfect numbers upto */
printf("Enter any number to print perfect number up to: ");
scanf("%d", &n);

printf("\nAll Perfect numbers between 1 to %d:\n", n);


/*
* Iterates from 1 to n and print if it is perfect number
*/
for(i=1; i<=n; i++)
{

sum = 0;
/*
* Checks whether the current number i is Perfect number or not
*/
for(j=1; j<i; j++)
{
if(i%j==0)
{
sum += j;
}
}

/* If the current number i is Perfect number */


if(sum == i)
{
printf("%d is Perfect Number\n", i);
}

return 0;
}
Write a program, which accepts a character from the user and checks if it is an alphabet, digit
or punctuation symbol. If it is an alphabet, check if it is uppercase or lowercase and then change
the case.

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("\n enter the character");
scanf("\n %c",&ch);

if(ch>=65 && ch<=90 || ch>=97 && ch<=122)


{
printf("\n the given character is alphabet");
if(ch>=65 && ch<=90)
{
printf("\n character is uppercase");
}
else
{
printf("\n character is lowercase");
}
}
if(ch>=48 && ch<=57)
{
printf(" the given character is digit");
}
if(ch>=58 && ch<=64 || ch>=91 && ch<=96)
{
printf("\n the given character is symbol");
}
getch();
}
/* output

enter the character B


the given character is alphabet
character is uppercase *
Write a function isEven, which accepts an integer as parameter and returns 1 if the number is
even, and 0 otherwise

#include<stdio.h>
#include<conio.h>
int iseven(int num);
void main()
{
int num,ans;
clrscr();
printf("\n enter the value of num");
scanf("\n %d",&num);
ans=iseven(num);
if(ans==1)
{
printf("\n number is even");
}
if(ans==0)
{
printf("\n number is odd");
}
getch();
}
int iseven(int num1)
{
if(num1%2==0)
{
return 1;
}
else
{
return 0;
}
}
/* output
enter the value of num7
number is odd */
Write a function isPrime, which accepts an integer as parameter and returns 1 if the number
is prime and 0 otherwise. Use this function in main to display the first 10 prime numbers

#include<stdio.h>
int main(){
int num,i,count=0;
printf("Enter a number: ");
scanf("%d",&num);

for(i=2;i<=num/2;i++){
if(num%i==0){
count++;
break;
}
}
if(count==0 && num!= 1)
printf("%d is a prime number",num);
else
printf("%d is not a prime number",num);
return 0;

#include <stdio.h>
int isPrime(int);
int main()
{
int n;
for (n = 100; n <= 500; n++)
{
if (isPrime(n))
{
printf("%d\t", n);
}
}
return 0;
}
int isPrime(int number)
{
int i;
for (i = 2; i < number; i++)
{
if (number % i == 0)
{
return 0;
}
}
return 1;
}
#include <stdio.h>
int main()
{
char ch;

/* Reads a character from user */


printf("Enter any character: ");
scanf("%c", &ch);
/* Checks if it is an alphabet */
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
{
printf("%c is ALPHABET.\n", ch);
}
else if(ch >= '0' && ch <= '9')
{
printf("%c is DIGIT.\n", ch);
}
else
{
printf("%c is SPECIAL CHARACTER.\n", ch);
}
return 0;
Write a recursive C function to calculate the sum of digits of a number. Use this function in
main to accept a number and print sum of its digits.
1.

#include <stdio.h>

2.
3.

int sum (int a);

4.
5.

int main()

6.

7.

int num, result;

8.
9.

printf("Enter the number: ");

10.

scanf("%d", &num);

11.

result = sum(num);

12.

printf("Sum of digits in %d is %d\n", num, result);

13.
14.

return 0;
}

15.
16.

int sum (int num)

17.

18.

if (num != 0)

19.

20.

return (num % 10 + sum (num / 10));

21.

22.

else

23.

24.
25.
26.

return 0;
}
}
27. Write a recursive C function to calculate the GCD of two numbers. Use this function in main.

The GCD is calculated as


#include <stdio.h>
int hcf(int n1, int n2);
int main()

{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1,n2));
return 0;
}
int hcf(int n1, int n2)
{
if (n2!=0)
return hcf(n2, n1%n2);
else
return n1;
}
Enter two positive integers: 366

60

G.C.D of 366 and 60 is 6.

3. Write a recursive function for the following recursive definition. Use this function in main to
display the first 10 numbers of the following series
#include <stdio.h>
int addNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Sum = %d",addNumbers(n));
return 0;
}
int addNumbers(int n)
{
if(n!=0)

return n+addNumbers(n-1);
else
return n;
}
Enter a positive integer: 20

Sum = 210

Write a recursive C function to calculate xy.


#include <stdio.h>
int power(int n1,int n2);
int main()
{
int base, exp;
printf("Enter base number: ");
scanf("%d",&base);
printf("Enter power number(positive integer): ");
scanf("%d",&exp);
printf("%d^%d = %d", base, exp, power(base, exp));
return 0;
}
int power(int base,int exp)
{
if ( exp!=1 )
return (base*power(base,exp-1));
}
Write a program to accept n numbers in the range of 1 to 25 and count the frequency of
occurrence of each number.
#include <stdio.h>
int main()
{
int arr[100], freq[100];
int size, i, j, count;
/*
* Read size of array and elements in array
*/
printf("Enter size of array: ");
scanf("%d", &size);

printf("Enter elements in array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
freq[i] = -1;
}
/*
* Counts frequency of each element
*/
for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
if(arr[i]==arr[j])
{
count++;
freq[j] = 0;
}
}
if(freq[i]!=0)
{
freq[i] = count;
}
}
/*
* Prints frequency of each element
*/
printf("\nFrequency of all elements of array : \n");
for(i=0; i<size; i++)
{
if(freq[i]!=0)
{
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}
return 0;
}
Write a program to accept n numbers and store all prime numbers in an array called prime.
Display this array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n;
clrscr();
printf("\nEnter the n number: ");
scanf("%d",&n);
printf("\nEnter the numbers: ");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}

printf("\nThe Prime Number is: ");


for(i=1;i<=n;i++)
{
if(a[i]%2==1)
{
printf("\t%d",a[i]);
}
}
getch();
}
Write a function to sort an array of n integers using Bubble sort method.
1.

#include <stdio.h>

2.

#define MAXSIZE 10

3.
4.

void main()

5.

6.

int array[MAXSIZE];

7.

int i, j, num, temp;

8.
9.

printf("Enter the value of num \n");

10.

scanf("%d", &num);

11.

printf("Enter the elements one by one \n");

12.

for (i = 0; i < num; i++)

13.

14.

scanf("%d", &array[i]);

15.

16.

printf("Input array is \n");

17.

for (i = 0; i < num; i++)

18.

19.

printf("%d\n", array[i]);

20.

21.

/* Bubble sorting begins */

22.

for (i = 0; i < num; i++)

23.

24.

for (j = 0; j < (num - i - 1); j++)

25.

26.

if (array[j] > array[j + 1])

27.

28.

temp = array[j];

29.

array[j] = array[j + 1];

30.

array[j + 1] = temp;

31.

32.

33.

34.

printf("Sorted array is...\n");

35.

for (i = 0; i < num; i++)

36.

37.

printf("%d\n", array[i]);

38.
39.

}
}

Write a program to accept a decimal number and convert it to binary, octal and hexadecimal.
Write separate functions.
1.

#include<stdio.h>

2.

FILE *FP=NULL;

3.

void main()

4.

5.

int CH,NUM;

6.

void octal(int);

7.

void binary(int);

8.

void hexa(int);

9.

clrscr();

10.

printf("ENTER DECIMAL NUMBER TO BE CONVERTED : \n");

11.

scanf("%d",&NUM);

12.

printf("\nSELECT CONVERSION");

13.

printf("\n 1. DECIMAL TO BINARY\n");

14.

printf("\n 2. DECIMAL TO OCTAL\n");

15.

printf("\n 3. DECIMAL TO HEXADECIMAL\n");

16.

printf("\nENTER CHOICE HERE :");

17.

scanf("%d",&CH);

18.

switch(CH)

19.

20.

case 1 : binary(NUM);

21.

printf("\nOUPUT WRITTEN TO OUTPUT.TXT");

22.

break;

23.

case 2 : octal(NUM);

24.

printf("\nOUPUT WRITTEN TO OUTPUT.TXT");

25.

break;

26.

case 3 : hexa(NUM);

27.

printf("\nOUPUT WRITTEN TO OUTPUT.TXT");

28.

break;

29.
30.

default :

printf("\nYOU HAVE ENTERED WRONG CHOICE !!!");

31.
32.

getch();

33.

34.

void hexa(int Y)

35.

36.

char HEXC[5];

37.

int NUM,I,LEN,HEXD[5];

38.

NUM=Y;

39.

LEN=0;

40.

while(Y>0)

41.

42.

HEXD[LEN]=Y%16;

43.

Y=Y/16;

44.

LEN++;

45.

};

46.

for(I=LEN-1;I>-1;I--)

47.

48.

if(HEXD[I]<10)

49.

HEXC[I]=HEXD[I]+48;

50.

else

51.

HEXC[I]=HEXD[I]+55;

52.

53.

if((FP=fopen("output.txt","a+t"))==NULL)

54.

55.

printf("FILE CAN'T BE OPENED OR CREATED\n");

56.

exit(0);

57.

58.

fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);

59.

for(I=LEN-1;I>-1;I--)

60.

61.

fprintf(FP,"%c",HEXC[I]);

62.

63.
64.

65.

void binary(int Y)

66.

67.

int NUM,I,LEN,BIN[20];

68.

NUM=Y;

69.

LEN=0;

70.

while(Y>0)

71.

72.

BIN[LEN]=Y%2;

73.

Y=Y/2;

74.

LEN++;

75.

};

76.

if((FP=fopen("output.txt","a+t"))==NULL)

77.

78.

printf("FILE CAN'T BE OPENED OR CREATED\n");

79.

exit(0);

80.

81.

fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);

82.

for(I=LEN-1;I>-1;I--)

83.

84.

fprintf(FP,"%d",BIN[I]);

85.

86.
87.

88.

void octal(int Y)

89.

90.

int NUM,I,LEN,OCT[5];

91.

NUM=Y;

92.

LEN=0;

93.

while(Y>0)

94.

95.

OCT[LEN]=Y%8;

96.

Y=Y/8;

97.

LEN++;

98.

};

99.

if((FP=fopen("output.txt","a+t"))==NULL)

100.

101.

printf("FILE CAN'T BE OPENED OR CREATED\n");

102.

exit(0);

103.

104.

fprintf(FP,"\nCONVERTED OCTAL EQUIVALENT VALUE OF %d IS \t",NUM);

105.

for(I=LEN-1;I>-1;I--)

106.

107.

fprintf(FP,"%d",OCT[I]);

108.
109.

}
}

SCREEN SHOTS : -

Write a program to find the union and intersection of the two sets of integers (store it in two
arrays).
#include
#include
void main()
{
int i,k=0,x[10],y[10],c[25],j,n,n1,d[25],f=0;
clrscr();
printf(\n how many elements in SET 1:);
scanf(%d,&n);
for(i=0;i<n;i++)
{
printf("\n please enter the elements no %d",i+1);
scanf("%d",&x[i]);
}
printf("\n how many elements in set 2:");
scanf("%d",&n1);
for(i=0;i<n1;i++)
{
printf("\n please enter elements no %d",i+1);
scanf("%d",&y[i]);
}
for(i=0;i<n;i++)
{
c[k]=x[i];
k++;
}
for(i=0;i<n;i++)
{
for(j=0;j<n1;j++)
{
if(y[i]==x[j])
break;
}
if(j==n)
{
c[k]=y[i];
k++;

}
}
printf("\n the union set is:{");
for(i=0;i<k;i++)
printf("%d",c[i]);
printf("}\n");
for(j=0;j<n;j++)
{
for(i=0;i<n1;i++)
{
if(y[i]==x[j])
break;
}
if(i!=n1)
{
d[f]=y[i];
f++;
}
}
printf("\n the intersection set is :{");
for(i=0;i<f;i++)
printf("%d",d[i]);
printf("}");
getch();
}

Write a program to remove all duplicate elements from an array.


#include<stdio.h>
int main() {
int arr[20], i, j, k, size;
printf("\nEnter array size : ");
scanf("%d", &size);
printf("\nAccept Numbers : ");
for (i = 0; i < size; i++)
scanf("%d", &arr[i]);
printf("\nArray with Unique list : ");
for (i = 0; i < size; i++) {
for (j = i + 1; j < size;) {
if (arr[j] == arr[i]) {
for (k = j; k < size; k++) {
arr[k] = arr[k + 1];
}
size--;
} else
j++;
}
}
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}

return (0);
}

Write a program to accept a matrix A of size mXn and store its transpose in matrix B. Display
matrix B. Write separate functions.
#include <stdio.h>
int main()
{
int a[10][10], trans[10][10], r, c, i, j;
printf("Enter rows and column of matrix: ");
scanf("%d %d", &r, &c);
/* Storing element of matrix entered by user in array a[][]. */
printf("\nEnter elements of matrix:\n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("Enter elements a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* Displaying the matrix a[][] */
printf("\nEntered Matrix: \n");
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf("%d ",a[i][j]);
if(j==c-1)
printf("\n\n");
}
/* Finding transpose of matrix a[][] and storing it in array trans[][]. */
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
trans[j][i]=a[i][j];
}

/* Displaying the transpose,i.e, Displaying array trans[][]. */


printf("\nTranspose of Matrix:\n");
for(i=0; i<c; ++i)
for(j=0; j<r; ++j)
{
printf("%d ",trans[i][j]);
if(j==r-1)
printf("\n\n");
}
return 0;
}

Vous aimerez peut-être aussi