SlideShare ist ein Scribd-Unternehmen logo
1 von 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;
}

Weitere ähnliche Inhalte

Was ist angesagt?

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 .
 

Was ist angesagt? (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 ...
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Mehr von 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
 

Kürzlich hochgeladen

Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
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
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdfSuman Jyoti
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfrs7054576148
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
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
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 
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
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 

Kürzlich hochgeladen (20)

Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
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 Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank  Design by Working Stress - IS Method.pdfIntze Overhead Water Tank  Design by Working Stress - IS Method.pdf
Intze Overhead Water Tank Design by Working Stress - IS Method.pdf
 
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Walvekar Nagar Call Me 7737669865 Budget Friendly No Advance Booking
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Intro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdfIntro To Electric Vehicles PDF Notes.pdf
Intro To Electric Vehicles PDF Notes.pdf
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
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
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
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
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
(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
 

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; }