KEMBAR78
Control Structures - I | PDF | Integer (Computer Science) | Computer Engineering
0% found this document useful (0 votes)
7 views40 pages

Control Structures - I

Uploaded by

janirubro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

Control Structures - I

Uploaded by

janirubro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Programming

Fundamentals
Lecture 05 :
C Programming

2
Printing values assigned to variables
Integer (int)
Format specifier - %d
#include<stdio.h>
int main()
{
int num=5; // declaration of the variable
printf("%d",num); //print the value assigned to the
variable
return 0;
} 3
Printing values assigned to variables
Float
Format specifier - %f
#include<stdio.h>
int main()
{
float num=5.21;
printf("%f",num);
return 0;
}
4
Printing values assigned to variables
Double
Format specifier - %lf
#include<stdio.h>
int main()
{
double num=5.213456;
printf("%lf",num);
return 0;
}
5
Printing values assigned to variables
Character - char
Format specifier - %c
#include<stdio.h>
int main()
{
char letter=‘Z’;
printf("%c",letter);
return 0;
}
6
Example
Write a C program to display following values assigned to
variables.
ID = 101
Grade = A
tot = 85
average = 80.25

7
#include<stdio.h>
int main()
{
int ID=101;
char grade='A';
int total=85;
float average=80.25;
char name[5]="Rose";
printf("The ID is %d\n",ID);
printf("Grade is %c\n",grade);
printf("Total is %d\n",total);
printf("Average is %f\n",average);
printf("Name is %s",name);
return 0;
}
Exercise
Write a C program to declare four variables with different
data types and assigned values for each and display the
values.

9
Defining string variable
In C programming we define string values by using an
array of characters.

char variable_name[size];

Example :

char name[10];
char name[10]=“Dushy”;
Printing values assigned to variables
String
Format specifier - %s
#include<stdio.h>
int main()
{
char name[5]=“Rose”;
printf("%s",name);
return 0;
}
11
Exercise 1
Write a C program to display your name and age.

Name = your name


Age = your age

Output :
My name is …………..
My age is ……………..
Exercise 1 - Code
#include<stdio.h>
int main()
{
char name[6]="Dushy";
int age=26;
printf("My name is %s\n",name);
printf("My age is %d\n",age);
return 0;
}
Exercise 2
Write a C program to store 50 and 4 into two variables
and display the addition, subtraction, division and
multiplication of two numbers.
Exercise 2 - Code
#include<stdio.h>
int main()
{
int num1 = 50,num2=3;
int add,sub,mul;
float div;
printf("Number 1= %d\n",num1);
printf("Number 2= %d\n",num2);
add=num1+num2;
To reduce the decimal places in float and
sub=num1-num2;
mul=num1*num2; double values we can use ;
div=num1/num2;
printf("Addition is %d\n",add); “%.2f” , “%.2lf” // two decimal values
printf("Substraction is %d\n",sub);
Eg:- 51.23
printf("Multiplication is %d\n",mul);
printf("Division is %.2f\n",div);
return 0;
}
Exercise 3
Write the C program to calculate the area and
perimeter of a rectangle.

width = 10
height = 5
Exercise 3 - Code
#include<stdio.h>
int main()
{
int height = 5 , width=10;
int area,perimeter;
printf("The height is %d\n",height);
printf("The width is %d\n",width);
area=height*width;
perimeter=2*(height+width);
printf("The area is %d\n",area);
printf("The perimeter is %d\n",perimeter);
return 0;
}
User inputs
• To make an application dynamic to provide
responses to users’ data and behaviours we need
to accept inputs from the user.
• In C , we can accept user inputs through a
console window.
• We use scanf( ) function to accept inputs from
the user.
User inputs contd..
Syntax :

scanf (“%<format specifier>”,&<var_name>);

Example : Consider int value.

scanf(“%d”,&num);
Example
Take a number from the user and display the value
entered by the user.
include<stdio.h>
int main()
{
int num; //declare variable
printf("Enter a number :"); //display message
scanf("%d",&num); //read the value entered by the user
printf("The number entered is %d",num); //display the
value

return 0;
}
Example
Take a number from the user and display the value entered
by the user.
int main()
{
int num;
printf("Enter a number :"); //display a message to user
scanf("%d",&num); //read the number entered by user
printf("The numbers is %d",num); //display number entered
return 0;
}
Exercise 1
Write a C program to input student ID,name and age
and display them.
Exercise 1
Write a C program to input student ID,name and age
and display them.
Exercise 1 - Code
#include<stdio.h>
int main()
{
int ID,age;
char name[10];
printf("Enter your ID :");
Output : -
scanf("%d",&ID);
printf("Enter your name :");
scanf("%s",&name);
printf("Enter your age :");
scanf("%d",&age);
printf("The ID is %d\n",ID);
printf("The name is %s\n",name);
printf("The age is %d\n",age);
return 0;
}
Exercise 1 – Code(II method)
#include<stdio.h>
int main()
{
int ID,age;
char name[10];
printf("Enter your ID :");
scanf("%d",&ID);
Output : -
printf("Enter your name :");
scanf("%s",&name);
printf("Enter your age :");
scanf("%d",&age);
printf("The ID is %d\n",ID);
printf("The name is %s\n",name);
printf("The age is %d\n",age);
return 0;
}
Exercise 2
Write a C program to take two numbers from the user
and display the total of two numbers.
Exercise 2 - Code
#include<stdio.h>
int main()
{
int num1,num2,tot=0;
printf("Enter number 1:");
scanf("%d",&num1); Output : -
printf("Enter number 2:");
scanf("%d",&num2);
tot=num1+num2;
printf("The total is %d",tot);
return 0;
}
Exercise 3
Write a C program to calculate the area and perimeter
of a rectangle.
(Hint : you have to take height and width from the
user)
Exercise 3 - Code
#include<stdio.h>
int main()
{
int height,width,area=0,perimeter=0;
printf("Enter height:");
scanf("%d",&height);
printf("Enter width:"); Output : -
scanf("%d",&width);
area=height*width;
perimeter=(height+width)*2;
printf("The area is %d\n",area);
printf("The perimeter is %d\n",perimeter);
return 0;
}
Exercise 4
Write a C program to get the user age and display the
user’s age after 20 years.
#include<stdio.h>
int main()
{
//variable declaration
int age,n_age;
//taking user inputs
printf("Enter your age :");
scanf("%d",&age);

//calculating new age


n_age=age+20;
//output the result
printf("Now you are %d years old\n",age);
printf("In 20 years you are %d years old\n",n_age);
return 0;
}
Exercise 4 - Code
#include<stdio.h>
int main() Output : -
{
int age,n_age;
printf("Enter your age :");
scanf("%d",&age);
n_age=age+20;
printf("Your age in 20 years is %d",n_age);
return 0;
}
Exercise 5
Write a C program to input temperature in Celsius(C)
and convert it into Fahrenheit and display.
Hint : F=C*9/5+32
#include<stdio.h>
int main()
{
//variable declaration
float c_temp,f_temp;
//taking user inputs
printf("Enter temperature :");
scanf("%f",&c_temp);

//calculating farenhite temperature


f_temp=c_temp*9/5+32;
//output the result
printf("Temperature in Farenhite :%.2f",f_temp);
return 0;
}
Activity
• Write a C program to calculate the total and average of
three numbers that are entered by the user.
Exercise 6 - Code
#include<stdio.h>
int main()
{
int num1,num2,num3,tot=0;
float avg=0;
printf("Enter number 1:");
scanf("%d",&num1);
printf("Enter number 2:");
scanf("%d",&num2);
printf("Enter number 3:");
scanf("%d",&num3);
tot=num1+num2+num3;
avg=(float)tot/3; //converting int data type into float data type to calculate the average
printf("Total is %d\n",tot);
printf("Total is %.2f\n",avg);
return 0;
}
Exercise 6
Write a C program to accept unit price(float),quantity
sold(int),discount rate(float) from the user and display
the sales amount according to the following formulae.
Sales amount=unit price*quantity sold
Total bill=sales amount-(sales amount*discount rate)
#include<stdio.h>
int main()
{
//variable declaration
float u_price,d_rate,sales=0,tot_bill=0;
int qty;
//taking user input
printf("Enter unit price :");
scanf("%f",&u_price);
printf("Enter quantity sold :");
scanf("%d",&qty);
printf("Enter discount rate :");
scanf("%f",&d_rate);
//calculating sales amount & total bill
sales=u_price*qty;
tot_bill=sales-(sales*d_rate);
//output the result
printf("Sales amount :%.2f\n",sales);
printf("Total bill amount :%.2f\n",tot_bill);
return 0;
}
C Programming
• Any questions before finish?
• Are you sure?
• Thank you for listening.

40

You might also like