SlideShare a Scribd company logo
INTRODUCTION TO C PROGRAMMING
Basic c programs
Updated on 31.8.2020
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)/*loop starts from 2 because 0 and 1 are alr
eady printed*/
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}
Basic c programs updated on 31.8.2020
#inlcude<stdio.h>
#inlcude<conio.h>
void main()
{
int r=5;
float area;
area=3.14*r*r;
printf("area:%d",area);
getch();
}
BASIC C PROGRAMS
1.Write a program to print the position of the smallestnumber
of n numbers using arrays.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20], small, pos;
clrscr();
printf("n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("n Enter the elements : ");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
small = arr[0]
pos =0;
for(i=1;i<n;i++)
{
if(arr[i]<small)
{
small = arr[i];
pos = i;
}
}
printf("n The smallest element is : %d", small);
printf("n The position of the smallest element in the array is :
%d", pos);
return 0;
}
2. Write a program to insert a number at a given locationin
an array.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, num, pos, arr[10];
clrscr();
printf("n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("n arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("n Enter the number to be inserted : ");
scanf("%d", &num);
printf("n Enter the position at which the number has to be added :
");
scanf("%d", &pos);
for(i=n–1;i>=pos;i––)
arr[i+1] = arr[i];
arr[pos] = num;
n = n+1;
printf("n The array after insertion of %d is : ", num);
for(i=0;i<n;i++)
printf("n arr[%d] = %d", i, arr[i]);
getch();
return 0;
}
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Enter the number to be inserted : 0
Enter the position at which the number has to be added : 3
The array after insertion of 0 is :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 0
arr[4] = 4
arr[5] = 5
3.Write a program to insert a number in an array that is
already sortedin ascending order.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, j, num, arr[10];
clrscr();
printf("n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("n arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("n Enter the numberto be inserted : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] > num)
{
for(j = n–1; j>=i; j––)
arr[j+1] = arr[j];
arr[i] = num;
break;
}
}
n = n+1;
printf("n The array after insertion of %d is : ", num);
for(i=0;i<n;i++)
printf("n arr[%d] = %d", i, arr[i]);
getch();
return 0;
}
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 4
arr[3] = 5
arr[4] = 6
Enter the number to be inserted : 3
The array after insertion of 3 is :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
arr[5] = 6
4.Write a program to delete a number from a given locationin
an array.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, pos, arr[10];
clrscr();
printf("n Enter the number of elements in the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("n arr[%d] = ", i);
scanf("%d", &arr[i]);
}
printf("nEnter the position from which the number has to be
deleted : ");
scanf("%d", &pos);
for(i=pos; i<n–1;i++)
arr[i] = arr[i+1];
n––;
printf("n The array after deletion is : ");
for(i=0;i<n;i++)
printf("n arr[%d] = %d", i, arr[i]);
getch();
return 0;
}
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Enter the position from which the number has to be deleted : 3
The array after deletion is :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 5
5. Write a program to merge two unsorted arrays.
#include <stdio.h>
#include <conio.h>
int main()
if(arr1[index_first]<arr2[index_second])
{
arr3[index] = arr1[index_first];
index_first++;
}
else
{
arr3[index] = arr2[index_second];
index_second++;
}
index++;
}
// if elements of the first array are over and the second array has
some elements
if(index_first == n1)
{
while(index_second<n2)
{
arr3[index] = arr2[index_second];
index_second++;
index++;
}
}
// if elements of the second array are over and the first array has
some elements
else if(index_second == n2)
{
while(index_first<n1)
{
arr3[index] = arr1[index_first];
index_first++;
index++;
}
}
printf("nn The merged array is");
for(i=0;i<m;i++)
printf("n arr[%d] = %d", i, arr3[i]);
getch();
return 0;
}
Output
Enter the number of elements in array1 : 3
Enter the elements of the first array
arr1[0] = 1
arr1[1] = 3
arr1[2] = 5
Enter the number of elements in array2 : 3
Enter the elements of the second array
arr2[0] = 2
arr2[1] = 4
arr2[2] = 6
//Program to Printan Integer
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
// reads and stores input
scanf("%d", &number);
// displays output
printf("You entered: %d", number);
return 0;
}
//Program to Add TwoIntegers
#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// calculating sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
//Program to Multiply Two Numbers
#include <stdio.h>
int main() {
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
// Calculating product
product= a * b;
// Result up to 2 decimal point is displayed using %.2lf
printf("Product = %.2lf", product);
return 0;
}
//Swap Numbers Using Temporary Variable
#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// Value of first is assigned to temp
temp = first;
// Value of second is assigned to first
first = second;
// Value of temp (initial value of first) is assigned to second
second = temp;
printf("nAfter swapping, firstNumber = %.2lfn", first);printf("After swapping, secondNumber = %.2lf", second);
return 0;
}
Programto Check Vowelor consonant
#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);
// evaluates to 1 if variable c is a lowercase vowel
lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c ==
'u');
// evaluates to 1 if variable c is a uppercasevowel
uppercase_vowel= (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c
== 'U');
// evaluates to 1 (true) if c is a vowel
if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
Check Armstrong Number of three digits
//153 = 1*1*1 + 5*5*5 + 3*3*3
#include <stdio.h>
int main() {
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d n", n, i, n * i);
}
return 0;
}
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;
result += remainder * remainder * remainder;
// removing last digit from the orignal number
originalNum /= 10;}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
Programto Check Palindrome
#include <stdio.h>
int main() {
int n, reversedN = 0, remainder, originalN;
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;
// reversed integer is stored in reversedN
while (n != 0) {
remainder = n % 10;
reversedN = reversedN * 10 + remainder;
n /= 10;
}
// palindrome if originalN and reversedN are equal
if (originalN == reversedN)
printf("%d is a palindrome.", originalN);
else
printf("%d is not a palindrome.", originalN);
return 0;
}
//ARRAY
//Example 2: Sum of two matrices
// C program to find the sum of two matrices of order 2*2
#include <stdio.h>
int main()
{
float a[2][2], b[2][2], result[2][2];
// Taking input using nested for loop
printf("Enter elements of 1st matrixn");
for (int row = 0; row < 2; ++row)
for (int col= 0; col< 2; ++col)
{
printf("Enter a%d%d:", row + 1, col+ 1);
scanf("%f", &a[row][col]);
}
// Taking input using nested for loop
printf("Enter elements of 2nd matrixn");
for (int row = 0; row < 2; ++row)
for (int col= 0; col< 2; ++col)
{
printf("Enter b%d%d:", row + 1, col+ 1);
scanf("%f", &b[row][col]);
}
// adding correspondingelements of two arrays
for (int row = 0; row < 2; ++row)
for (int col= 0; col< 2; ++col)
{
result[row][col] = a[row][col] + b[row][col];
}
// Displaying the sum
printf("nSum Of Matrix:");
for (int row = 0; row < 2; ++row)
for (int col= 0; col< 2; ++col)
{
printf("%.1ft", result[row][col]);
if (col == 1)
printf("n");
}
return 0;
}
Program to Find the Transpose of a
Matrix
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
// Assigning elements to the matrix
printf("nEnter matrix elements:n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
// Displaying the matrix a[][]
printf("nEntered matrix: n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("n");
}
// Finding the transpose of matrix a
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {transpose[j][i] = a[i][j];
}
// Displaying the transpose of matrix a
printf("nTranspose of the matrix:n");
for (i = 0; i < c; ++i)
for (j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("n");
}
return 0;
}

More Related Content

PDF
Data Structure using C
Bilal Mirza
 
DOCX
Cpds lab
praveennallavelly08
 
PPT
All important c programby makhan kumbhkar
sandeep kumbhkar
 
DOCX
DataStructures notes
Lakshmi Sarvani Videla
 
DOCX
SaraPIC
Sara Sahu
 
DOCX
Practical File of C Language
RAJWANT KAUR
 
DOCX
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
DOCX
Practical write a c program to reverse a given number
Mainak Sasmal
 
Data Structure using C
Bilal Mirza
 
All important c programby makhan kumbhkar
sandeep kumbhkar
 
DataStructures notes
Lakshmi Sarvani Videla
 
SaraPIC
Sara Sahu
 
Practical File of C Language
RAJWANT KAUR
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Hazrat Bilal
 
Practical write a c program to reverse a given number
Mainak Sasmal
 

What's hot (20)

PDF
c-programming-using-pointers
Sushil Mishra
 
DOCX
Chapter 8 c solution
Azhar Javed
 
DOCX
Practical write a c program to reverse a given number
Mainak Sasmal
 
DOCX
Data Structures Using C Practical File
Rahul Chugh
 
DOCX
Core programming in c
Rahul Pandit
 
PDF
C programms
Mukund Gandrakota
 
PDF
Common problems solving using c
ArghodeepPaul
 
DOCX
C programs
Minu S
 
DOCX
C lab manaual
manoj11manu
 
PDF
Data Structures Practical File
Harjinder Singh
 
PDF
88 c-programs
Leandro Schenone
 
DOC
Daa practicals
Rekha Yadav
 
PPSX
C programming array & shorting
argusacademy
 
PDF
The solution manual of c by robin
Abdullah Al Naser
 
PPTX
4. chapter iii
Chhom Karath
 
DOCX
C Programming
Sumant Diwakar
 
PDF
C Programming Example
PRATHAMESH DESHPANDE
 
PDF
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
DR B.Surendiran .
 
c-programming-using-pointers
Sushil Mishra
 
Chapter 8 c solution
Azhar Javed
 
Practical write a c program to reverse a given number
Mainak Sasmal
 
Data Structures Using C Practical File
Rahul Chugh
 
Core programming in c
Rahul Pandit
 
C programms
Mukund Gandrakota
 
Common problems solving using c
ArghodeepPaul
 
C programs
Minu S
 
C lab manaual
manoj11manu
 
Data Structures Practical File
Harjinder Singh
 
88 c-programs
Leandro Schenone
 
Daa practicals
Rekha Yadav
 
C programming array & shorting
argusacademy
 
The solution manual of c by robin
Abdullah Al Naser
 
4. chapter iii
Chhom Karath
 
C Programming
Sumant Diwakar
 
C Programming Example
PRATHAMESH DESHPANDE
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
DR B.Surendiran .
 
Ad

Similar to Basic c programs updated on 31.8.2020 (20)

DOCX
Write a program to check a given number is prime or not
aluavi
 
PPTX
array ppt of c programing language for exam
shimishtrivedi
 
PPT
array.ppt
DeveshDewangan5
 
PPTX
Examples sandhiya class'
Dr.Sandhiya Ravi
 
PDF
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 
DOCX
ADA FILE
Gaurav Singh
 
PDF
C lab programs
Dr. Prashant Vats
 
PDF
C lab programs
Dr. Prashant Vats
 
DOC
6.array
Shankar Gangaju
 
PPTX
Arrays
RaziyasultanaShaik
 
PDF
Unit 4
SHIKHA GAUTAM
 
PPTX
C Programming Language Part 8
Rumman Ansari
 
PPTX
Arrays 2d Arrays 2d Arrays 2d Arrrays 2d
LakshayBhardwaj39
 
DOCX
C file
simarsimmygrewal
 
PDF
C programs
Koshy Geoji
 
DOC
C lab-programs
Tony Kurishingal
 
PDF
Programming in C Lab
Neil Mathew
 
PDF
1D Array
A. S. M. Shafi
 
PPTX
Array,MULTI ARRAY, IN C
naveed jamali
 
Write a program to check a given number is prime or not
aluavi
 
array ppt of c programing language for exam
shimishtrivedi
 
array.ppt
DeveshDewangan5
 
Examples sandhiya class'
Dr.Sandhiya Ravi
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 
ADA FILE
Gaurav Singh
 
C lab programs
Dr. Prashant Vats
 
C lab programs
Dr. Prashant Vats
 
C Programming Language Part 8
Rumman Ansari
 
Arrays 2d Arrays 2d Arrays 2d Arrrays 2d
LakshayBhardwaj39
 
C programs
Koshy Geoji
 
C lab-programs
Tony Kurishingal
 
Programming in C Lab
Neil Mathew
 
1D Array
A. S. M. Shafi
 
Array,MULTI ARRAY, IN C
naveed jamali
 
Ad

More from vrgokila (6)

PPT
evaluation technique uni 2
vrgokila
 
PPT
Unit 2 HCI DESIGN RULES AND DESIGN PATTERNS
vrgokila
 
PPT
Unit 2 hci
vrgokila
 
PPT
Array 31.8.2020 updated
vrgokila
 
PDF
Unit 1 ocs752 introduction to c programming
vrgokila
 
PPT
Array
vrgokila
 
evaluation technique uni 2
vrgokila
 
Unit 2 HCI DESIGN RULES AND DESIGN PATTERNS
vrgokila
 
Unit 2 hci
vrgokila
 
Array 31.8.2020 updated
vrgokila
 
Unit 1 ocs752 introduction to c programming
vrgokila
 
Array
vrgokila
 

Recently uploaded (20)

PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
PPTX
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
PDF
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PDF
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PPTX
Online Cab Booking and Management System.pptx
diptipaneri80
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
Inventory management chapter in automation and robotics.
atisht0104
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
Civil Engineering Practices_BY Sh.JP Mishra 23.09.pptx
bineetmishra1990
 
CAD-CAM U-1 Combined Notes_57761226_2025_04_22_14_40.pdf
shailendrapratap2002
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
FLEX-LNG-Company-Presentation-Nov-2017.pdf
jbloggzs
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
The Effect of Artifact Removal from EEG Signals on the Detection of Epileptic...
Partho Prosad
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
Online Cab Booking and Management System.pptx
diptipaneri80
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 

Basic c programs updated on 31.8.2020

  • 1. INTRODUCTION TO C PROGRAMMING Basic c programs Updated on 31.8.2020 #include<stdio.h> int main() { int n1=0,n2=1,n3,i,number; printf("Enter the number of elements:"); scanf("%d",&number); printf("n%d %d",n1,n2);//printing 0 and 1 for(i=2;i<number;++i)/*loop starts from 2 because 0 and 1 are alr eady printed*/ { n3=n1+n2; printf(" %d",n3); n1=n2; n2=n3; } return 0; }
  • 3. #inlcude<stdio.h> #inlcude<conio.h> void main() { int r=5; float area; area=3.14*r*r; printf("area:%d",area); getch(); } BASIC C PROGRAMS 1.Write a program to print the position of the smallestnumber of n numbers using arrays. #include <stdio.h> #include <conio.h> int main() { int i, n, arr[20], small, pos; clrscr(); printf("n Enter the number of elements in the array : "); scanf("%d", &n); printf("n Enter the elements : "); for(i=0;i<n;i++) scanf("%d",&arr[i]);
  • 4. small = arr[0] pos =0; for(i=1;i<n;i++) { if(arr[i]<small) { small = arr[i]; pos = i; } } printf("n The smallest element is : %d", small); printf("n The position of the smallest element in the array is : %d", pos); return 0; } 2. Write a program to insert a number at a given locationin an array. #include <stdio.h> #include <conio.h> int main() { int i, n, num, pos, arr[10]; clrscr(); printf("n Enter the number of elements in the array : "); scanf("%d", &n); for(i=0;i<n;i++) { printf("n arr[%d] = ", i); scanf("%d", &arr[i]); } printf("n Enter the number to be inserted : "); scanf("%d", &num); printf("n Enter the position at which the number has to be added : "); scanf("%d", &pos); for(i=n–1;i>=pos;i––) arr[i+1] = arr[i]; arr[pos] = num; n = n+1; printf("n The array after insertion of %d is : ", num); for(i=0;i<n;i++) printf("n arr[%d] = %d", i, arr[i]); getch(); return 0; } Output Enter the number of elements in the array : 5 arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4 arr[4] = 5 Enter the number to be inserted : 0 Enter the position at which the number has to be added : 3 The array after insertion of 0 is : arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 0 arr[4] = 4
  • 5. arr[5] = 5 3.Write a program to insert a number in an array that is already sortedin ascending order. #include <stdio.h> #include <conio.h> int main() { int i, n, j, num, arr[10]; clrscr(); printf("n Enter the number of elements in the array : "); scanf("%d", &n); for(i=0;i<n;i++) { printf("n arr[%d] = ", i); scanf("%d", &arr[i]); } printf("n Enter the numberto be inserted : "); scanf("%d", &num); for(i=0;i<n;i++) { if(arr[i] > num) { for(j = n–1; j>=i; j––) arr[j+1] = arr[j]; arr[i] = num; break; } } n = n+1; printf("n The array after insertion of %d is : ", num); for(i=0;i<n;i++) printf("n arr[%d] = %d", i, arr[i]); getch(); return 0; } Output Enter the number of elements in the array : 5 arr[0] = 1 arr[1] = 2 arr[2] = 4 arr[3] = 5 arr[4] = 6 Enter the number to be inserted : 3 The array after insertion of 3 is : arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4 arr[4] = 5 arr[5] = 6 4.Write a program to delete a number from a given locationin an array. #include <stdio.h> #include <conio.h> int main() { int i, n, pos, arr[10]; clrscr(); printf("n Enter the number of elements in the array : "); scanf("%d", &n);
  • 6. for(i=0;i<n;i++) { printf("n arr[%d] = ", i); scanf("%d", &arr[i]); } printf("nEnter the position from which the number has to be deleted : "); scanf("%d", &pos); for(i=pos; i<n–1;i++) arr[i] = arr[i+1]; n––; printf("n The array after deletion is : "); for(i=0;i<n;i++) printf("n arr[%d] = %d", i, arr[i]); getch(); return 0; } Output Enter the number of elements in the array : 5 arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 4 arr[4] = 5 Enter the position from which the number has to be deleted : 3 The array after deletion is : arr[0] = 1 arr[1] = 2 arr[2] = 3 arr[3] = 5 5. Write a program to merge two unsorted arrays. #include <stdio.h> #include <conio.h> int main() if(arr1[index_first]<arr2[index_second]) { arr3[index] = arr1[index_first]; index_first++; } else { arr3[index] = arr2[index_second]; index_second++; } index++; } // if elements of the first array are over and the second array has some elements if(index_first == n1) { while(index_second<n2) { arr3[index] = arr2[index_second]; index_second++; index++; } } // if elements of the second array are over and the first array has some elements else if(index_second == n2) { while(index_first<n1) { arr3[index] = arr1[index_first];
  • 7. index_first++; index++; } } printf("nn The merged array is"); for(i=0;i<m;i++) printf("n arr[%d] = %d", i, arr3[i]); getch(); return 0; } Output Enter the number of elements in array1 : 3 Enter the elements of the first array arr1[0] = 1 arr1[1] = 3 arr1[2] = 5 Enter the number of elements in array2 : 3 Enter the elements of the second array arr2[0] = 2 arr2[1] = 4 arr2[2] = 6 //Program to Printan Integer #include <stdio.h> int main() { int number; printf("Enter an integer: "); // reads and stores input scanf("%d", &number); // displays output printf("You entered: %d", number); return 0; } //Program to Add TwoIntegers #include <stdio.h> int main() { int number1, number2, sum; printf("Enter two integers: "); scanf("%d %d", &number1, &number2); // calculating sum sum = number1 + number2; printf("%d + %d = %d", number1, number2, sum); return 0; } //Program to Multiply Two Numbers #include <stdio.h> int main() { double a, b, product; printf("Enter two numbers: ");
  • 8. scanf("%lf %lf", &a, &b); // Calculating product product= a * b; // Result up to 2 decimal point is displayed using %.2lf printf("Product = %.2lf", product); return 0; } //Swap Numbers Using Temporary Variable #include<stdio.h> int main() { double first, second, temp; printf("Enter first number: "); scanf("%lf", &first); printf("Enter second number: "); scanf("%lf", &second); // Value of first is assigned to temp temp = first; // Value of second is assigned to first first = second; // Value of temp (initial value of first) is assigned to second second = temp; printf("nAfter swapping, firstNumber = %.2lfn", first);printf("After swapping, secondNumber = %.2lf", second); return 0; } Programto Check Vowelor consonant #include <stdio.h> int main() { char c; int lowercase_vowel, uppercase_vowel; printf("Enter an alphabet: "); scanf("%c", &c); // evaluates to 1 if variable c is a lowercase vowel lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); // evaluates to 1 if variable c is a uppercasevowel uppercase_vowel= (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); // evaluates to 1 (true) if c is a vowel if (lowercase_vowel || uppercase_vowel) printf("%c is a vowel.", c); else printf("%c is a consonant.", c); return 0;
  • 9. } Check Armstrong Number of three digits //153 = 1*1*1 + 5*5*5 + 3*3*3 #include <stdio.h> int main() { int n, i; printf("Enter an integer: "); scanf("%d", &n); for (i = 1; i <= 10; ++i) { printf("%d * %d = %d n", n, i, n * i); } return 0; } #include <stdio.h> int main() { int num, originalNum, remainder, result = 0; printf("Enter a three-digit integer: "); scanf("%d", &num); originalNum = num; while (originalNum != 0) { // remainder contains the last digit remainder = originalNum % 10; result += remainder * remainder * remainder; // removing last digit from the orignal number originalNum /= 10;} if (result == num) printf("%d is an Armstrong number.", num); else printf("%d is not an Armstrong number.", num); return 0; } Programto Check Palindrome #include <stdio.h> int main() { int n, reversedN = 0, remainder, originalN; printf("Enter an integer: "); scanf("%d", &n); originalN = n; // reversed integer is stored in reversedN while (n != 0) { remainder = n % 10; reversedN = reversedN * 10 + remainder; n /= 10; } // palindrome if originalN and reversedN are equal if (originalN == reversedN) printf("%d is a palindrome.", originalN); else
  • 10. printf("%d is not a palindrome.", originalN); return 0; } //ARRAY //Example 2: Sum of two matrices // C program to find the sum of two matrices of order 2*2 #include <stdio.h> int main() { float a[2][2], b[2][2], result[2][2]; // Taking input using nested for loop printf("Enter elements of 1st matrixn"); for (int row = 0; row < 2; ++row) for (int col= 0; col< 2; ++col) { printf("Enter a%d%d:", row + 1, col+ 1); scanf("%f", &a[row][col]); } // Taking input using nested for loop printf("Enter elements of 2nd matrixn"); for (int row = 0; row < 2; ++row) for (int col= 0; col< 2; ++col) { printf("Enter b%d%d:", row + 1, col+ 1); scanf("%f", &b[row][col]); } // adding correspondingelements of two arrays for (int row = 0; row < 2; ++row) for (int col= 0; col< 2; ++col) { result[row][col] = a[row][col] + b[row][col]; } // Displaying the sum printf("nSum Of Matrix:"); for (int row = 0; row < 2; ++row) for (int col= 0; col< 2; ++col) { printf("%.1ft", result[row][col]); if (col == 1) printf("n"); } return 0; }
  • 11. Program to Find the Transpose of a Matrix #include <stdio.h> int main() { int a[10][10], transpose[10][10], r, c, i, j; printf("Enter rows and columns: "); scanf("%d %d", &r, &c); // Assigning elements to the matrix printf("nEnter matrix elements:n"); for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { printf("Enter element a%d%d: ", i + 1, j + 1); scanf("%d", &a[i][j]); } // Displaying the matrix a[][] printf("nEntered matrix: n"); for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { printf("%d ", a[i][j]); if (j == c - 1) printf("n"); } // Finding the transpose of matrix a for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) {transpose[j][i] = a[i][j]; } // Displaying the transpose of matrix a printf("nTranspose of the matrix:n"); for (i = 0; i < c; ++i) for (j = 0; j < r; ++j) { printf("%d ", transpose[i][j]); if (j == r - 1) printf("n"); } return 0; }