Download Programming in C Notes
Programming in C Notes: Roshan Khatri, Junior Professor, Nepal Engineering College
Pokhara University Tech Club C Programming Solution (Roshan Lamichane et. al)
Course Objectives:
The object of this course is to acquaint the students with the basic principles of programming and development of software systems. It encompasses the use of programming systems to achieve specified goals, identification of useful programming abstractions or paradigms, the development of formal models of programs, the formalization of programming language semantics, the specification of program, the verification of programs, etc. the thrust is to identify and clarify concepts that apply in many programming contexts:
Programming Language
A programming language is a formal language comprising a set of commands, instructions and other syntax to create a software program.
Machine Language
A machine language is the language written as a string of binary 1's and 0's. It is the only language which a computer understands without using a translation program.
Assembly Language
A assembly language is a low level programming language that allows a user to write a program using alphanumeric mnemonic codes, instead of numeric code for a set of instruction. It requires a translator known as assembler to convert assembly language into a machine language. Here instructions are given in English like ADD, SUM, MOV etc.
High Level Language
A high level language is a machine independent language, means it is portable. It enables a user to write a program in a language which resembles English words and familiar mathematical symbols. The language in this category is Pascal, Cobol, Fortran etc.
Chapter 1
Introduction
History of computing and computers, programming, block diagram of computer, generation
of computer, types of computer, software, Programming Languages, Traditional and
structured programming concept
Chapter 2
Programming logic
Problems solving(understanding of problems, feasibility and requirement analysis) Design
(flow Chart & Algorithm), program coding (execution, translator), testing and debugging,
Implementation, evaluation and Maintenance of programs, documentation
Chapter 3
Variables and data types
Constants and variables, Variable declaration, Variable Types, Simple input/output
function, Operators
Chapter 4
Control Structures
Introduction, types of control statements- sequential, branching- if, else, else-if and switch
statements, case, break and continue statements; looping- for loop, while loop, do—while
loop, nested loop, goto statement
Chapter 5
Arrays and Strings
Introduction to arrays, initialization of arrays, multidimensional arrays, String, function
related to the strings
Chapter 6
Functions
Introduction, returning a value from a function, sending a value to a function, Arguments,
parsing arrays and structure, External variables, storage classes, pre-processor directives,
C libraries, macros, header files and prototyping
Chapter 7
Pointers
Definition pointers for arrays, returning multiple values form functions using pointers.
Pointer arithmetic, pointer for strings, double indirection, pointer to arrays, Memory
allocation-malloc and calloc
Chapter 8
Structure and Unions
Definition of Structure, Nested type Structure, Arrays of Structure, Structure and Pointers,
Unions, self-referential structure
Chapter 9
Files and File Handling
Operating a file in different modes (Real, Write, Append), Creating a file in different modes
(Read, Write, Append)
Laboratory:
Laboratory work at an initial stage will emphasize on the verification of programming concepts learned
in class and use of loops, functions, pointers, structures and unions. Final project of 10 hours will be
assigned to the students which will help students to put together most of the programming concepts
developed in earlier exercises.
Textbooks:
1. Programming with C, Byran Gottfried
2. C Programming, Balagurusami
References
1. A book on C by A L Kely and Ira Pohl
2. The C Programming Language by Kerighan, Brain and Dennis Ritchie
3. Depth in C, Shreevastav
Sample C Programs
/************************************************************************
\ We hope that you have some knowledge of c programming and *
/ We recommend you to go through the chapter briefly before coming to this solution *
\ We have leave other exercise on your own and we only focus on the programming section
/ Note:
\ This is just for educational purpose
/ Feedback: aakrist666@gmail.com
\Tested on Code::Block 16.01,borlandC
*/
/* This program read Ramesh's basic salary from user
And after deducting the allowances it show his gross salary*/
#include<stdio.h>
#include<conio.h>
int main()
{
float basic_salary,dearnessAllowance,houseRentAllowance,gross_salary;
printf("Please enter Ramesh Basic Salary:");
scanf("%f",&basic_salary); //scan Ramesh basic salary
dearnessAllowance=basic_salary*0.4;
houseRentAllowance=basic_salary*0.2;
gross_salary=basic_salary-dearnessAllowance-houseRentAllowance;//calculation for gross salary
printf("The gross salary of Mr.Ramesh is:%0.2f",gross_salary); // 0.2f is for 2 decimal place output you can use %f also
getch();
}
/*This program read the distance between two cities in KM and convert the distance in meters, feet, inches centimeters*/
/*This program requires some mathematical conversions*/
#include<stdio.h>
main()
{
float distanceKM,distanceInMeter,distanceInFeet,distanceInInche,distanceInCentimeter;
printf("Enter the distance between 2 cities:");
scanf("%f",&distanceKM);
distanceInMeter=distanceKM*1000;
distanceInFeet=distanceKM*32800.8333;// 1 km=32800.8 foot
distanceInInche=distanceKM*39370.0778;
distanceInCentimeter=distanceKM*100000;
printf("distance in \nMeter=%0.3f meters \nfeets=%0.3f feets \nInches=%0.3f inches \nCentimeters=%0.3f centimeters",distanceInMeter,distanceInFeet,distanceInInche,distanceInCentimeter);//%0.nf will print out the answer upto n decimal place
getch();
}
/* This program will read the marks of student in five different subject and print out
percentage
aggregrate marks
Note that the program will only work if the marks in all subject is less than hundred and greater than equal
We can add more features by using some more if else blocks
*/
#include<stdio.h>
int main()
{
int mark_physics,mark_math,mark_English,mark_IT,mark_Economics,totalMarks;
float percentage;
printf("Enter the marks in respective subject:\nPhysics:");
scanf("%d",&mark_physics);
printf("Mathematics:");
scanf("%d",&mark_math);
printf("English:");
scanf("%d",&mark_English);
printf("Information Technology:");
scanf("%d",&mark_IT);
printf("Economics:");
scanf("%d",&mark_Economics);
totalMarks=mark_physics+mark_math+mark_English+mark_IT+mark_Economics;
percentage=totalMarks/5;
if((mark_physics<=100)&&(mark_math<=100)&&(mark_English<=100)&&(mark_IT<=100)&&(mark_Economics<=100)&&(mark_physics>0)&&(mark_math>0)&&(mark_English>0)&&(mark_IT>0)&&(mark_Economics>0))
printf("Total Marks:%d\nPercentage:%f",totalMarks,percentage);
}
/*This program reads the temperature of city in Fahrenheit degrees and convert in celsius scale*/
/* We know:
C/100=(F-32)/180
or, C=(F-32)*(100/180)
or, C=(F-32)*(5/9)............(i)
Since, We are using float not integer for better precision
we are not using eqn(i) to calculate C , if we use eqn(i) we always get answer 0.00 as (5/9)=0.000 (float division)
So we should change eqn(i) in better format, you can do it on your own style and I am using following format:
C=(5*fTemp-160)/9................(ii)
*/
#include<stdio.h>
#include<conio.h>
int main( )
{
float fTemp,cTemp;
printf("Please input temperature of city in Fahrenheit:");
scanf("%f",&fTemp); // Read the temperature in Fahreinheit scale
cTemp=(5*fTemp-160)/9;// converting in Celcius Scale using eqn (ii)
printf("Temperature in celsius is %0.3f",cTemp); //%0.nf limits the output upto n decimal place,
getch();
}
/*This progareaOfRectanglem input the length and breadth for rectangle and areaOfRectangledius for circle
It printout area and perimeter for rectangle adn area and circumference for circle
*/
/*This program is all about some basic mathematics formula of geometry*/
/* %0.nf is not allowed in scanf*/
#include<stdio.h>
#include<conio.h>
main()
{
float lengthOfRectangle,breadthOfRectangle,areaOfRectangle,perimeterOfRectangle,radiusOfCircle,areaOfCircle,circumferenceOfCircle;
printf("Length of rectangle:");
scanf("%f",&lengthOfRectangle);
printf("breadth of rectangle:");
scanf("%f",&breadthOfRectangle);
printf("Radius of circle:");
scanf("%f",&radiusOfCircle);
areaOfRectangle=lengthOfRectangle*breadthOfRectangle;
perimeterOfRectangle=2*(lengthOfRectangle+breadthOfRectangle);
areaOfCircle=3.14*radiusOfCircle*radiusOfCircle;
circumferenceOfCircle=2*3.14*radiusOfCircle;
printf("Area of rectangle is %0.3f\nPerimeter is %0.3f",areaOfRectangle,perimeterOfRectangle);
printf("\nArea of circle is %0.3f\nCircumference is%0.3f",areaOfCircle,circumferenceOfCircle);
getch();
}
/* This program read two number from the keyboard and store in c and d and will switch their contents*/
#include<stdio.h>
main()
{
int c,d;
printf("Enter the value for C:");
scanf("%d",&c);
printf("Enter the value for D:");
scanf("%d",&d);
printf("Before Swapping the value of C is %d and D is %d\n",c,d);
printf("After swapping C=%d D=%d",d,c);// interchanging of the contents
}
/*program to clculate the cp of 15 items when sp and profit are entered through keyboard as a whole*/
/* just another simple mathematical program*/
/* You can extend the program as you like */
#include<stdio.h>
main()
{
float sellingPrice,totalProfit,totalCostPrice,singleCostPrice;
printf("SP of 15 items is:");
scanf("%f",&sellingPrice);
printf("Profit in the 15 items is :");
scanf("%f",&totalProfit);
totalCostPrice=sellingPrice-totalProfit;
singleCostPrice=totalCostPrice/15;
printf("The total CP of the 15 items is:%f\n",totalCostPrice);
printf("Cp of one item %f",singleCostPrice);
}
/*Program to find out the sum of digits of 5 digited inputNumberber*/
#include<stdio.h>
main()
{
int inputNumber,sumOfDigits,a,b,c,d,e,d1,d2,d3,d4,d5;
printf("Please input the five digit inputNumberber:");
scanf("%d",&inputNumber);
a=inputNumber/10;
d5=inputNumber%10;
b=a/10;
d4=a%10;
c=b/10;
d3=b%10;
d=c/10;
d2=c%10;
e=d/10;
d1=d%10;
sumOfDigits=d1+d2+d3+d4+d5;
printf("The sumOfDigits of digits of %d is %d",inputNumber,sumOfDigits);
}
/* This program read a five digited number and calculate the sum of the number
eg: if user enter 12345 the computer return 15
*/
#include<stdio.h>
#include<conio.h>
int main()
{
int inputNumber,sumOfDigits;
int firstDigit,secondDigit,thirdDigit,fourthDigit,fifthDigit;
int fourDigited,threeDigited,twoDigited,oneDigited;
printf("Please enter five digited number:\n");
scanf("%d",&inputNumber);
if((inputNumber<=99999)&&(inputNumber>=9999))
{
//let's take the number as 12345
fourDigited=inputNumber/10; //1234
threeDigited=fourDigited/10; //123
twoDigited=threeDigited/10; //12
oneDigited=twoDigited/10; //1
fifthDigit=inputNumber%10; //5
fourthDigit=fourDigited%10; //4
thirdDigit=threeDigited%10; //3
secondDigit=twoDigited%10; //2
firstDigit=oneDigited%10; //1
sumOfDigits=firstDigit+secondDigit+thirdDigit+fourthDigit+fifthDigit;
printf("The sum of %d is %d",inputNumber,sumOfDigits);
printf("\n\nThe reverse of %d is %d%d%d%d%d",inputNumber,fifthDigit,fourthDigit,thirdDigit,secondDigit,firstDigit);
}
else
printf("Please enter the five digit number:");
getch();
}
/*
* C program to check if my compiler is following ANSI/IEEE Standard
*/
#include<stdio.h>
#include<conio.h>
int main()
{
double x=1e+9000;
double y=x*x;
printf("x=%e\ny=%e\n",x,y);
getch();
return 0;
}
// Prime Number Identification in C
#include<stdio.h>
#include<conio.h>
int main()
{
int num1,num2;
printf("Please input the number:");
scanf("%d",&num1);
num2=2;
while(num2<=num1-1)
{
if(num1%num2==0)
{
printf("%d is not a prime number",num1);
break;
}
num2++;
}
if(num2==num1)
printf("%d is a prime number",num1);
getch();
}
d
Programming in C Notes - Pokhara University, BSC CSIT, IOE
Reviewed by Nischal Lal Shrestha
on
February 13, 2021
Rating:
No comments: