0% found this document useful (0 votes)
86 views9 pages

Exp 4 Lab

The document contains 6 programming problems involving loop control structures in C language. Each problem contains the aim, algorithm, program code and output. The problems include printing odd numbers between 1 to N using for loop, finding sum of even numbers between 1 to N using loops, calculating factorial of a number using for loop, reversing a number using while loop, checking if a number is Armstrong number using while loop, and printing a multiplication pattern using for loop.

Uploaded by

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

Exp 4 Lab

The document contains 6 programming problems involving loop control structures in C language. Each problem contains the aim, algorithm, program code and output. The problems include printing odd numbers between 1 to N using for loop, finding sum of even numbers between 1 to N using loops, calculating factorial of a number using for loop, reversing a number using while loop, checking if a number is Armstrong number using while loop, and printing a multiplication pattern using for loop.

Uploaded by

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

Ex.No.4 USAGE OF LOOP CONTROL STRUCTURES Reg.

No:
01.12.2021 URK21CS7046
1) Write a C program to print all odd numbers between 1 to N. - using for loop

Aim: To print all odd numbers between 1 to N. - using for loop.

Algorithm

Step 1: Start the Program.


Step 2: Declare the required variables i , n.
Step 3: Get the value of i & n from user.
Step 4: Use for loop and if condition to print the odd numbers.
Step 5: Display odd number s from i – n.
Step 6: Stop the Program.

Program

#include <stdio.h>
int main () {
int i, n;
printf("Enter number range: ");
scanf("%d", &n);
printf("All odd numbers from 1 to %d is: \n", n);
for(i=1; i<=n; i++)
{
if (i%2! =0)
{
printf("%d\n", i);
}
}
return 0;
}

Output

1
Result:

The program is executed successfully and the odd numbers between 1 - N is printed in the screen
using input and output functions.

2
1)
2) Write C program to find sum of even numbers between 1 to n - using while, do...while, for loop.

Aim: To find sum of even numbers between 1 to n - using while, do...while, for loop.

Algorithm

Step 1: Start the Program.


Step 2: Declare the required variables i , n , sum.
Step 3: Get the value of i & n from user.
Step 4: Use for loop and increment the loop by 2 the add the numbers to ‘sum’.
Step 5: Display sum.
Step 6: Stop the Program.

Program

#include <stdio.h>

int main () {
int i, n, sum=0;
printf("Enter limit: ");
scanf("%d", &n);
for(i=2; i<=n; i+=2)
{
sum += i;
}
printf("Sum of all even number between 1 to %d = %d", n, sum);
return 0;
}

Output

Result:

The program is executed successfully and the sum of all even numbers between 1 - N is printed in the
screen using input and output functions.

1
1)
3) Write a C program to find the factorial of the given number.

Aim: To find the factorial of the given number.

Algorithm

Step 1: Start the Program.


Step 2: Declare the required variables i , f , num.
Step 3: Get the value num from user.
Step 4: Use for loop and increment the value of f = f*i inside the for loop.
Step 5: Display f i.e., factorial.
Step 6: Stop the Program.

Program

#include<stdio.h>
int main () {
int i, f=1, num;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1; i<=num;i++)
f=f*i;
printf("Factorial of %d is: %d",num,f);
return 0;
}

Output

Result:

The program is executed successfully and the factorial of N numbers are printed in the screen using
input and output functions.

1
4) Write a C program to enter a number and print the reverse. Using while Loop

Aim: To enter a number and print the reverse. Using while Loop.

Algorithm

Step 1: Start the Program.


Step 2: Declare the required variables n, reverse, rem.
Step 3: Get the value of n from user.
Step 4: Use while loop and divide the n by 10 give reverse equals to 0*10+ rem increment it after
each loop so assign these values inside the for loop.
Step 5: Display reverse.
Step 6: Stop the Program.

Program

#include<stdio.h>
int main () {
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while (n! =0) {
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
return 0;
}

Output

Result:

The program is executed successfully and the reverse of the given number is printed in the screen
using input and output functions.

1
2
5) Write a C program to check whether the given number is Armstrong or not.

Aim: To check whether the given number is Armstrong or not.

Algorithm

Step 1: Start the Program.


Step 2: Declare the required variables n,r,sum=0, temp & get the value of n from user.
Step 3: Use while loop and inside the while loop assign the value for r, sum, n.
Step 4: Using If/else condition Display the given number is Amstrong or not.
Step 5: Stop the Program.

Program

#include<stdio.h>
int main () {
int n,r,sum=0, temp;
printf("URK21CS7046 KARTHIK M");
printf("\nenter the number=");
scanf("%d",&n);
temp=n;
while(n>0) {
r=n%10;
sum=sum+(r*r*r);
n=n/10;
}
if(temp==sum)
printf("Armstrong number ");
else
printf("not Armstrong number");
return 0;
}
Output

Result:

The program is executed successfully and the reverse of the given number is printed in the screen
using input and output functions.

1
2
6) Write a C program to print the following pattern up to the given range.

1 * 1 =1
2* 2=4
3* 3 = 9

Aim: To print the above pattern up to given numbers.

Algorithm

Step 1: Start the Program.


Step 2: Use for loop directly and print the above pattern using the formula i*i.
Step 3: Display the above pattern.
Step 4: Stop the Program.

Program

#include <stdio.h>
int main () {
int i;
for (i = 1; i < 4; ++i)
{
printf("%d * %d = %d \n", i , i , i * i);;
}
return 0;
}

Output

Result:

The program is executed successfully and the above pattern is displayed up to given n number in
screen using input and output functions.

You might also like