You are on page 1of 8

1) Write a program (WAP) that prompts the user to enter a

series of numbers, then writes the numbers in reverse order.


Further, use a macro to define the length of the array.
INPUT: #include <stdio.h>
#include <stdlib.h>
#define N 10

int main()
{
int arr[N];
int i;
printf("enter %d numbers:",N);
for (i =0; i<N; i++)
{
scanf("%d", &arr[i]);
}

printf("\n reverse order: ");


for(i = N-1; i>=0; i--)
{
printf("%d\t", arr[i]);
}

return 0;
}
OUTPUT: enter 10 numbers:12
13
14
15
16
17
18
19
10
11

reverse order: 11 10 19 18 17 16 15 14
13 12
Process returned 0 (0x0) execution time : 17.153 s
Press any key to continue.
2) Modify the program 1, and ask the user to enter how
many numbers do you want to reverse.
Use a variable length array (VLA) for writing the program.
INPUT: #include <stdio.h>
#include <stdlib.h>
#define MAX 100
int main()
{

int arr[MAX];
int i,n;
printf("enter how many numbers want to print:");
scanf("%d",&n);
printf("enter %d numbers:",n);
for(i=0; i<n; i++)
{
scanf("%d", &arr[i]);
}

printf("\n reverse order: ");


for(i = n-1; i>=0; i--)
{
printf("%d\t", arr[i]);
}

return 0;
}
OUTPUT: enter how many numbers want to print:5
enter 5 numbers:11
12
13
14
15

reverse order: 15 14 13 12 11
Process returned 0 (0x0) execution time : 16.121 s
Press any key to continue.
3) WAP that checks whether any of digits in a number appear
more than once. After the user enters
a number, the program prints either Repeated digit or No
repeated digit.
INPUT: #include <stdio.h>
#include <stdlib.h>

int main()
{
int num,rem,a[10]={0};
printf("enter a positive number:");
scanf("%d",&num);
while(num)
{
rem=num%10;
if(a[rem]==1)
break;
else
{
a[rem]=1;
}

num=num/10;
}
if(num)
printf("repeated digit");
else
printf("not a repeated digit");

return 0;
}
OUTPUT: enter a positive number:353
repeated digit
Process returned 0 (0x0) execution time : 3.229 s
Press any key to continue.
4) Modify the program 3, so that it shows which digits (if
any) were repeated
INPUT: #include<stdio.h>
int main()
{
int rem,num,a[10]={0}; //assigning 0 to all the elements
printf("enter a poitive number\n");
scanf("%d",&num);
printf("Repeated digits:\n");
while(num)
{
rem=num%10;
if(a[rem]==1)
printf("%d\t",rem);
else
a[rem]=1;

num=num/10;
}
return 0;

}
OUTPUT: enter a poitive number
553322299
Repeated digits:
9 2 2 3 5
Process returned 0 (0x0) execution time : 5.298 s
Press any key to continue.

5) Modify the program 3, so that it prints a table showing


how many times each digit appears in
the number:
INPUT: #include<stdio.h>

int main()
{
long long num,n;
int i,lastdigit;
int freq[10];
printf("enter any number:");
scanf("%lld",&num);
for(i=0;i<10;i++)
{
freq[i]=0;
}
n=num;
while(n!=0)
{
lastdigit=n%10;
n=n/10;
freq[lastdigit]++;
}
printf("digit:");
for(i=0;i<10;i++)
{
printf("%d\t",i);
}
printf("\n");
printf("occur:");
for(i=0;i<10;i++)
{
printf("%d\t",freq[i]);
}
return 0;

}
OUTPUT:
enter any number:224
digit:0 1 2 3 4 5 6 7 8 9
occur:0 0 2 0 1 0 0 0 0 0
Process returned 0 (0x0) execution time : 2.720 s
Press any key to continue.
6) Modify the program 3, so that the user can enter more
than one number to be tested for repeated
digits. The program should terminate when the user enters a
number that is less than or equal to 0.
INPUT: #include <stdio.h>
#include <stdlib.h>
int main()
{
while(1)
{
int num,rem,a[10]={0};

{
printf("enter a positive number:");
scanf("%d",&num);
if(num>0)
{
while(num)
{
rem=num%10;
if(a[rem]==1)
break;
else
a[rem]=1;

num=num/10;
}
if(num)
printf("repeated digit\n");
else
printf("not a repeated digit\n");
}
else
exit(0);
}
}
return 0;
}
OUTPUT:
enter a positive number:224
repeated digit
enter a positive number:345
not a repeated digit
enter a positive number:0

Process returned 0 (0x0) execution time : 15.481 s


Press any key to continue.

You might also like