Review Questions 8 (Looping 1)
Review Questions 8 (Looping 1)
1. Write a C Program which receives a positive integer from standard input stream
(terminal keyboard), and prints the multiplication table up to the entered integer to
the terminal display screen using iteration control statement.
Solution :-
#include<stdio.h>
int main()
{
int i,a,n;
printf("Enter The Number That You Want To Print Table \n");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
a=n*i;
printf("%d * %d = %d\n", n, i, n*i);
}
return 0;
}
1. Write a program in C to display the first 10 natural numbers.
Answer;
#include <stdio.h>
Int/void main()
{
int i;
printf("The first 10 natural numbers are:\n");
for (i=1;i<=10;i++)
{
printf("%d ",i);
}
printf("\n");
}
#include <stdio.h>
int main()
{
int j, sum = 0;
3. Write a program in C to read 10 numbers from keyboard and find their sum and
average.
Answer;
#include <stdio.h>
void main()
{
int i,n,sum=0;
float avg;
printf("Input the 10 numbers : \n");
for (i=1;i<=10;i++)
{
printf("Number-%d :",i);
scanf("%d",&n);
sum +=n;
}
avg=sum/10.0;
printf("The sum of 10 no is : %d\nThe Average is : %f\
n",sum,avg);
4. Program to print all lowercase alphabets from 'a' to 'z' using while loop in C
#include <stdio.h>
int main()
{
char alphabet;
alphabet='a';
printf("Lowercase alphabets:\n");
while(alphabet<='z')
{
printf("%c ",alphabet);
alphabet++;
}
return 0;
}
5. Print all Leap Years from 1 to N using C program
#include <stdio.h>
int main()
{
int i,n;
return 0;
}
6. C program to print all even numbers from 1 to n
/**
* C program to print all even numbers from 1 to n
*/
#include <stdio.h>
int main()
{
int i, n;
/*
* Start loop counter from 1, increment it by 1,
* will iterate till n
*/
for(i=1; i<=n; i++)
{
/* Check even condition before printing */
if(i%2 == 0)
{
printf("%d\n", i);
}
}
return 0;
}
7. C program to read age of 15 person and count total Baby age, School age and
Adult age
This program is asked through comment
Question
to read an age of 15 person & find out how many of them fall under :
a) Still a baby- age 0 to 5
b) Attending school - age 6 to 17
c) Adult life-age 18 & over
[using while loop]
#include <stdio.h>
int main()
{
int age;
int cnt_baby=0,cnt_school=0,cnt_adult=0;
int count=0;
while(count<15)
{
printf("Enter age of person [%d]: ",count+1);
scanf("%d",&age);
//increase counter
count++;
}
return 0;
}