0% found this document useful (0 votes)
12 views

Subjective Assignment 05 Forloop Ans

The document provides solutions to 10 programming exercises involving loops in C language. The exercises cover printing patterns like numbers, alphabets and their sums using for loops. Concepts like even/odd numbers, multiplication tables and counting digits in a number are explained through examples.

Uploaded by

Yash rai
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)
12 views

Subjective Assignment 05 Forloop Ans

The document provides solutions to 10 programming exercises involving loops in C language. The exercises cover printing patterns like numbers, alphabets and their sums using for loops. Concepts like even/odd numbers, multiplication tables and counting digits in a number are explained through examples.

Uploaded by

Yash rai
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/ 10

Assignment of Subjective questions

1. Write a C program to print all natural numbers from 1 to n using loop.

Example

Input

Input upper limit: 10


Output

Natural numbers from 1 to 10: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Solution -

#include <stdio.h>
int main()
{
int i, n;

/* Input upper limit from user */


printf("Enter any number: ");
scanf("%d", &n);

printf("Natural numbers from 1 to %d : \n", n);

/*
* Start loop counter from 1 (i=1) and go till n (i<=n)
* increment the loop count by 1 to get the next value.
* For each repetition print the value of i.
*/
for(i=1; i<=n; i++)
{
printf("%d\n", i);
}

return 0;
}
2. Write a C program to print all natural numbers in reverse from n to 1 using for
loop.

Example

Input

Input N: 10
Output

Natural numbers from 10-1 in reverse:


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

Solution -

#include <stdio.h>

int main()
{
int i, start;

/* Input start range from user */


printf("Enter starting value: ");
scanf("%d", &start);

/*
* Run loop from 'start' to 1 and
* decrement 1 in each iteration
*/
for(i=start; i>=1; i--)
{
printf("%d\n", i);
}

return 0;
}
3. Write a C program to print alphabets from a to z using for loop.

Example

Input

Output

Alphabets: a, b, c, ... , x, y, z

Solution -

#include <stdio.h>

int main()
{
char ch;

printf("Alphabets from a - z are: \n");


for(ch='a'; ch<='z'; ch++)
{
printf("%c\n", ch);
}

return 0;
}

4. Write a C program to print all even numbers from 1 to n using for loop.

Example

Input

Input upper range: 10


Output

Even numbers between 1 to 10:


2, 4, 6, 8, 10
Solution -

#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;
}
5. Write a C program to print all odd numbers from 1 to n using for loop.

Example

Input

Input upper limit: 10


Output

Odd numbers between 1 to 10:


1, 3, 5, 7, 9

Solution -

#include <stdio.h>

int main()
{
int i, n;

/* Input upper limit from user */


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

printf("All odd numbers from 1 to %d are: \n", n);

/* Start loop from 1 and increment it by 1 */


for(i=1; i<=n; i++)
{
/* If 'i' is odd then print it */
if(i%2!=0)
{
printf("%d\n", i);
}
}

return 0;
}
6. Write a C program to find the sum of all natural numbers between 1 to n using
for loop.

Example

Input

Input upper limit: 10


Output

Sum of natural numbers 1-10: 55

Solution -

#include <stdio.h>

int main()
{
int i, n, sum=0;

/* Input upper limit from user */


printf("Enter upper limit: ");
scanf("%d", &n);

/* Find sum of all numbers */


for(i=1; i<=n; i++)
{
sum += i;
}

printf("Sum of first %d natural numbers = %d", n, sum);

return 0;
}
7. Write a C program to input number from user and find sum of all even numbers
between 1 to n.

Example

Input

Input upper limit of even number: 10


Output

Sum of even numbers between 1 to 10: 30

Solution -

#include <stdio.h>

int main()
{
int i, n, sum=0;

/* Input upper limit from user */


printf("Enter upper limit: ");
scanf("%d", &n);

for(i=2; i<=n; i+=2)


{
/* Add current even number to sum */
sum += i;
}

printf("Sum of all even number between 1 to %d = %d", n, sum);

return 0;
}
8. Write a C program to find sum of all odd numbers from 1 to n using for loop.

Example

Input

Input upper limit: 10


Output

Sum of odd numbers from 1-10: 25

Solution -

#include <stdio.h>

int main()
{
int i, n, sum=0;

/* Input range to find sum of odd numbers */


printf("Enter upper limit: ");
scanf("%d", &n);

/* Find the sum of all odd number */


for(i=1; i<=n; i+=2)
{
sum += i;
}

printf("Sum of odd numbers = %d", sum);

return 0;
}
9. Write a C program to input a number from user and print multiplication table of
the given number using for loop.

Example

Input

Input num: 5
Output

5*1 =5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50

Solution -

#include <stdio.h>

int main()
{
int i, num;

/* Input a number to print table */


printf("Enter number to print table: ");
scanf("%d", &num);

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


{
printf("%d * %d = %d\n", num, i, (num*i));
}

return 0;
}
10. Write a C program to input a number from user and count number of digits in
the given integer using loop.

Example

Input

Input num: 35419


Output

Number of digits: 5

Solution -

#include <stdio.h>

int main()
{
long long num;
int count = 0;

/* Input number from user */


printf("Enter any number: ");
scanf("%lld", &num);

/* Run loop till num is greater than 0 */


do
{
/* Increment digit count */
count++;

/* Remove last digit of 'num' */


num /= 10;
} while(num != 0);

printf("Total digits: %d", count);

return 0;
}

You might also like