Categories
Academics Computers

C programming code to display asterisk in diagonal pattern

Course Details

Field of Study: Computer Science and Engineering
Course Name: C Programming
Course Description: C programming code to display asterisk in diagonal pattern where the length of the diagonal is given by n

Code
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,col,row;
printf(“Enter the number\n”);
scanf(“%d”,&n);
for(int i=1;i<=n;i++) {
for(int j=1;j<2n;j++)
{
if(j==i)
printf(“
“);
else
printf(” “);
}
printf(“\n”);
}
getch();
}

Output for n=7 is shown below

Categories
Academics Computers

C programming code to display asterisk in X pattern

Course Details

Field of Study: Computer Science and Engineering
Course Name: C Programming
Course Description: C programming code to display asterisk in X pattern where the length of X is given by n

Code
#include<conio.h>
#include<stdio.h>
void main()
{
int n,i,j;
clrscr();
printf(“Enter the value of n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“\n”);
for(j=1;j<=n;j++)
{
if((i==j)||(i+j==(n+1)))
printf(“*”);
else
printf(” “) ;
}
}
getch();
}

Output for n=7 is shown below

Categories
Academics Computers

C programming code to display asterisk in a right angle triangle

Course Details

Field of Study: Computer Science and Engineering
Course Name: C Programming
Course Description: C programming code to display asterisk in a right angle triangle below left diagonal

Code
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n;
printf(“Enter the number\n”);
scanf(“%d”,&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}

Output for n=6 is shown below

Categories
Academics Computers

C programming code to concatenate 2 sorted arrays in ascending order

Course Details

Field of Study: Computer Science and Engineering
Course Name: C Programming
Course Description: C programming code to concatenate 2 sorted arrays in ascending order

Code
#include<stdio.h>
#include<conio.h>

void concatenate(int [], int, int [], int, int []);

void main()
{
clrscr();
int a[100], b[100], m, n, c, sorted[200];

printf(“Enter the number of elements of the first array\n”);
scanf(“%d”, &m);

printf(“\nEnter %d sorted integers\n”, m);
for (c = 0; c < m; c++) {
scanf(“%d”, &a[c]);
}

printf(“\nEnter the number of elements of the second array\n”);
scanf(“%d”, &n);

printf(“\nEnter %d sorted integers\n”, n);
for (c = 0; c < n; c++) {
scanf(“%d”, &b[c]);
}

concatenate(a, m, b, n, sorted);

printf(“\nThe Concatenated array:\n”);
printf(“======================\n”);

for (c = 0; c < m + n; c++) {
printf(“%d\n”, sorted[c]);
}

getch();
}

void concatenate(int a[], int m, int b[], int n, int sorted[]) {
int i, j, k;

j = k = 0;

for (i = 0; i < m + n;) {
if (j < m && k < n) {
if (a[j] < b[k]) {
sorted[i] = a[j];
j++;
}
else {
sorted[i] = b[k];
k++;
}
i++;
}
else if (j == m) {
for (; i < m + n;) {
sorted[i] = b[k];
k++;
i++;
}
}
else {
for (; i < m + n;) {
sorted[i] = a[j];
j++;
i++;
}
}
}
}

Categories
Academics Computers

C programming code to find minimum value of an array

Course Details

Field of Study: Computer Science and Engineering
Course Name: C Programming
Course Description: C programming code to find minimum value of an array

Code
#include<stdio.h>
#include<conio.h>

void main()
{
clrscr();
int x[6]={70,29,3,8,30,76};
int min=x[0];
for(int i=0;i<6;i++)
{
if(x[i] < min)
min=x[i];
}
printf(“Minimum value is %d”,min);
getch();
}

Categories
Academics Computers

C programming code to add even and odd numbers up to n

Course Details

Field of Study: Computer Science and Engineering
Course Name: C Programming
Course Description: C programming code to add even and odd numbers up to n

Code
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,sO=0,sE=0;
printf(“Enter the number\n”);
scanf(“%d”,&n);
for(int i=1;i<=n;i++)
{
if(i%2==0)
sE=sE+i;
else
sO=sO+i;
}
printf(“The sum of even Nos up to %d=%d\nThe sun of odd Nos up to %d=%d”,n,sE,n,sO);
getch();
}

Exit mobile version