Académique Documents
Professionnel Documents
Culture Documents
#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;
}
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);
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;
}
}
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);
#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;
#include <stdio.h>
2.
3.
4.
5.
int main()
6.
7.
8.
9.
10.
scanf("%d", &num);
11.
result = sum(num);
12.
13.
14.
return 0;
}
15.
16.
17.
18.
if (num != 0)
19.
20.
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.
{
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
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
#include <stdio.h>
2.
#define MAXSIZE 10
3.
4.
void main()
5.
6.
int array[MAXSIZE];
7.
8.
9.
10.
scanf("%d", &num);
11.
12.
13.
14.
scanf("%d", &array[i]);
15.
16.
17.
18.
19.
printf("%d\n", array[i]);
20.
21.
22.
23.
24.
25.
26.
27.
28.
temp = array[j];
29.
30.
array[j + 1] = temp;
31.
32.
33.
34.
35.
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.
11.
scanf("%d",&NUM);
12.
printf("\nSELECT CONVERSION");
13.
14.
15.
16.
17.
scanf("%d",&CH);
18.
switch(CH)
19.
20.
case 1 : binary(NUM);
21.
22.
break;
23.
case 2 : octal(NUM);
24.
25.
break;
26.
case 3 : hexa(NUM);
27.
28.
break;
29.
30.
default :
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.
56.
exit(0);
57.
58.
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.
79.
exit(0);
80.
81.
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.
102.
exit(0);
103.
104.
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();
}
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];
}