Module 2 repository
This repository contains 5 C programs demonstrating fundamental concepts such as loops, functions, pattern printing, and basic number operations. Each program includes its Aim, Algorithm, and placeholders for Source Code and Sample Output.
This repository contains 5 C programs demonstrating concepts such as loops, pattern printing, functions, and basic number operations. Each program includes Aim and Algorithm.
- Print 1 to n Odd Numbers
- Hollow Square Pattern using For Loop
- Factorial of a Number using Function (Without Return Type, With Arguments)
- Check Palindrome using For Loop
- Multiplication Table using Do-While Loop
Aim: Print all odd numbers from 1 to a user-provided number n.
Algorithm:
- Ask the user to input a number
n. - Loop from 1 to
n. - Check if the current number is odd (
num % 2 != 0). - Print the odd numbers.
source code
#include<stdio.h>
int main()
{
int num,i;
scanf("%d",&num);
for(i=1;i<=num;i +=2){
printf("%d ",i);
}
return 0;
}Result:
The program displays all odd numbers from 1 up to the entered number n.
Aim: Print a hollow square pattern of size n using for loops.
Algorithm:
- Ask the user to input the size
nof the square. - Loop through rows (1 to n) and columns (1 to n).
- Print
*for the borders (first row, last row, first column, last column). - Print space for inner positions.
source code
#include<stdio.h>
int main()
{
int num,i,j;
scanf("%d",&num);
for(i=1;i<=num;i++){
for(j=1;j<=num;j++){
if(i==num||i==1||j==1||j==num){
printf("#");
}
else
printf(" ");
}
printf("\n")
}
return 0;
}output
Result:
The program successfully prints a hollow square pattern of stars (*) with the given size.
Aim: Find the factorial of a given number using a function without return type but with arguments.
Algorithm:
- Ask the user to input a number
n. - Create a function
factorial(int n)that calculates the factorial. - Use a loop inside the function to compute factorial.
- Print the factorial from within the function.
source code
#include<stdio.h>
void fact(int num)
{
int fact=1,i;
for(i=1;i<=num;i++)
{
fact=fact*i;
}
printf("Factorial value is: %d",fact);
}
int main()
{
int n;
scanf("%d",&n);
fact(n);
return 0;
}output
Result:
The program computes and displays the factorial of the entered number using a function with arguments and no return type.
Aim: Check if the input entered by the user is a palindrome.
Algorithm:
- Ask the user to input a number.
- Store the input in a variable.
- Use a
forloop to compare the characters/digits from start and end. - If all characters/digits match, it is a palindrome; else it is not.
source code
#include<stdio.h>
int main()
{
int num,rem;
int original;
int reversed=0;
scanf("%d",&num);
original = num;
for(;num!=0;num /= 10){
rem=num%10;
reversed = reversed*10+rem;
}
if(original==reversed){
printf("Palindrome Number");
}
else
{
printf("Not a Palindrome Number");
}
return 0;output
Result:
The program determines and displays whether the given number is a palindrome.
Aim: Print the multiplication table of a user-provided number using a do-while loop.
Algorithm:
- Ask the user to input a number
n. - Initialize a counter
i = 1. - Use a
do-whileloop to multiplynbyiand print the result. - Increment
iuntil it reaches 10.
source code
#include <stdio.h>
int main()
{
int num,i=1;
scanf("%d", &num);
do {
printf("%d ",num*i);
i++;
}
while(i<=num);
return 0;
}output
Result:
The program prints the complete multiplication table of the user-entered number from 1 to 10 using a do-while loop.
