SlideShare uma empresa Scribd logo
1 de 11
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;
}
#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;
}

Mais conteúdo relacionado

Mais procurados

c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointersSushil Mishra
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File Rahul Chugh
 
Core programming in c
Core programming in cCore programming in c
Core programming in cRahul Pandit
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using cArghodeepPaul
 
C programs
C programsC programs
C programsMinu S
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File Harjinder Singh
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shortingargusacademy
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robinAbdullah Al Naser
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 

Mais procurados (20)

c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
C PROGRAMS
C PROGRAMSC PROGRAMS
C PROGRAMS
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
C programms
C programmsC programms
C programms
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
C programs
C programsC programs
C programs
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Daa practicals
Daa practicalsDaa practicals
Daa practicals
 
C programming array & shorting
C  programming array & shortingC  programming array & shorting
C programming array & shorting
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
4. chapter iii
4. chapter iii4. chapter iii
4. chapter iii
 
C Programming
C ProgrammingC Programming
C Programming
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 

Semelhante a Basic c programs updated on 31.8.2020

Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresLakshmi Sarvani Videla
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Er Ritu Aggarwal
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdfsowmya koneru
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfAshutoshprasad27
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxAshutoshprasad27
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8Rumman Ansari
 
C basics
C basicsC basics
C basicsMSc CST
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxhappycocoman
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)Ashishchinu
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSKavyaSharma65
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C ProgramsKandarp Tiwari
 

Semelhante a Basic c programs updated on 31.8.2020 (20)

Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Array Programs.pdf
Array Programs.pdfArray Programs.pdf
Array Programs.pdf
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdfPCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.pdf
 
PCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docxPCA-2 Programming and Solving 2nd Sem.docx
PCA-2 Programming and Solving 2nd Sem.docx
 
Examples sandhiya class'
Examples sandhiya class'Examples sandhiya class'
Examples sandhiya class'
 
C programs
C programsC programs
C programs
 
C Programming Language Part 8
C Programming Language Part 8C Programming Language Part 8
C Programming Language Part 8
 
C basics
C basicsC basics
C basics
 
array.ppt
array.pptarray.ppt
array.ppt
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
VTU Data Structures Lab Manual
VTU Data Structures Lab ManualVTU Data Structures Lab Manual
VTU Data Structures Lab Manual
 
Progr2
Progr2Progr2
Progr2
 
C file
C fileC file
C file
 
LET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERSLET US C (5th EDITION) CHAPTER 2 ANSWERS
LET US C (5th EDITION) CHAPTER 2 ANSWERS
 
DAA Lab File C Programs
DAA Lab File C ProgramsDAA Lab File C Programs
DAA Lab File C Programs
 
Ds
DsDs
Ds
 

Mais de vrgokila

evaluation technique uni 2
evaluation technique uni 2evaluation technique uni 2
evaluation technique uni 2vrgokila
 
Unit 2 HCI DESIGN RULES AND DESIGN PATTERNS
Unit 2 HCI DESIGN RULES AND DESIGN PATTERNSUnit 2 HCI DESIGN RULES AND DESIGN PATTERNS
Unit 2 HCI DESIGN RULES AND DESIGN PATTERNSvrgokila
 
Unit 2 hci
Unit 2 hciUnit 2 hci
Unit 2 hcivrgokila
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updatedvrgokila
 
Unit 1 ocs752 introduction to c programming
Unit 1 ocs752 introduction to c programmingUnit 1 ocs752 introduction to c programming
Unit 1 ocs752 introduction to c programmingvrgokila
 

Mais de vrgokila (6)

evaluation technique uni 2
evaluation technique uni 2evaluation technique uni 2
evaluation technique uni 2
 
Unit 2 HCI DESIGN RULES AND DESIGN PATTERNS
Unit 2 HCI DESIGN RULES AND DESIGN PATTERNSUnit 2 HCI DESIGN RULES AND DESIGN PATTERNS
Unit 2 HCI DESIGN RULES AND DESIGN PATTERNS
 
Unit 2 hci
Unit 2 hciUnit 2 hci
Unit 2 hci
 
Array 31.8.2020 updated
Array 31.8.2020 updatedArray 31.8.2020 updated
Array 31.8.2020 updated
 
Unit 1 ocs752 introduction to c programming
Unit 1 ocs752 introduction to c programmingUnit 1 ocs752 introduction to c programming
Unit 1 ocs752 introduction to c programming
 
Array
ArrayArray
Array
 

Último

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSrknatarajan
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spaintimesproduction05
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringmulugeta48
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 

Último (20)

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICSUNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
UNIT-IFLUID PROPERTIES & FLOW CHARACTERISTICS
 
Vivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design SpainVivazz, Mieres Social Housing Design Spain
Vivazz, Mieres Social Housing Design Spain
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
chapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineeringchapter 5.pptx: drainage and irrigation engineering
chapter 5.pptx: drainage and irrigation engineering
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 

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; }
  • 2.
  • 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; }