Write a program (WAP) that will take N numbers as inputs and compute their average.
(Restriction: Without using any array)
Sample inputSample output
3
10 20 30.5 AVG of 3 inputs: 20.166667
2
22.4 11.1 AVG of 2 inputs: 16.750000
>
#include<stdio.h>
int main()
{
int i,n;
float a,sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%f",&a);
sum+=a;
}
printf("AVG of %d inputs: %f",n,sum/n);
return 0;
}
Write a program (WAP) that will take two numbers X and Y as inputs. Then it will print the square of X and increment (if XY) X by 1, until X reaches Y. If and when X is equal to Y, the program prints “Reached!”
Sample input(X,Y)Sample output
10 5 100, 81, 64, 49, 36, Reached!
5 10 25, 36, 49, 64, 81, Reached!
10 10 Reached!
Write a program (WAP) for the described scenario:
Player-1 picks a number X and Player-2 has to guess that number within N tries.
For each wrong guess by Player-2, the program prints “Wrong, N-1 Choice(s) Left!”
If Player-2 at any time successfully guesses the number, the program prints “Right,
Player-2 wins!” and terminates right away. Otherwise after the completion of N wrong
tries, the program prints “Player-1 wins!” and halts.
(Hint: Use break/continue)Sample input(X,N,n1, n2,..,nN)Sample output
5
3
12 8 5 Wrong, 2 Choice(s) Left!
Wrong, 1 Choice(s) Left!
Right, Player-2 wins!
100
5
50 100 Wrong, 4 Choice(s) Left!
Right, Player-2 wins!
20
3
12 8 5 Wrong, 2 Choice(s) Left!
Wrong, 1 Choice(s) Left!
Wrong, 0 Choice(s) Left!
Player-1 wins!
Write a program (WAP) that will run and show keyboard inputs until the user types an ’A’ at the keyboard.
Sample inputSample output
X Input 1: X
1 Input 2: 1
a Input 3: a
A
#include<stdio.h>
int main()
{
char ch;
int i=0;
while(scanf(" %c",&ch)==1){
i++;
if(ch=='A')break;
printf("Input %d: %c\n",i,ch);
}
}
Write a program (WAP) that will give the sum of first Nth terms for the following series.
1, -2, 3, -4, 5, -6, 7, -8, 9, -10, 11, -12, 13, -14, …….
Sample inputSample output
2 Result: -1
3 Result: 2
4 Result: -2
#include<stdio.h>
int main()
{
int i,n,sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++){
if(i%2!=0){
sum+=i;
}
else
sum-=i;
}
printf("Result: %d",sum);
return 0;
}