C Programs for Number Manipulations
C Programs for Number Manipulations
[Link] :01
Date : 1.a. Swapping of two
numbers
with third variable.
AIM :
To create a c program Swapping of two numbers
with third variable.
Algorithm :
Step 1 : Start
Step 2 : Declare three integer variables a, b, and c
Step 3 : Read value of a
Step 4 : Read value of b
Step 5 : Display values of a and b before swapping
Step 6 : Assign c = a
Step 7 : Assign a = b
Step 8 : Assign b = c
Step 9 : Display values of a and b after swapping
Step 10 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int a,b,c;
clrscr();
printf("swapping of two numbers with third variable.\n");
printf("enter a=");
scanf("%d",&a);
printf("enter b=");
scanf("%d",&b);
printf("before swapping :\n");
printf("a=%d,b=%d\n",a,b);
c=a;
a=b;
b=c;
printf("after swapping :\n");
printf("a=%d,b=%d\n",a,b);
getch();
return 0;
}
Flowchart :
[Link] :01
Date : 1.b. Swapping of two
numbers
without third variable.
AIM :
To create a c program Swapping of two numbers
without third variable.
Algorithm :
Step 1 : Start
Step 2 : Declare three integer variables a, b, and c
Step 3 : Read value of a
Step 4 : Read value of b
Step 5 : Display values of a and b before swapping
Step 6 : compute a = a - b
Step 7 : compute a = a - b
Step 8 : compute a = a - b
Step 9 : Display values of a and b after swapping
Step 10 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int a,b;
clrscr();
printf("swapping of two numbers without third variable.\
n");
printf("enter a=");
scanf("%d",&a);
printf("enter b=");
scanf("%d",&b);
printf("before swapping :\n");
printf("a=%d,b=%d\n",a,b);
a=a-b;
b=a+b;
a=b-a;
printf("after swapping :\n");
printf("a=%d,b=%d\n",a,b);
getch();
return 0;
}
Flowchart :
[Link] :02
Date : Reversing a number.
AIM :
To create a c program reversing a number.
Algorithm :
Step 1 : Start
Step 2 : Declare integer variables num, rem, and
reverse and initialize reverse = 0
Step 3 : Read the number num
Step 4 : Display the original number
Step 5 : Repeat while num 0
Step 5.1 : Find remainder: rem = num % 10
Step 5.1 : Update reverse: reverse = (reverse ×10)
+ rem
Step 5.1 : Remove last digit: num = num / 10
Step 6 : Display the reversed number
Step 7 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int num,revers=0,rem;
clrscr();
printf("reversing a number.\n");
printf("enter a number:");
scanf("%d",&num);
printf("before revers the number : %d\n",num);
while(num!=0){
rem=num%10;
revers=revers*10+rem;
num=num/10;
}
printf("after revers the number : %d\n",revers);
getch();
return 0;
}
[Link] :03
Date : Number of digit and
display each digit of a number.
AIM :
To create a c program Number of digits in a number,
Displaying each digit of a number - Menu driven
program.
Algorithm :
Step 1 : Start
Step 2 : Read the number n
Step 3 : Store n in a temporary variable temp
Step 4 : Initialize count = 0
Step 5 : While temp > 0
ncrement count by 1
Divide temp by 10
Step 6 : Display the value of count (number of digits)
Step 7 : While n > 0
Find digit = n % 10
Display digit
Divide n by 10
Step 8 : Stop
Output :
Program :
#include <stdio.h>
#include <conio.h>
int main(){
int n, temp, count = 0, digit;
clrscr();
printf("find number of digit in given number\n");
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while(temp > 0){
count++;
temp = temp / 10; }
printf("Number of digits = %d\n", count);
printf("Digits are:\n");
while(n > 0){
digit = n % 10;
printf("%d\n", digit);
n = n / 10; }
getch();
return 0;
}
[Link] :04
Date : Factor of a number.
AIM :
To create a c program Factor of a number.
Algorithm :
Step 1 : Start
Step 2 : Declare integer variables num and i
Step 3 : Read the number num
Step 4 : Display message "Factors of the number"
Step 5 : For i = 1 to num, repeat:
If num % i == = 0, then print i
Step 6 : End loop
Step 7 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main() {
int num, i;
clrscr();
printf("find the given number factors.\n");
printf("given the number: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i) {
if (num % i == 0) {
printf("%d ", i);
}
}
printf("\n");
getch();
return 0;
}
[Link] :05
Date : 5.a. display natural
numbers from 1 to n.
AIM :
To create a c program display natural number from
1 to n.
Algorithm :
Step 1 : Start
Step 2. Input: Get the limit n from the user.
Step 3. Initialize: Set counter i = 1.
Step 4. Loop: While i < n:
Output: Display the value of i.
Increment: Set i = i +1.
Step 5. Stop.
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int n,i;
clrscr();
printf("display natural numbers 1 to n.\n");
printf("enter the value n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%2.2d ",i);
}
getch();
return 0;
}
[Link] :05
Date : 5.b. display natural
numbers from n to 1.
AIM :
To create a c program display natural number from
n to 1.
Algorithm :
Step 1 : Start
Step 2. Input: Get the starting limit n from the user.
Step 3. Initialize: Set counter i = n.
Step 4. Loop: While i≥ 1:
Output: Display the value of i.
Decrement: Set i = i - 1.
Step 5. Stop.
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int n,i;
clrscr();
printf("display natural numbers n to 1.\n");
printf("enter the value of n:");
scanf("%d",&n);
for(i=n;i>=1;i--){
printf("%2.2d ",i);
}
getch();
return 0;
}
[Link] :06
Date : Display alphabets from
a to z and z to a.
AIM :
To create a c program display alphabets form a to z
and z to a.
Algorithm :
Step 1 : Start
Step 2 : Declare a character variable ch
Step 3 : Display message "Alphabets from a to z"
Step 4 : For ch = 'a' to 'z', repeat:
Print ch
Step 5 : Print a new line
Step 6 : Display message "Alphabets from z to a"
Step 7 : For ch = 'z' to 'a', repeat:
Print ch
Step 8 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
char ch;
clrscr();
printf("display alphabets from a to z.\n");
printf("alphabets from a to z:");
for(ch='a';ch<='z';ch++){
printf("%c ",ch);
}
printf("\n");
printf("display alphabets from z to a.\n");
printf("alphabets from z to a:");
for(ch='z';ch>='a';ch--){
printf("%c ",ch);
}
getch();
return 0;
}
Flowchart :
[Link] :07
Date : Multiplication table.
AIM :
To create a c program multiplication table.
Algorithm :
Step 1 : Start
Step 2 : Declare two integer variables i and n
Step 3 : Read the number n
Step 4 : For i = 1 to 10, repeat:
* Print n × i = n*i
Step 5 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int i,n;
clrscr();
printf("multiplication table of given 'n' number value :\n");
printf("give the number :");
scanf("%d",&n);
for(i=1;i<=10;i++){
printf("%d*%d=%d\n",i,n,i*n);
}
getch();
return 0;
}
[Link] :08
Date : Armstrong number.
AIM :
To create a c program Armstrong number.
Algorithm :
Step 1 : Start
Step 2. Declare integer variables num, temp, sum, and
rem
Step 3. Initialize sum = 0
Step 4. Read the number num
Step 5. Store the value of num in temp
Step 6. While num > 0, do the following:
rem = num % 10, sum = sum + (rem × rem × rem)
num = num / 10
Step 7. If temp == sum, then
Print "Number is Armstrong"
Step 8. Else
Print "Number is not Armstrong"
Step 9. Stop.
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int num,temp,sum=0,rem;
clrscr();
printf("find the given number is armstrong or not.\n");
printf("enter a number:");
scanf("%d",&num);
temp=num;
while(num >0){
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if(temp==sum){
printf("the given number %d is armstrog\n",sum);
}else{
printf("the given number %d is not armstong\n",sum);
}
getch();
return 0;
}
[Link] :09
Date : Armstrong Check and
Fibonacci Series.
AIM :
To create a c program Armstrong Check and
Fibonacci Series.
Algorithm :
Step 1 : Start .
Step 2 : Read user choice.
Step 3 : If choice = 1 (Armstrong):
Read number.
Count number of digits.
Find sum of each digit raised to power of digits.
Compare sum with original number.
Display whether Armstrong or not.
Step 4 : If choice = 2 (Fibonacci):
Read number of terms.
Initialize first two numbers as 0 and 1.
Print numbers by adding previous two terms repeatedly.
Step 5 : If choice is invalid, display error message.
Step 6 : Stop the program.
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int choice;
clrscr();
printf("check armstrong or fibonacci series.\n");
printf("1. Check Armstrong Number\n");
printf("2. Print Fibonacci Series\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
{
int num, temp, rem, digits = 0;
int sum = 0;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while(temp > 0)
{
digits++;
temp = temp / 10;
}
temp = num;
while(temp > 0)
{
rem = temp % 10;
sum = sum + pow(rem, digits);
temp = temp / 10;
}
if(sum == num)
printf("%d is an Armstrong number", num);
else
printf("%d is NOT an Armstrong number", num);
break;
}
case 2:
{
int n, a = 0, b = 1, c, i;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci series:\n");
for(i = 1; i <= n; i++)
{
printf("%d ", a);
c = a + b;
a = b;
b = c;
}
break;
}
default:
printf("Invalid Choice");
}
getch();
return 0;
}
[Link] :10
Date : Last digit of a number
( even or add. )
AIM :
To create a c program last digit of a number even
or odd.
Algorithm :
Step 1 : Start
Step 2 : Declare variables num and last_digit
Step 3 : Read the value of num
Step 4 : Find last digit using last_digit = num % 10
Step 5 : If last_digit % 2 == 0, print "Last digit is
Even"
Step 6 : Else print "Last digit is Odd"
Step 7 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int num,last_digit;
clrscr();
printf("check the last digit of a given number is odd or
even\n");
printf("enter a number:");
scanf("%d",&num);
last_digit=num%10;
if(last_digit%2==0){
printf("%d last digit is even.\n");
}else{
printf("%d last digit is odd.\n");
}
getch();
return 0;
}
[Link] :11
Date : MINUTES TO HOURS.
AIM :
To create a c program minutes to hours.
Algorithm :
Step 1 : Start
Step 2 : Declare integer variables min, hour, remainmin
Step 3 : Display message asking for minutes
Step 4 : Read the value of min
Step 5 : Compute hour = min / 60
Step 6 : Compute remainmin = min % 60
Step 7 : Display the result in hours and minutes format
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int min;
int hour;
int remainmin;
clrscr();
printf("convert minutes to hours.\n");
printf("enter a minutes:");
scanf("%d",&min);
hour = min/60;
remainmin = min%60;
printf("%d mintuse convert to the hour:%2.2d:%2.2d\
n",min,hour,remainmin);
getch();
return 0;
}
[Link] :12
Date : Finding grade.
AIM :
To create a c program finding grade.
Algorithm :
Step 1 : Start
Step 2 : Declare integer variable mark
Step 3 : Ask the user to enter the mark
Step 4 : Read the value of mark
Step 5 : If mark >= 90, display grade A
Step 6 : Else if mark >= 80, display grade B
Step 7 : Else if mark >= 70, display grade C
Step 8 : Else if mark >= 60, display grade D
Step 9 : Else display grade F
Step 10 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int mark;
clrscr();
printf("give your mark finding your grade.\n");
printf("enter the mark:");
scanf("%d",&mark);
if(mark>=90){
printf("your grade is A.\n");
}else if(mark>=80){
printf("your grade is B.\n");
}else if(mark>=70){
printf("your grade is c.\n");
}else if(mark>=60){
printf("your grade is d.\n");
}else{
printf("your grade is F.\n");
}
getch();
return 0; }
[Link] :13
Date : Square of number.
AIM :
To create a c program square of number.
Algorithm :
Step 1 : Start the program.
Step 2 : Initialize variable i.
Step 3 : Use a loop from i = 1 to 20.
Step 4 : For each value of i, calculate square as i × i.
Step 5 : Display the number and its square.
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int i;
clrscr();
printf("square of numbers 1 to 20.\n");
for(i=1;i<=20;i++){
printf("%2.2d this number square is %2.2d\n",i,i*i);
}
getch();
return 0;
}
[Link] :14
Date : Palindrome number.
AIM :
To create a c program palindrome number.
Algorithm :
Step 1 : Start
Step 2 : Declare variables num, temp, rem, rev and
initialize rev = 0
Step 3 : Ask the user to enter a number
Step 4 : Read the value of num
Step 5 : Store num in temp
Step 6 : While num is not equal to 0:
* Find rem = num % 10
* Update rev = rev * 10 + rem
* Update num = num / 10
Step 7 : Compare temp with rev
Step 8 : If both are equal, display "Palindrome"
Step 9 : Else, display "Not a Palindrome"
Step 10 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int num,temp,rem,rev=0;
clrscr();
printf("check the given number is palindrome.\n");
printf("enter a number:");
scanf("%d",&num);
temp=num;
while(num!=0){
rem=num%10;
rev=rev*10+rem;
num=num/10;
}
if(temp==rev){
printf("%d is a palindrome.\n",temp);
}else{
printf("%d is not a palindrome.\n",temp);
}
getch();
return 0; }
[Link] :16
Date : string.
AIM :
To create a c program Length of string, Comparison
of strings, String to uppercase, String to lowercase,
Number of words in a string, Reverse a string, Keyword
searching in a text - Menu driven program.
Algorithm :
Step 1 : Start
Step 2 : Display the menu of string operations
Step 3 : Read the user's choice
Step 4 : If choice = 1 (Length)
o Read a string
o Find its length using a loop or strlen()
o Display the length
Step 5 : If choice = 2 (Compare strings)
o Read two strings
o Compare them using strcmp()
o Display whether they are equal
Step 6 : If choice = 3 (Uppercase)
o Read a string
o Convert each character to uppercase
o Display the result
Output :
o
Result :
The above program was executed and output was
Verified successfully.
Program :
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char str1[100], str2[100], key[50];
int choice, i, count = 1, found = 0;
clrscr();
printf("STRING OPERATIONS MENU\n");
printf("1. Length of string\n");
printf("2. Compare two strings\n");
printf("3. Convert to Uppercase\n");
printf("4. Convert to Lowercase\n");
printf("5. Count number of words\n");
printf("6. Reverse a string\n");
printf("7. Keyword search\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // clear buffer
switch(choice)
{
case 1:
printf("Enter string: ");
gets(str1);
printf("Length = %d\n", strlen(str1));
break;
case 2:
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
if(strcmp(str1, str2) == 0)
printf("Strings are equal\n");
else
printf("Strings are not equal\n");
break;
case 3:
printf("Enter string: ");
gets(str1);
for(i = 0; str1[i]; i++)
str1[i] = toupper(str1[i]);
printf("Uppercase: %s\n", str1);
break;
case 4:
printf("Enter string: ");
gets(str1);
for(i = 0; str1[i]; i++)
str1[i] = tolower(str1[i]);
printf("Lowercase: %s\n", str1);
break;
case 5:
printf("Enter string: ");
gets(str1);
for(i = 0; str1[i]; i++)
{
if(str1[i] == ' ' && str1[i+1] != ' ')
count++;
}
printf("Number of words = %d\n", count);
break;
case 6:
printf("Enter string: ");
gets(str1);
strrev(str1);
printf("Reversed string: %s\n", str1);
break;
case 7:
printf("Enter the text: ");
gets(str1);
printf("Enter keyword: ");
gets(key);
if(strstr(str1, key) != NULL)
printf("Keyword found\n");
else
printf("Keyword not found\n");
break;
default:
printf("Invalid choice\n");
}
getch();
return 0;
}
[Link] :17
Sorting
Date : of string - ascending, descending -
Menu driven program.
AIM :
To create a c program Sorting of string - ascending,
descending - Menu driven program.
Algorithm :
Step 1 : Start
Step 2 : Read the string from the user.
Step 3: Find the length of the string.
Step 4 : Display menu:
Sort in Ascending Order
Sort in Descending Order
Step 5 : Read the user’s choice.
Step 6 : If choice = 1 (Ascending):
Compare characters using nested loops.
Swap characters if they are in wrong order.
Display the sorted string.
Step 7 : If choice = 2 (Descending):
Compare characters using nested loops.
Swap characters if they are in wrong order.
Display the sorted string.
Output :
void main()
{
char str[100], temp;
int i, j, choice, len;
clrscr();
printf("Enter a string: ");
gets(str);
len = strlen(str);
printf("\nMENU");
printf("\n1. Sort in Ascending Order");
printf("\n2. Sort in Descending Order");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
for(i = 0; i < len - 1; i++)
{
for(j = i + 1; j < len; j++)
{
if(str[i] > str[j])
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
printf("\nAscending Order: %s", str);
break;
case 2:
for(i = 0; i < len - 1; i++)
{
for(j = i + 1; j < len; j++)
{
if(str[i] < str[j])
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
printf("\nDescending Order: %s", str);
break;
default:
printf("\nInvalid Choice!");
}
getch();
}
Result :
The above program was executed and output was
Verified successfully.
[Link] :18
Date : Electricity bill.
AIM :
To create a c program electricity bill.
Algorithm :
Step 1 : Start
Step 2 : Declare variables units (integer) and bill (float).
Step 3 : Read the number of electricity units
consumed.
Step 4 : If units ≤ 100,
bill = units × 5
Step 5 : Else if units ≤ 200,
bill = (100 × 5) + (units − 100) × 7
Step 6 : Else if units ≤ 300,
bill = (100 × 5) + (100 × 7) + (units − 200) × 10
Step 7 : Else,
bill = (100 × 5) + (100 × 7) + (100 × 10) + (units
− 300) × 12
Step 8 : Add fixed charge: bill = bill + 50
Step 9 : Display the total electricity bill.
Step 10 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main() {
int units;
float bill = 0;
clrscr();
printf("Enter electricity units consumed: ");
scanf("%d", &units);
if (units <= 100) {
bill = units * 5; }
else if (units <= 200) {
bill = (100 * 5) + (units - 100) * 7; }
else if (units <= 300) {
bill = (100 * 5) + (100 * 7) + (units - 200) * 10; }
else {
bill = (100 * 5) + (100 * 7) + (100 * 10) + (units -300)*12;}
bill += 50;
printf("Total Electricity Bill = %.2f\n", bill);
getch();
return 0;
}
[Link] :19
Date : Count of number of
vowels and
consonants in a string.
AIM :
To create a c program Count of number of vowels
and consonants in a string.
Algorithm :
Step 1 : Start
Step 2 : Start the program.
Step 3 : Declare a string array str[100] and integer
variables vowels =0, consonants = 0.
Step 4 : Read the string from the user using fgets().
Step 5 : For each character in the string:
o Convert it to lowercase using tolower().
o Check if it is an alphabet (a to z).
If it is a vowel (a, e, i, o, u), increase
vowels.
Otherwise, increase consonants.
Step 6 : Display the count of vowels and consonants.
Step 7 : Stop the program.
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
int main() {
char str[100];
int vowels = 0;
int consonants = 0;
int i;
clrscr();
printf("count of number vowels and consonant.\n");
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++) {
char ch = tolower(str[i]);
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else {
consonants++;
}
}
}
printf("Number of Vowels: %d\n", vowels);
printf("Number of Consonants: %d\n", consonants);
getch();
return 0;
}
[Link] :20
Date : Decimal to Binary
Conversion.
AIM :
To create a c program Decimal to Binary
Conversion.
Algorithm :
Step 1 : Start
Step 2 : Declare integer variables n, r, binary = 0, place
= 1.
Step 3 : Read the decimal number from the user (n).
Step 4 : Repeat while n > 0:
o Find remainder: r = n % 2.
o Store digit in result: binary = binary + (r *
place).
o Divide the number by 2: n = n / 2.
o Update place value: place = place * 10.
Step 5 : Display the binary value.
Step 6 : Stop the program.
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int n,binary,r,place;
clrscr();
binary = 0;
place = 1;
printf("convert decimal to binary.\n");
printf("give the number :");
scanf("%d",&n);
while(n>0){
r = n%2;
binary = binary + r * place;
n = n/2;
place = place * 10;
}
printf("%d\n",binary);
getch();
return 0;
}
[Link] :21
Date : Decimal to Sum of
series.
AIM :
To create a c program Sum of series.
Algorithm :
Step 1 : Start
Step 2 : Declare integer variables n, i, sum = 0.
Step 3 : Read the value of n from the user.
Step 4 : For i from 1 to n:
Add i to sum.
Display the term i.
Step 5 : Print the total sum of the series.
Step 6 : Stop the program.
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include<stdio.h>
#include<conio.h>
int main(){
int n,i,sum=0;
clrscr();
printf("sum of series.\n");
printf("enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum=sum+i;
printf("%d+",i);
}
printf("0=%d",sum);
printf("\n");
getch();
return 0;
}
[Link] :22
Date : Changing uppercase to
lowercase,lowercase to uppercase using string functions -
function.
AIM :
To create a c program Changing uppercase to
lowercase, lowercase to uppercase using string
functions - User defined function.
Algorithm :
Step 1 : Start
Step 2 : Start the program.
Step 3 : Declare a character array str[100].
Step 4 : Read a string from the user.
Step 5 : Call the function changeCase(str).
Step 6 : In changeCase():
o Repeat for each character in the string:
If the character is uppercase, convert it to
lowercase.
If the character is lowercase, convert it to
uppercase.
Step 7 : Print the converted string.
Step 8 : Stop the program.
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
void changeCase(char str[]);
int main()
{
char str[100];
clrscr();
printf("Enter a string: ");
gets(str);
changeCase(str);
printf("Converted string: %s", str);
getch();
return 0;
}
void changeCase(char str[])
{
int i;
for(i = 0; i < strlen(str); i++)
{
if(isupper(str[i]))
str[i] = tolower(str[i]);
else if(islower(str[i]))
str[i] = toupper(str[i]);
}
}
[Link] :23
Date : Pattern Printing -
Switch case.
AIM :
To create a c program Pattern Printing - Switch
case.
Algorithm :
Step 1 : Start
Step 2 : Declare integer variables choice, i, j, and n.
Step 3 : Display the pattern menu:
1. Star Square
2. Right Triangle
3. Number Triangle
Step 4 : Read the user’s choice.
Step 5 : Ask and read the number of rows n.
Step 6 : Use switch(choice) to select the pattern:
Case 1: Print a square star pattern using two
nested loops.
Case 2: Print a right-angled triangle pattern
using nested loops.
Case 3: Print a number triangle pattern using
nested loops.
Default: Display "Invalid choice!".
Step 7 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include <stdio.h>
#include<conio.h>
int main(){
int marks[50], n, i, key, count = 0;
clrscr();
printf("Enter number of students: ");
scanf("%d", &n);
for(i = 0; i < n; i++){
printf("Enter marks %d: ", i + 1);
scanf("%d", &marks[i]);
}
printf("Enter the mark to find occurrence: ");
scanf("%d", &key);
for(i = 0; i < n; i++){
if(marks[i] == key)
count++;
}
printf("Occurrence of %d = %d times", key, count);
getch();
return 0;
}
[Link] :26
Date : Finding occurrence of each
face of adie - Arrays.
AIM :
To create a c program Finding occurrence of each
face of a die - Arrays.
Algorithm :
Step 1 : Start
Step 2 : Declare an array die[50] to store dice values
and count[6] initialized to zero.
Step 3 : Read the number of throws n.
Step 4 : Repeat from i = 0 to n-1:
Read the face value for each throw.
If the value is between 1 and 6, increment the
respective index in count array.
Otherwise, display "Invalid face".
Step 5 : Display the number of occurrences for each
face from 1 to 6.
Step 6 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include <stdio.h>
#include <conio.h>
int main(){
int die[50], count[6] = {0};
int n, i;
clrscr();
printf("Enter number of throws: ");
scanf("%d", &n);
for(i = 0; i < n; i++){
printf("Enter face for throw %d (1 to 6): ", i + 1);
scanf("%d", &die[i]);
if(die[i] >= 1 && die[i] <= 6)
count[die[i] - 1]++;
else
printf("Invalid face! Enter only 1 to 6.\n"); }
printf("\nOccurrence of each face:\n");
for(i = 0; i < 6; i++){
printf("Face %d = %d times\n", i + 1, count[i]); }
getch();
return 0;
}
[Link] :27
Date : matrices.
AIM :
To create a c program To add two matrices,
subtract two matrices, multiply two matrices, sum of
diagonal elements, sum of upper triangle matrix, sum of
lower triangle matrix, sum of each row and each column
of a matrix, transpose of a matrix - User defined
functions and Menu driven program.
Algorithm :
Step 1 : Start
Step 2 : Declare two matrices A and B and required
variables.
Step 3 : Read number of rows and columns of first
matrix.
Step 4 : Read elements of first matrix.
Step 5 : Read number of rows and columns of second
matrix.
Step 6 : Read elements of second matrix.
Step 7 : Display the menu with operations:
Add
Subtract
Multiply
Diagonal sum
Upper triangle sum
Lower triangle sum
Output :
Program :
#include <stdio.h>
#include <conio.h>
void readMatrix(int a[10][10], int r, int c);
void printMatrix(int a[10][10], int r, int c);
void addMatrix(int a[10][10], int b[10][10], int r, int c);
void subMatrix(int a[10][10], int b[10][10], int r, int c);
void mulMatrix(int a[10][10], int b[10][10], int r1, int c1,
int r2, int c2);
void diagonalSum(int a[10][10], int n);
void upperSum(int a[10][10], int n);
void lowerSum(int a[10][10], int n);
void rowColumnSum(int a[10][10], int r, int c);
void transpose(int a[10][10], int r, int c);
int main(){
int a[10][10], b[10][10];
int r1, c1, r2, c2, ch;
clrscr();
printf("Enter rows and columns of first matrix: ");
scanf("%d%d", &r1, &c1);
readMatrix(a, r1, c1);
switch(ch){
case 1:
if(r1 == r2 && c1 == c2)
addMatrix(a, b, r1, c1);
else
printf("Addition not possible!\n");
break;
case 2:
if(r1 == r2 && c1 == c2)
subMatrix(a, b, r1, c1);
else
printf("Subtraction not possible!\n");
break;
case 3:
if(c1 == r2)
mulMatrix(a, b, r1, c1, r2, c2);
else
printf("Multiplication not possible!\n");
break;
case 4:
if(r1 == c1)
diagonalSum(a, r1);
else
printf("Not a square matrix!\n");
break;
case 5:
if(r1 == c1)
upperSum(a, r1);
else
printf("Not a square matrix!\n");
break;
case 6:
if(r1 == c1)
lowerSum(a, r1);
else
printf("Not a square matrix!\n");
break;
case 7:
rowColumnSum(a, r1, c1);
break;
case 8:
transpose(a, r1, c1);
break;
case 9:
printf("Exiting Program...\n");
break;
default:
printf("Invalid choice!\n");
}
} while(ch != 9);
getch();
return 0;
}
void readMatrix(int a[10][10], int r, int c)
{
int i,j;
printf("Enter elements:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
}
void printMatrix(int a[10][10], int r, int c)
{
int i,j;
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
void addMatrix(int a[10][10], int b[10][10], int r, int c){
int i,j,sum[10][10];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
sum[i][j]=a[i][j]+b[i][j];
printf("Sum Matrix:\n");
printMatrix(sum,r,c);
}
void subMatrix(int a[10][10], int b[10][10], int r, int c){
int i,j,diff[10][10];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
diff[i][j]=a[i][j]-b[i][j];
printf("Difference Matrix:\n");
printMatrix(diff,r,c);
}
void mulMatrix(int a[10][10], int b[10][10], int r1, int c1,
int r2, int c2){
int i,j,k, mul[10][10]={0};
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
for(k=0;k<c1;k++)
mul[i][j]+=a[i][k]*b[k][j];
printf("Product Matrix:\n");
printMatrix(mul,r1,c2);
}
void diagonalSum(int a[10][10], int n){
int i,sum=0;
for(i=0;i<n;i++)
sum+=a[i][i];
printf("Diagonal Sum = %d\n",sum);
}
void upperSum(int a[10][10], int n){
int i,j,sum=0;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
sum+=a[i][j];
printf("Upper Triangle Sum = %d\n",sum);
}
void lowerSum(int a[10][10], int n){
int i,j,sum=0;
for(i=1;i<n;i++)
for(j=0;j<i;j++)
sum+=a[i][j];
printf("Lower Triangle Sum = %d\n",sum);
}
void rowColumnSum(int a[10][10], int r, int c)
{
int i,j,sum;
for(i=0;i<r;i++){
sum=0;
for(j=0;j<c;j++)
sum+=a[i][j];
printf("Row %d Sum = %d\n",i+1,sum);
}
for(j=0;j<c;j++){
sum=0;
for(i=0;i<r;i++)
sum+=a[i][j];
printf("Column %d Sum = %d\n",j+1,sum);
}
}
void transpose(int a[10][10], int r, int c){
int t[10][10], i,j;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
t[j][i]=a[i][j];
printf("Transpose Matrix:\n");
printMatrix(t,c,r);
}
Result :
The above program was executed and output was
Verified successfully.
[Link] :28
Date : Linear search, binary
search - Menu driven.
AIM :
To create a c program Linear search, binary search
- Menu driven.
Algorithm :
Linear Search & Binary Search (Menu Driven)
Step 1 : Start
Step 2 : Read number of elements n
Step 3 : Read array elements
Step 4 : Display menu (1-Linear, 2-Binary)
Step 5 : Read choice and search element key
If choice = 1(Linear Search):
Step 6 : Compare key with each element from start
Step 7 : If match found → display position
Step 8 : If end reached → display "Not found"
If choice = 2 (Binary Search):
Step 9 : Set low = 0, high = n-1
Step 10 : Find mid = (low + high) / 2
Step 11 : Compare key with a[mid]
Step 12 : If equal → display position
Output :
Step 12 : If smaller → high = mid - 1
Step 13 : If larger → low = mid + 1
Step 14 : Repeat until found or low > high
Step 15 : If not found → display message
Step 16 : Stop
Program :
#include <stdio.h>
#include <conio.h>
int main(){
int a[50], n, i, key, ch;
int low, high, mid, found = 0;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements (in sorted order for Binary
Search):\n");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("\nSEARCH MENU\n");
printf("1. Linear Search\n");
printf("2. Binary Search\n");
printf("Enter your choice: ");
scanf("%d", &ch);
printf("Enter element to search: ");
scanf("%d", &key);
switch(ch){
case 1:
for(i = 0; i < n; i++){
if(a[i] == key){
found = 1;
printf("Element found at position %d", i + 1);
break;
}
}
if(found == 0)
printf("Element not found");
break;
case 2:
low = 0;
high = n - 1;
found = 0;
while(low <= high){
mid = (low + high) / 2;
if(a[mid] == key){
printf("Element found at position %d", mid + 1);
found = 1;
break;
}
else if(key < a[mid])
high = mid - 1;
else
low = mid + 1;
}
if(found == 0)
printf("Element not found");
break;
default:
printf("Invalid choice");
}
getch();
return 0;
}
Result :
The above program was executed and output was
Verified successfully.
[Link] :29
Date : Bubble sort, selection
sort - Menu driven.
AIM :
To create a c program Bubble sort, selection
sort - Menu driven.
Algorithm :
Step 1 : Start
Step 2 : Read number of elements n
Step 3 : Read n elements into array a[]
Step 4 : Display menu
Step 5 : Read choice ch
If ch = 1 → Bubble Sort
Step 6 : Repeat for i = 0 to n-2
Step 7 : Repeat for j = 0 to n-i-2
Step 8 : If a[j] > a[j+1], swap them
Step 9 : After all passes, array becomes sorted
If ch = 2 → Selection Sort
Step 10 : Repeat for i = 0 to n-2
Step 11: Set min = i
Step 12 : Find smallest element from i+1 to n-1
Step 13 : Swap smallest with a[i]
Output :
Step 14 : Continue until entire array is sorted
Step 15 : Display sorted array
Step 16 : Stop
Program :
#include <stdio.h>
#include <conio.h>
int main()
{
int a[50], n, i, j, temp, ch, min;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("\nSORTING MENU\n");
printf("1. Bubble Sort\n");
printf("2. Selection Sort\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
for(i = 0; i < n - 1; i++)
{
for(j = 0; j < n - i - 1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Sorted array by Bubble Sort:\n");
break;
case 2:
for(i = 0; i < n - 1; i++)
{
min = i;
for(j = i + 1; j < n; j++)
{
if(a[j] < a[min])
min = j;
}
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
printf("Sorted array by Selection Sort:\n");
break;
default:
printf("Invalid choice");
}
for(i = 0; i < n; i++)
printf("%d ", a[i]);
getch();
return 0;
}
Result :
The above program was executed and output was
Verified successfully.
[Link] :30
Date : Student details -
Structure.
AIM :
To create a c program Student details - Structure.
Algorithm :
Step 1 : Start
Step 2 : Define a structure student with members: roll,
name, marks
Step 3 : Declare an array of structure s[2]
Step 4 : Assign values to roll number, name and marks
for each student
Step 5 : Display student details using structure
members
Step 6 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include <stdio.h>
#include <conio.h>
#include <string.h>
struct student
{
int roll;
char name[20];
float marks;
};
int main()
{
struct student s[2];
clrscr();
s[0].roll = 101;
strcpy(s[0].name, "Gokul");
s[0].marks = 85.5;
s[1].roll = 102;
strcpy(s[1].name, "Akash");
s[1].marks = 90.0;
printf("\n--- Student Details ---\n");
printf("\nStudent 1\n");
printf("Roll No : %d\n", s[0].roll);
printf("Name : %s\n", s[0].name);
printf("Marks : %.2f\n", s[0].marks);
printf("\nStudent 2\n");
printf("Roll No : %d\n", s[1].roll);
printf("Name : %s\n", s[1].name);
printf("Marks : %.2f\n", s[1].marks);
getch();
return 0;
}
[Link] :31
Date : Write and read students
details as structure - Files.
AIM :
To create a c program Write and read students
details as structure - Files.
Algorithm :
Step 1 : Start
Step 2 : Define structure student
Step 3 : Open file in write mode
Step 4 : Get student details from user
Step 5 : Write details into file
Step 6 : Close file
Step 7 : Open file in read mode
Step 8 : Read details from file
Step 9 : Display student details
Step 10 : Close file
Step 11 : Stop
Result :
The above program was executed and output was
Verified successfully.
Output :
Program :
#include <stdio.h>
#include <conio.h>
struct student
{
int roll;
char name[20];
float marks;
};
int main()
{
struct student s[2];
FILE *fp;
int i;
clrscr();
s[0].roll = 101;
strcpy(s[0].name, "Gokul");
s[0].marks = 85.5;
s[1].roll = 102;
strcpy(s[1].name, "Akash");
s[1].marks = 90.0;
fp = fopen("[Link]", "w");
for(i = 0; i < 2; i++)
fprintf(fp, "%d %s %.2f\n", s[i].roll, s[i].name, s[i].marks);
fclose(fp);
printf("Data written to file successfully!\n");
fp = fopen("[Link]", "r");
printf("\n--- Student Details from File ---\n");
while(fscanf(fp, "%d %s %f",
&s[i].roll, s[i].name, &s[i].marks) != EOF)
{
printf("\nRoll No : %d", s[i].roll);
printf("\nName : %s", s[i].name);
printf("\nMarks : %.2f\n", s[i].marks);
}
fclose(fp);
getch();
return 0;
}