Lecture_Week_6.1
Lecture_Week_6.1
and Programming
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
return 0;
}
Write a C program to display a pyramid
#include <stdio.h>
int main() {
int rows = 5; // Number of rows for the pyramid
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
• 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
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
• 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