Professional Documents
Culture Documents
Exp 4 Lab
Exp 4 Lab
No:
01.12.2021 URK21CS7046
1) Write a C program to print all odd numbers between 1 to N. - using for loop
Algorithm
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
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.
Algorithm
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
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.
Algorithm
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
Algorithm
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.