0% found this document useful (0 votes)
6 views15 pages

Cprogramming

c programming

Uploaded by

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

Cprogramming

c programming

Uploaded by

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

Q)Explain Datatype Conversion in C with examples.

Type conversion is converting one type of data to another


type. It is also known as Type Casting.
 There are two types of type conversion:
 Implicit Type Conversion
 This type of conversion is usually performed by the
compiler when necessary without any commands
by the user.It is also called Automatic Type Conversion.
 Explicit Type Conversion
 These conversions are done explicitly by users using the
pre-defined functions.
Write the difference between algorithm and flowchart

Input/output operations
In C programming, input/output (I/O) operations are
categorized into two main types:
1)formatted –
EX: print (), Scanf()
2)unformatted-
EX:getchar() putchar()
gets() puts()

Q)Explain Formatted Input/output in C


Printf():
The printf() function is used to write data to the standard output. It is
defined in the standard library stdio.h.
The syntax for printf()
printf("format string", list of arguments);

Scanf()
The scanf() function is used to read data from the standard input
(usually the keyboard). It is also defined in the standard library
stdio.h.
The syntax for scanf()
scanf("format string", &variable1, &variable2, ...);
Q) Explain Unformatted input/output in C

To read and write a character the following I/O are used

getchar():
The getchar()function is used to read a single character
from the standard input stream, typically the keyboard.
Syntax:
Variablename=getchar();
putchar():
The putchar()functon is used to write a single character to
the standard output, which is typically the console.It is part
of the<stdio.h>header file.
Syntax:
int putchar(int character);

To read and write strings the following I/O is used


In C programming, gets() and puts()are functions used
for handling string input and output, respectively.

gets()Function
The gets() function is used to read a string from standard
input (typically the keyboard) and store it in a character
array.
Syntax: char *gets(char *str);
puts() Function
The puts()function is used to write a string to standard
output (typically the console).

Syntax:int puts(const char *str);


Q) Explain Pretest loop(for and while loop) and post test
loop(do-while loop)

Q)Discuss why C is called a mid-level , structured &amp;


Programmer’s Language
C is a middle-level language
•  It bridges the gap between high-level and low-level
languages.
•  Supports structured programming and is portable.

C is a structured language
• Encourages breaking down a program into smaller,logical
functions or blocks of code.
• Readability and maintenance: This modularity makes
programs easier to understand, debug, and modify.

C is a programmers language
• Gives programmers explicit control over memory and
system resources.
• Produces fast and efficient programs due to its powerful
operators and data types.
Q)Explain the brief history of c
programming
The C programming language has a rich history rooted
in the development of operating systems.
• C evolved from earlier languages like ALGOL, BCPL, and B,
with B being a simplified version of BCPL.
• Dennis Ritchie developed C in 1972 at Bell Labs, taking the
ideas from B and adding new features like data types.
• The primary motivation for creating C was to rewrite the
Unix operating system, which was initially written in
assembly language.
• C quickly became the primary language for system
programming, due to its ability to provide low-level access
to memory and its efficient performance.
• Over time, C has been standardized by organizations
like ANSI (American National Standards Institute) and
ISO (International Organization for Standardization),

Q) Draw a flow chart and write c programme to compute the


simple intrest
#include <stdio.h>
int main() {
int p, t;
float r;
printf("enter the principle amount\n");
scanf("%d", & p);
printf(" enter the time period of repayment\n");
scanf("%d", & t);
printf("enter the rate of interest for lending\n");
scanf("%f", & r);
printf("The simple interest is %f", prt / 100);
return 0;
}

Q) What is conditional operator? (ternary operator)


We use the ternary operator in C to run one code
when the condition is true and another code when
the condition is false
Syntax :
Testcondition?expression1:expression 2
Q)Write a C program to find the largest of three
numbers using ternary operator
#include <stdio.h>
int main() {
int num1, num2, num3, largest;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
largest = (num1 > num2) ? ((num1 > num3) ? num1 : num3) :
((num2 > num3) ? num2 : num3);
printf("The largest number is: %d\n", largest);
return 0;
}
Q)Design algorithm and flowchart to check whether a given
number is palindrome or not a palindrome.

Step 1: Start
Step 2: Read the input number from the user
Step 3: Declare and initialize the variable reverse and assign input to a
temp variable tempNum=num
Step 4: Start the while loop until num !=0 becomes false
rem = num % 10
reverse*= 10 + rem
num = num / 10
Step 5 : Check if reverse == tempNum
Step 6: If it’s true then the number is a palindrome
Step 7: If not, the number is NOT a palindrome
Step 8: Stop
Write a C program to find sum of n natural
numbers using do-while.
#include<stdio.h>
int main()
{
int n, i = 1, Sum = 0;

printf("Please Enter any Integer Value\n");


scanf("%d", &n);

do
{
Sum = Sum + i;
i++;
} while(i <= n);

printf("Sum of Natural Numbers = %d", Sum);


return 0;
}

Q)Write a C program to compute electricity bill for the


following data.
Hint:
bill=unitsConsumed*charges per unit
#include <stdio.h>

int main() {
int unitsConsumed;
float totalBill = 0.0;

printf("Enter the number of units consumed: ");


scanf("%d", &unitsConsumed);

if (unitsConsumed <= 100) {


totalBill = unitsConsumed * 0.5;
} else if (unitsConsumed <= 300) {
totalBill = (100 * 0.5) + ((unitsConsumed - 100) * 0.75);
} else if (unitsConsumed <= 400) {
totalBill = (100 * 0.5) + (200 * 0.75) + ((unitsConsumed - 300) * 1.0);
} else if (unitsConsumed <= 500) {
totalBill = (100 * 0.5) + (200 * 0.75) + (100 * 1.0) + ((unitsConsumed -
400) * 1.5);
} else { // unitsConsumed >= 501
totalBill = (100 * 0.5) + (200 * 0.75) + (100 * 1.0) + (100 * 1.5) +
((unitsConsumed - 500) * 1.75);
}

printf("Electricity Bill: %.2f\n", totalBill);

return 0;
}

You might also like