0% found this document useful (0 votes)
22 views7 pages

Review Questions 8 (Looping 1)

The document contains 7 examples of C programs that use looping statements like for loops and while loops to print patterns, find sums, read user input, etc. The examples include programs to: 1) Print the multiplication table of a number entered by the user; 2) Display and find the sum of the first 10 natural numbers; 3) Read 10 numbers from the user and find their sum and average.

Uploaded by

asdfasdfas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views7 pages

Review Questions 8 (Looping 1)

The document contains 7 examples of C programs that use looping statements like for loops and while loops to print patterns, find sums, read user input, etc. The examples include programs to: 1) Print the multiplication table of a number entered by the user; 2) Display and find the sum of the first 10 natural numbers; 3) Read 10 numbers from the user and find their sum and average.

Uploaded by

asdfasdfas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Looping Questions / Examples

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.

The first natural numbers


1, 2, 3, 4, 5, 6, 7, 8, 9, 10

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");
}

2. Write a C program to find the sum of first 10 natural numbers

The first natural numbers


1, 2, 3, 4, 5, 6, 7, 8, 9, 10

The sum of first natural numbers


1+ 2+ 3+ 4+ 5+ 6+ 7+ 8+ 9+ 10
Answer;

#include <stdio.h>
int main()
{
int j, sum = 0;

printf("The first 10 natural number is :\n");

for (j = 1; j <= 10; j++)


{
sum = sum + j;
printf("%d ",j);
}
printf("\nThe Sum is : %d\n", sum);
}

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>

//function to check leap year


int checkLeapYear(int year)
{
if( (year % 400==0)||(year%4==0 && year%100!=0) )
return 1;
else
return 0;
}

int main()
{
int i,n;

printf("Enter the value of N: ");


scanf("%d",&n);

printf("Leap years from 1 to %d:\n",n);


for(i=1;i<=n;i++)
{
if(checkLeapYear(i))
printf("%d\t",i);
}

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;

/* Input upper limit of even number from user */


printf("Print all even numbers till: ");
scanf("%d", &n);

printf("Even numbers from 1 to %d are: \n", 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);

if(age>=0 && age<=5)


cnt_baby++;
else if(age>=6 && age<=17)
cnt_school++;
else
cnt_adult++;

//increase counter
count++;
}

printf("Baby age: %d\n",cnt_baby);


printf("School age: %d\n",cnt_school);
printf("Adult age: %d\n",cnt_adult);

return 0;
}

You might also like