[go: up one dir, main page]

0% found this document useful (0 votes)
15 views27 pages

Lecture_Week_6.1

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)
15 views27 pages

Lecture_Week_6.1

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/ 27

Introduction to Computing

and Programming

Loops and Arrays


Examples of Loop for Practice
1. Write a C program to find sum of n natural numbers
2. Write a C program to find the table of 2
3. Write a C program to check whether a number is even or not
4. Write a C program to check whether a number is Armstrong number or not
5. Write a C program to check whether a number is prime number or not
6. Write a C program to check whether a number is palindrome or not
7. Write a C program to display Fibonacci series
8. Write a C program to display a pyramid

https://www.geeksforgeeks.org/pattern-programs-in-c/
Write a C program to find sum of n natural numbers
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter a positive integer: "); // Input the value of n
scanf("%d", &n); // Make sure the input is a positive integer
if (n < 0) {
printf("Invalid input! Please enter a positive integer.\n");
return 0;}
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("Sum of the first %d natural numbers is: %d\n", n, sum);
return 0;}
Write a C program to find the table of 2
#include <stdio.h>

int main() {
int i;
// Print the multiplication table of 2
printf("Multiplication table of 2:\n");
for (i = 1; i <= 10; i++) {
printf("2 x %d = %d\n", i, 2 * i);
}
return 0;
}
#include <stdio.h>
int main() {
int num, reversedNum = 0, remainder, originalNum;
printf("Enter an integer: "); Check whether a
scanf("%d", &num); number is palindrome
originalNum = num; or not
while (num != 0) {
remainder = num % 10; // Get the last digit of the number
reversedNum = reversedNum * 10 + remainder; // Build the reversed number
num = num / 10; // Remove the last digit from the original number }
if (originalNum == reversedNum) {
printf("%d is a palindrome.\n", originalNum);
} else {
printf("%d is not a palindrome.\n", originalNum);
} return 0; }
#include <stdio.h> Write a C program to
int main() {
int num, originalNum, remainder, result = 0; check whether a number
printf("Enter a three-digit integer: "); is Armstrong number
scanf("%d", &num);
originalNum = num;
▪ Example:
while (originalNum != 0) { ▪ 153 is an Armstrong number because
remainder = originalNum % 10; 1^3+5^3+3^3=153.
result += remainder * remainder * remainder; ▪ 122 is not an Armstrong number
originalNum /= 10; because 1^3+2^3+2^3= 1+8+8 = 17 which
} is not equal to 122.
if (result == num) ▪ 9474 = 9^4+4^4+7^4+4^4 is an
printf("%d is an Armstrong number.", num); Armstrong number.
else
printf("%d is not an Armstrong number.", num);
return 0;
}
#include <stdio.h>
#include<math.h>
int main() {
int n;
printf("Enter the number to check"); Write a C program to
scanf("%d",&n);
int flag = 0;
check whether a
for (int i = 2; i<=sqrt(n); i++) { number is prime
if (n%i == 0) {
printf("%d is not prime", n);
number or not
flag = 1;
break; } }
if (flag == 0){ printf("%d is prime", n);}
return 0;}
#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
Fibonacci
printf("Fibonacci Series: %d, %d, ", t1, t2); Sequence
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2; }
return 0;}
Write a C program to display a pyramid
#include <stdio.h>

int main() {
int rows = 5; // Number of rows for the pyramid

// Outer loop to handle the number of rows


for (int i = 1; i <= rows; i++) {
// Inner loop to print stars for each row
for (int j = 1; j <= i; j++) {
printf("*");
}
// Move to the next line after printing each row
printf("\n");
}

return 0;
}
Write a C program to display a pyramid
#include <stdio.h>

int main() {
int rows = 5; // Number of rows for the pyramid

// Outer loop to handle the number of rows


for (int i = 1; i <= rows; i++) {
// Inner loop to print stars for each row
for (int j = 1; j <= i; j++) {
printf(“j");
}
// Move to the next line after printing each row
printf("\n");
}

return 0;
}
#include<stdio.h> #include<stdio.h>
int main() int main()
{ int n; { int n;
printf("Enter the value of n"); printf("Enter the value of n");
scanf("%d",&n); scanf("%d",&n);
for (int i =0; i < n; i++) for (int i =0; i < n; i++)
*********
{ for(int j = 0; j<i; j++) {for(int j = 0; j<i; j++) *******
*****
{ printf(" "); } { printf(" "); } ***
for (int k = 0; k < n-i; k++) for (int k = 0; k < n-i; k++){ *

{printf("*");} printf("*"); }
*****
printf("\n");} ****
for (int k = 0; k < n-i-1; k++)
} *** { printf("*");}
**
* printf("\n"); }}
for(int i = 0; i<n-1; i++) for(int i = 0; i<n-1; i++)
{ {
for(int j = 0; j < n-1-i; j++)
for(int j = 0; j < n-1-i; j++)
{
{ printf(" ");
printf(" "); }
} for(int k = 0; k <=i; k++)
{
for(int k = 0; k <=i; k++)
printf("*");
{ }
printf("*"); for(int k = 0; k <=i-1; k++)
* *
} **
{ ***
*** printf("*"); *****
printf("\n"); **** } *******
} printf("\n");
}
#include<stdio.h>
int main(){
int n;
printf("Enter the value of n");
scanf("%d",&n);
for(int i = 0; i<n-1; i++) {
for(int j = 0; j < n-1-i; j++) { printf(" "); } *
for(int k = 0; k <=i; k++) { printf("*"); } ***
for(int k = 0; k <=i-1; k++) { printf("*"); } *****
*******
printf("\n"); } *********
*******
for (int i =0; i < n; i++) { *****
for(int j = 0; j<i; j++) { printf(" "); } ***
for (int k = 0; k < n-i; k++) { printf("*"); } *
for (int k = 0; k < n-i-1; k++) { printf("*"); }
printf("\n"); }
}
Arrays
Introduction to Arrays

An array is a collection of Called as derived data All the elements of an


elements of the same type type. array occupy a set of
that are referenced by a contiguous memory
common name. locations.
Why need to use array type?

"We have a list of 1000 students' marks of an integer type. If


using the basic data type (int), we will declare something like the
following…“

int studMark0, studMark1, studMark2, ..., studMark999;


Issues if not using Arrays

• Can you imagine how long we have to write the declaration part by using normal variable
declaration?
int main(void)
{
int studMark1, studMark2, studMark3, studMark4, …, …, studMark998, stuMark999,
studMark1000;


return 0;
}
Problem can be solved using Array

• By using an array, we just declare


like this,
• int studMark[1000];
• This will reserve 1000 contiguous
memory locations for storing the
students’ marks.
• Graphically, this can be depicted as
in the following figure.
Arrays – one dimensional (1D)

• Fixed-size collection of similar data items stored in


contiguous memory locations.

• Can be used to store the collection of primitive data


types such as int, char, float, etc., as well as derived
and user-defined data types such as pointers,
structures, etc.
• Syntax
Array • data_type array_name[array_size];
• Example:
Declaration
• Initialization with Declaration
Array • Syntax:
Initialization • data_type array_name [size] = {value1, value2, ... valueN};
• The compiler can automatically deduce the size of
the array
Array
• The size of the array is equal to the number of
Initialization elements present in the initializer list .
with
Declaration • data_type array_name[] = {1,2,3,4,5};
without Size
• The size of the above arrays is 5 which is
automatically deduced by the compiler.
Array Initialization after Declaration (Using Loops)

• We initialize the array after the declaration by assigning the initial


value to each element individually.
• We can use for loop, while loop, or do-while loop to assign the
value to each element of the array.
• Example:
for (int i = 0; i < N; i++) {
array_name[i] = valuei;
}
Example on Array Initialization
#include <stdio.h>
int main()
{
int arr[5] = { 10, 20, 30, 40, 50 }; // array initialization using initialier list
int arr1[] = { 1, 2, 3, 4, 5 }; // array initialization using initializer list without specifying size
float arr2[5]; // array initialization using for loop
for (int i = 0; i < 5; i++) {
arr2[i] = (float)i * 2.1;}
return 0;
}
Array Elements in Memory
16 bytes get immediately reserved in memory, 2 bytes each for the 8 integers .

The array is not being initialized; all eight values present in it would be garbage values.

Whatever be the initial values, all the array elements would always be present in
contiguous memory locations.

int arr[8] ;
Bound checking in Array
• No check to see if the subscript used for an array
exceeds the size of the array. main( )
{
• Exceeded data will simply be placed in memory int num[40], i ;
outside the array; probably on top of other data, or for ( i = 0 ; i <= 100 ; i++ )
on the program itself.
num[i] = i ;
}
• This will lead to unpredictable results

• No error message to warn


Access Array Elements

• We can access any element using the array subscript operator [ ] and the
index value i of the element.
• array_name [index];
• Indexing in the array always starts with 0 and the last element is at N – 1
where N is the number of elements in the array.

int arr[5] = { 15, 25, 35, 45, 55 }; // array declaration and initialization

printf("Element at arr[2]: %d\n", arr[2]); // accessing element at index 2 i.e 3rd element

You might also like