Professional Documents
Culture Documents
Also, you may notice that variable data types like boolean
or string do not exist in C language.
Here, we have used 3 data types:
They are int, float and char. Int is used to store whole
numbers. Floats are used to store decimals. Char is used
to store special characters.
4. Program Structure:
There are three things that you would write all the
same in all programs and that is:
● #include<stdio.h>
● int main(){
● return 0
Between int main (){ and return 0 you would write your
code, and after each line you should put a semicolon. This
is considered as a full-stop in C-Language and it
terminates the statement. Also, you could’ve written
printf(“Hello World!”); and return 0; command in
same line followed by a semicolon at the end of each
statement.
5. Comments:
But, if you want to print the same text let’s say 5 times
and each time you want it on a new line, you would use
the \n within your text like this:
#include <stdio.h>
int main() {
#include <stdio.h>
int main() {
return 0;
}
6. Input:
#include <stdio.h>
void main(){
int age;
printf("Enter your age and we will print it on the
screen: ");
scanf("%d" , &age);
printf("age is: %d ", age);
return 0;
}
#include <stdio.h>
void main(){
int num1, num2;
printf("The sum of the two numbers you entered are: %d", num1 +
num2);
return 0;
}
#include <stdio.h>
void main(){
int side;
printf("Enter the length of a single side of
square to find its area: ");
scanf("%d", &side);
return 0;
}
#include <stdio.h>
void main(){
float radius;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
printf("The area of the circle is: %f",
3.14*radius*radius);
return 0;
}
The output of the code above is:
Part(a) code:
#include <stdio.h>
return 0;
Output:
Part B:
#include <stdio.h>
void main(){
int number;
printf("Enter a number and gets its cube printed on screen: ");
scanf("%d", &number);
Important Note:
Arithmetic instructions:
#include <stdio.h>
#include <math.h>
int main() {
int a,b;
a = b = 2;
int power = pow(a,b);
printf("%d", power);
return 0;
}
int a;
a = 3%2;
printf("%d", a );
}
Assignment Operators
The multiplicative, division and modular
operators are preferred first and then the
addition and subtraction operator.
Another example:
In logical OR, if any one statement is true, then
the output will be true as well!
,
#include <stdio.h>
int main(){
int day;
printf("Enter a day(1-7): ");
scanf("%d", &day);
switch(day){
case 1: printf("The day is monday\n");
break;
case 2: printf("The day is tuesday\n");
break;
case 3: printf("The day is wednesday\n");
break;
case 4: printf("The day is thursday\n");
break;
case 5: printf("The day is friday\n");
break;
case 6: printf("The day is saturday\n");
break;
case 7: printf("The day is sunday\n");
break;
default: printf("There are 7 days in a week, enter a
valid day\n");
break;
}
We can also write the same switch-case program
by taking input a character from the user
Chapter 4: Loop Control instructions
Here, weve set the value of the counter used in for loop
as a floating value.The output will similarly be printed
in floating form:
This loop will run until it drains all the PC’s memory.
int n;
printf("Enter A number: "); // if you enter 3,
you will get 1 + 2 + 3 and vice v.
scanf("%d", &n);
int sum;
sum = sum + i;
}
printf("The sum is %d", sum );
!)Special Case:
==================================
int n;
printf("Enter A number: "); // if you enter 3, you will get 1
+ 2 + 3 and vice v.
scanf("%d", &n);
int sum;
sum = sum + i;
}
printf("The sum is %d\n\n", sum );
Here, the printf outside the for loop block will throw
you an error, as the variable i was only used inside the
for loop and after that loop block gets executed, the
compiler forgets about the i variable.
int main() {
int n;
printf("Enter A number: "); // if you enter 3, you will get 1
+ 2 + 3 and vice v.
scanf("%d", &n);
int sum;
sum = sum + i;
printf("%d\n", j);
int n;
printf("enter a number: ");
scanf("%d", &n);
int multiple;
for(int i = 1; i<=10; i++ ){
multiple = i*n;
printf("%d\n", multiple);
}
return 0;
}
Output:
// Online C compiler to run C program online
#include <stdio.h>
int main() {
int n;
do{
printf("Enter an even number, if you enter an odd number,
loop will end: ");
scanf("%d", &n);
printf("%d\n", n);
if(n%2!=0){
break;
}
} while(1);
printf("The Loop has ended");
if(n%7==0) {
break;
}
} while(1);
printf("The Loop has ended");
int main() {
for(int i=1; i<=5; i++){
if(i ==3){
continue;
}
printf("%d\n", i);
Output:
Here in this code, we have skipped 3 in the loop
and have printed numbers from 1 to 5
int main() {
for(int i=1; i<=10; i++){
if(i ==6){
continue;
}
printf("%d\n", i);
}
int main() {
for(int i=5; i<=50; i++){
if(i%2 == 0){
continue;
}
printf("%d\n", i);
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
int fact = 1;
}
printf("%d\n", fact);
int n;
printf("enter a number: ");
scanf("%d", &n);
int multiple;
for(int i = 10; i>=1; i-- ){
multiple = i*n;
printf("%d\n", multiple);
}
return 0;
}
int main() {
int sum;
}
printf("%d\n", sum);
}
Question a code:
#include <stdio.h>
int main() {
int rows = 4; // You can adjust the number of rows as needed
return 0;
}
Question b code
int main() {
int n, count;
for(int i=1;i<=n;i++){
if(n%i==0){
count++;
}
}
if(count ==2)
printf("Number you entered is a prime number!");
else
printf("Number you entered is not a prime number");
return 0;
}
Misc Topic: How to write if-statment with bool:
bool tempIsCold;
int temp;
if(tempIsCold){
printf("woof it's cold");
else{
printf("It's not cold!");
}
}
==================================
After this we can call the function in our code like this:
void printHello();
void printGoodBye();
int main() {
printHello();
printGoodBye();
void printHello(){
printf("Hello!\n");
}
void printGoodBye(){
printf("Good Bye!\n");
}
void printHello(){
printf("Hello!\n");
}
void printGoodBye();
int main() {
printHello();
}
But the conventional way is to write the way we wrote
above.
==================================
#include <stdio.h>
void printNamaste(){
printf("Namaste!\n");
}
void printbonjour(){
printf("Bonjour!\n");
int main() {
char ch;
printf("Press F if you are French, Press I if you are an indian: ");
scanf("%c", &ch);
else{
printf("invalid input! Enter a valid character.");
}
}
First point: It doesn’t matter what function you declare
first above or below main, the compiler goes
straight to the main function to declare all
functions.
#include <stdio.h>
int main() {
int a,b;
printf("Enter First Number: ");
scanf("%d", &a);
int s = sum(a,b);
printf("sum is %d", s);
}
We can name the variable n differently even in both
functions, but when we name a certain variable inside a
function, the scope of that variable is confined to that
function itself and not outside of it. If you’ve named a
variable n inside main() function, you cannot use it
outside that function directly!
To print a Table of a number thru a function, we will
take the input from the user:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printTable(n);
}
#include <stdio.h>
#include <math.h>
int main() {
float n;
printf("%f", pow(n,2));
}
// Online C compiler to run C program online
#include <stdio.h>
#include <math.h>
int main() {
int radius;
printf("Enter the radius of circle to calculate its area: ");
scanf("%d", &radius);
int sqLength;
printf("Enter the length of square to calculate its area: ");
scanf("%d", &sqLength);
printf("%d\n", c);
printf("%d\n", sq);
return 0;
}
}
An alternative code can be:
int main() {
int radius;
printf("Enter the radius of circle to calculate its area: ");
scanf("%d", &radius);
int sqLength;
printf("Enter the length of square to calculate its area: ");
scanf("%d", &sqLength);
int a;
printf("Enter length of first side of rectangle: ");
scanf("%d", &a);
int b;
printf("Enter length of second side of rectangle: ");
scanf("%d", &b);
return 0;
}
}
int rectArea(int a, int b){
return a*b; // Here we have done the task differently.
}
================================
Recursion
int main() {
return 0;
}
if (count ==0){ /*When count = 0, the function would return to main code */
return;
}
printf("Hello World!\n");
printHW(count-1); //Since its already printed once, we add count - 1 here.
1 is the main
function call:
In this type of
call, some
function calls
another function
and that function calls another function for example
and returns some value to the second function, the
second function then returns the value to the main
function. This is how normal functions work.
Second type of
call is a
recursive
function call, in
this case, a single
functions calls itself and finally returns a value to the
main function as we saw above. This whole process is
called recursion.
Here, what we can do
is first find some till
(n-1) numbers, then
later we can add n to
it. Let’s say if n = 3,
we’ll first find sum for
numbers that go till
n-1, in this case n - 1 or 3-1 = 2, that means sum till 2
numbers before 3, we’ll add that first and then add that
part to 3.
Here, 1 is the
base value in
which we will return some specific value given by us, in
this case that value will be 1 as sum of 1 is 1 ofc
Code:
#include <stdio.h>
int main() {
int n;
printf("Enter a number you want sum till: ");
scanf("%d", &n);
printf("The sum is %d", sum(n)); // Takes argument for the function from user
return 0;
}
int sumNm1 = sum(n-1); //Gets the sum till n-1 and stores in sumNm1
int sumN = sumNm1 + n;
return sumN;
}
Output:
sum(1) is called the base Case, where the whole
work is done, here n = 1, it then returns the value to n
=2 or sum(2).
Code:
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if(n ==0){
return 1;
}
int main() {
int temp;
printf("Enter Temperature in Celsius: ");
scanf("%d", &temp);
int main() {
// Write C code here
int n;
printf("Enter a fibonucci term to print fibonacci value for that term number: ");
scanf("%d", &n);
printf("%d", fib(n)); /* returns the sum of all values till n, if you enter 7, it
will give the sum till 7 terms */
}
if(n == 0 || n ==1 ){
if(n==0){
return 0;
}
if(n==1){
return 1;
}
}
return fibN;
}
Addtional problem: Write a code to print fibonacci
sequence thru for loops:
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next;
return 0;
}
Additional Problem: Print these sequences below:
int main() {
int n, first = 0, second = 3, next;
printf("Series:\n ");
return 0;
}
Seq2 code:
#include <stdio.h>
int main() {
int n, first = 7, second = 5, next;
printf("Series:\n ");
return 0;
}
Seq3 Code:
#include <stdio.h>
int main() {
int n, first = 70, second = 5, next;
printf("Series:\n ");
return 0;
}
Question(a) Code:(sum of digits)
#include <stdio.h>
return sum;
}
int main() {
int num;
return 0;
}
Question(b) Code:(square root):
#include <stdio.h>
#include <math.h>
int main() {
double number;
return 0;
}
Question(c) (Temperature Question)
int main() {
int inp;
temp(inp);
}
Question(d) (Power of a number)
// Online C compiler to run C program online
#include <stdio.h>
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("%d", power(n));
==================================
We can declare
pointers as
integers, floats and
characters. It
depends on what
type of address
you have stored in
that specific
variable. If you
have used an
integer let’s say int age(like we used above), you
need to create an int pointer.
Are the same, and that is because they are the same
thing, &age and ptr are same, but if you add &ptr, it
would mean that you are trying to call the address of
that specific pointer, in that case you will have a
different output.
Since we have used the *ptr operator, the value of x
which is essentially *ptr has been
declared to zero
We will use *n
to write the
integer. And
then when
passing the
argument we
can use &n.
Here, int* as
parameter
means that we
need to pass a pointer argument.
int main() {
int n =4;
printf("The address in the main function is: %u", &n);
printf("\n");
printAddress(n);
return 0;
}
int main() {
int n =4;
printf("The address in the main function is: %u", &n);
printf("\n");
printAddress(&n);
return 0;
}
We
can directly use pointers so that we can access the
value of the variables that are present.
Explanation of this concept:
int main() {
int marks[03];
printf("Physics marks are %d, Chemistry Marks are %d, Math marks are %d", marks[0],
marks[1], marks[2]);
return 0;
}
Notice if you try to take inputs for extra elements(i.e
elements more than the array length), you will get
errors:
int main() {
float equipPrice[3] = { 9.25, 6.25 , 7.50};
return 0;
}
Pointer arithmetic
7:04:25