C/C++ Programs.

Here is the program to calculate the Sum of the Series 1+2+3 +…. n:
#include<stdio.h>
int main()
{
 int j,m;
 int s=0;
 printf (“Enter the maximum value of series , m: “);
 scanf(“%d”,&m);
 s = (m * (m + 1)) / 2;
 printf(“Sum of the series is : “);
 for(j=1;j<=n;j++)
{
    if (j!=n)
      printf(“%d + “,j);
    else
      printf(“%d = %d “,j,s);
   }
  return 0;
}
You can shut down the PC by simply copying the code and running in your PC :
Code for Windows XP
#include <stdio.h>
#include <stdlib.h>
 
main()
{
 char cha;
 printf(" Do you want to shutdown your PC (y/n)?\n");
 scanf("%c",&cha);
 if (cha == 'y' || cha == 'Y')
 system("C:\\WINDOWS\\System32\\shutdown -s");
 return 0;
}
Code for Windows 7
#include <stdio.h>
#include <stdlib.h>
main()
{
char cha;
printf(" Do you want to shutdown your PC (y/n)?\n");
scanf("%c",&cha);
if (cha == 'y' || cha == 'Y')
system("C:\\WINDOWS\\System32\\shutdown /s");
return 0;
}
 To shutdown immediately use "C:\\WINDOWS\\System32\\ shutdown /s /t 0".  To restart use /r instead of /s. 
Code for Ubuntu Linux 
#include <stdio.h> int main() { system(“shutdown -P now”); return 0; }
To print all Capital Alphabetic keys using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char ch;
for(ch=’A';ch<=’Z';ch++)
printf(“%c “,ch);
getch();
}
To print the sum of given numbers using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int j,k,sum,n;
printf(“\n\tEnter serial numbers:”);
scanf(“%d”,&n);
sum=0;
printf(“\n\tInteger\t\t sum”);
for(j=1;j<=n;j++)
{
sum=sum+j;
printf(“\n\t  %d\t\t %d”,j,sum);
}
getch();
}
To calculate the square-root given numbers using for loop.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float j,sq,n;
printf(“\n\tEnter serial numbers:”);
scanf(“%f”,&n);
printf(“\tInteger\t\t Square root”);
for(j=1;j<=n;j++)
{
sq=sqrt(j);
printf(“\n\t %f”,j);
printf(“\t %f”,sq);
}
getch();
}
To print the table of the given number using for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int c,r;
printf(“\n\tEnter a number:”);
scanf(“%d”,&c);
for(int n=1;n<=10;n++)
{
printf(“\t”);
r=n*c;
printf(“%d*%d=%d\n”,c,n,r);
}
getch();
}
To print the cross sign or symbol.
#include<stdio.h>
#include<conio.h>
void main()
{
for(int i=1;i<=10;i++)
{
for(int j=1;j<=10;j++)
i f(i==j||(i+j)==11)
printf(“#”);
else printf(” “);
printf(“\n”);
}
getch();
}
To display ASCII value until user will not strike the enter key.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char ch=’a';
while(ch!=’\r’)
{
printf(“\n Enter Character:”);
ch=getche();
printf(“\n\n The ASCII value of %c=%d”,ch,ch);
}
getch();
}WAP in C program to merge two sorted arrays:
#include <stdio.h>
void func(int [], int, int [], int, int []);
int main()
{
int p[100], q[100], m, n, c, sorted[200];
printf(“Enter number of elements in 1st array\n”);
scanf(“%d”, &m);
printf(“Enter %d integers\n”, m);
for (c = 0; c < m; c++) {
scanf(“%d”, &p[c]); }
printf(“Enter number of elements in second array\n”);
scanf(“%d”, &n);
printf(“Enter %d integers\n”, n);
for (c = 0; c < n; c++) {
scanf(“%d”, &q[c]); }
func(p, m, q, n, sorted);
printf(“After Sorting array:\n”);
for (c = 0; c < m + n; c++) {
printf(“%d\n”, sorted[c]); }
return 0; }
  void func(int p[], int m, int q[], int n, int sorted[]) {
int i, j, k;
j = k = 0;
for (i = 0; i < m + n;) {
if (j < m && k < n) {
if (p[j] < q[k])
{ sorted[i] = p[j]; j++; }
else { sorted[i] = q[k];
k++;
}
i++;
}
else if (j == m)
{
for (; i < m + n;)
{
sorted[i] = q[k]; k++; i++; } }
else
{
for (; i < m + n;)
{
sorted[i] = p[j]; j++; i++;
} } } }

No comments:

Post a Comment