C Programming Codes
13.4K subscribers
139 photos
2 videos
12 links
C Programming Codes || Quizzes || DSA

Learn along with the community

Any queries
admin - @Pradeep_saii
Download Telegram
//program to print leap years from m to n'th year
#include<stdio.h>
int main()
{
int m,n;
printf("Enter the year m:");
scanf("%d",&m);
printf("Enter the year n:");
scanf("%d",&n);
do
{
if((m%4==0 && m%100!=0) || (m%400==0))
{
printf("%d is a leap year\n",m);
}
else{
printf("%d is not a leap year\n",m);
}
m+=1;
}while(m<=n);
return 0;
}
👍132
#include <stdio.h>
int main() {
int x = 5;
int y = x++;
printf("%d %d", x, y);
return 0;
}
👍24🔥21
Output of above program is?
Anonymous Quiz
25%
5,5
58%
5,6
11%
6,5
5%
6,6
🤔41🤯28👍217💯7😁4🔥2🥰2
What is an Array ?
An array is a collection of elements of the same data type, and all these elements are stored in contiguous memory locations, which means that they are placed next to each other in the memory. This arrangement makes it easy to access and manipulate individual elements within the array.
👍4612🥰1
What is an Index?
To access an element in an array, we use its index. The index is a number that identifies the position of the element in the array. The first element in the array has index 0, the second element has index 1, and so on.
👍324🥰1👏1
What is address of an array ?
The address of an array is the address of its first element. This is because an array is simply a contiguous block of memory, with each element stored sequentially. The address of the first element is the starting address of that block of memory.
• It is important to note that the address of an array is different from the address of any individual element in the array.
• Usually address is in Hexadecimal form ,for understanding purpose we will take decimal numbers as addresses.
👍294🤝1
>Classification of Arrays:
Arrays are classified into two types,

• Single-dimensional array:
A single-dimensional array is a linear list of elements of the same data type. Each element in the array is identified by its unique index, which is an integer value. The index of the first element in the array is always 0, and the index of the last element in the array is always one less than the total number of elements in the array.
• Multi-dimensional array:
A multi-dimensional array is a collection of data in multiple dimensions. A two-dimensional array, for example, can be visualized as a table with rows and columns. Each element in the array is identified by a unique set of indices, one index for each dimension.
👍3112🔥2🤔2😁1
Declaration of Single dimensional array
👍151
Initialization of single dimensional array
👍5
//1. Program to initialize an array using compile time initialization and print the array elements.
#include<stdio.h>
int main()
{
int a[5]={1,2,5,9,12},i;
printf("Elements of array are,\n");
for(i=0 ; i<sizeof(a)/sizeof(a[0]) ;i++ )
{
printf("Array element at index %d is %d\n",i,a[i]);
}
return 0;
}
👍14🤯2
//2. Program to initialize an array using run time initialization and print the array elements.
#include<stdio.h>
int main()
{
int a[20],i,n;
printf("Enter number of elements:");
scanf("%d",&n);
printf("Enter the elements into the array:\n");
for(i=0 ; i<n ; i++)
{
scanf("%d",&a[i]);
}
printf("Elements of array are,\n");
for(i=0 ; i<n ;i++ )
{
printf("Array element at index %d is %d\n",i,a[i]);
}
return 0;
}
👏82👍2🔥1
//3. Program to find sum and average of n array elements
#include<stdio.h>
int main()
{
int a[20],sum=0,n,i;
float avg;
printf("Enter number of elements in the array:");
scanf("%d",&n);
printf("Enter the elements into the array:");
for(i=0 ; i<n ;i++) // loop for reading elements into array
{
scanf("%d",&a[i]);
}
for(i=0 ; i<n ;i++) //loop for calculating sum of array elements
{
sum = sum + a[i];
}
avg=(float)sum/n;
printf("Sum of array elements:%d\n",sum);
printf("Average of array elements:%f\n",avg);
return 0;
}
👍10🔥21🤯1
// 4.Program to find product of n elements in the array
#include<stdio.h>
int main()
{
int a[20],n,i,product=1;
printf("Enter number of elements in the array:");
scanf("%d",&n);
printf("Enter %d elements into the array:\n",n);
for(i=0; i< n ;i++) //loop to read array elements
{
scanf("%d",&a[i]);
}
for(i=0 ; i< n ; i++) //loop to compute product of array elements
{
product=product*a[i];
}
printf("Product of the array elements : %d",product);
return 0;
}
👍11
// 5.Program to find the largest element in the array
#include<stdio.h>
int main()
{
int a[]={12,10,52,3,34},n,large,i;
n=sizeof(a)/sizeof(a[0]); // n will have the length of the array ,here its 5
large=a[0]; // first we will assume that first element is largest
for(i=1; i<n ;i++)
{
if(a[i]>large) // checking wheather other elements is larger than first element
{
large=a[i]; //if any element is large assigning that value to large
}
}
printf("The largest element in the array is %d",large);
return 0;
}
👍121
// 6.Program to find the smallest element in the array
#include<stdio.h>
int main()
{
int a[]={11,5,10,3,34},n,smallest,i;
n=sizeof(a)/sizeof(a[0]); // n will have the length of the array ,here its 5
smallest = a[0]; // first we will assume that first element is smallest
for(i=1; i<n ;i++)
{
if(a[i] < smallest) // checking wheather other elements is smaller than first element
{
smallest=a[i]; //if any element is smaller ,assigning that value to smallest
}
}
printf("The smallest element in the array is %d",smallest);
return 0;
}
5👍3🔥2
// program to print the array elements in reverse order .
#include<stdio.h>
int main()
{
int a[]={2,5,4,8,7},n,i;
n=sizeof(a)/sizeof(a[0]);
printf("The elements of the array are:\n");
for(i=0 ; i<n ; i++)
{
printf("%d\t",a[i]);
}
printf("\nThe array elements in reverse are:\n");
for(i=n-1 ; i>=0 ; i--)
{
printf("%d\t",a[i]);
}
return 0;
}
👍152
//8. Program to read n integers into array and count number of odd and even
#include<stdio.h>
int main()
{
int a[50],i,odd=0,even=0,n;
printf("Enter the value of n:");
scanf("%d",&n);
printf("Enter the elements into the array:\n");
for(i=0 ; i< n ;i++ )
{
scanf("%d",&a[i]);
}
for(i=0 ; i<n ;i++)
{
if(a[i]%2 == 0)
even++; //even = even +1
else
odd++;
}
printf("Number of even numbers entered:%d\n",even);
printf("Number of odd numbers entered :%d",odd);
return 0;
}
👍92🥰2🔥1
//9. Write a C Program to generate Fibonacci series using arrays
#include<stdio.h>
int main()
{
int fib[20],i,n;
printf("Enter number of terms in the fibonacci series:");
scanf("%d",&n);
if(n<=0)
{
printf("Invalid input entered ,Please enter a positive number");
return 1;
}
fib[0]=0;
fib[1]=1;
for(i=2 ; i<n ;i++) //loop to generate fibonacci series
{
fib[i]=fib[i-1]+fib[i-2];
}
printf("Fibonacci series upto %d terms :\n",n);
for(i=0 ; i<n ; i++)
{
printf("%d ",fib[i]);
}
return 0;
}
👍75
//10. Program to add two single dimensional arrays which have 5 elements in each.
#include<stdio.h>
int main()
{
int a[5]={1,5,7,3,9},b[5]={2,6,4,8,10},c[5],i;
for(i=0;i<5;i++)
{
c[i]=a[i]+b[i];
}
printf("Resultant array after adding the two array's:\n");
for(i=0 ;i<5 ;i++)
{
printf("%d\t",c[i]);
}
return 0;
}
👍16🔥2
// 11.Program to find the second largest element of an array
#include<stdio.h>
int main()
{
int a[20],i,large1,large2,size;
printf("Enter the size of the array:");
scanf("%d",&size);
printf("Enter the elements of the array:\n");
for(i=0 ; i< size ; i++)
{
scanf("%d",&a[i]);
}
if(a[0]>a[1]){
large1=a[0];
large2=a[1];
}
else{
large1=a[1];
large2=a[2];
}
for(i=2 ; i<size ;i++)
{
if(a[i]>large1){
large2=large1;
large1=a[i];
}
else if(a[i] > large2 && a[i]<large1){
large2=a[i];
}
}
printf("The second largest element of the array= %d",large2);
return 0;
}
👍115