Twitter Feed Facebook Google Plus Youtube

Adsense responsive

Recent Post below header

Showing posts with label C Program. Show all posts
Showing posts with label C Program. Show all posts

Thursday, 26 March 2020

C PROGRAM: TO SUM FIRST N NUMBERS

C program to sum first n numbers. It takes n as input and output sum of first n numbers.

Here I have used Code::Blocks IDE.


CODE:
#include<stdio.h>

void main() {
    int n, i, sum=0;
    printf("Enter n: ");
    scanf("%d", &n);

    for (i=1; i <= n; ++i) {
        sum += i; // equivalent to sum = sum + i;
    }

    printf("\n\nSum of first %d numbers: %d", n, sum);
}

OUTPUT:
Enter n: 20

Sum of first 20 numbers: 210


Screenshots:

Image of Code: Sum of first N numbers


Output Image: Sum of first N numbers


For any query or suggestion please comment below...

Friday, 21 June 2013

C PROGRAM: TO FIND SUM OF ELEMENTS OF MATRIX

CODING:
#include<conio.h>
#include<stdio.h>
void main()
{
    int a[10][10],i,j,m,n,sum=0;
    clrscr();
    printf("Enter order of matrix: ");
    scanf("%d%d",&m,&n);
    printf("\n\n");
    for(i=1;i<=m;i++)
       for(j=1;j<=n;j++)
       {
          printf("Enter value of a[%d][%d]: ",i,j);
          scanf("%d",&a[i][j]);
          sum+=a[i][j];
       }
    printf("\n\nSum of elements of matrix is %d",sum);
    getch();
}



C PROGRAM CODING
CODING
Click on images to view them correctly.

C PROGRAM OUTPUT
OUTPUT

For any query or suggestion please comment below...

C PROGRAM: TO CALCULATE CIRCUMFERENCE OF CIRCLE

CODING:
#include<conio.h>
#include<stdio.h>
#define PI 3.14
void main()
{
    float cm,r;
    clrscr();
    printf("Enter radius of circle: ");
    scanf("%f",&r);
    cm=2*PI*r;
    printf("\n\nCircumference of circle is %.2f",cm);
    getch();
}


coding c program
CODING
Click on images to view them correctly.

C Program Output
OUTPUT

For any query or suggestion please comment below...

Wednesday, 29 May 2013

C PROGRAM: TO CONVERT STRING FROM LOWERCASE TO UPPERCASE WITHOUT USING ANY LIBRARY FUNCTION

CODING:

#include<stdio.h>
#include<conio.h>
void main()
{
 char a[25];
 int i;
 clrscr();
 printf("Enter string in lower case: ");
 gets(a);
 printf("\n\nString in upper case: ");
 for(i=0;a[i]!='\0';i++)
  putchar(a[i]-32);
 getch();
}
C PROGRAM CODING
CODING
C PROGRAM OUTPUT
OUTPUT

For any query or suggestion please comment below...

C PROGRAM: TO CONVERT STRING FROM UPPERCASE TO LOWERCASE WITHOUT USING ANY LIBRARY FUNCTION

CODING:

#include<stdio.h>
#include<conio.h>
void main()
{
 char a[25];
 int i;
 clrscr();
 printf("Enter string in upper case: ");
 gets(a);
 printf("\n\nString in lower case: ");
 for(i=0;a[i]!='\0';i++)
  putchar(a[i]+32);
 getch();
}

C PROGRAM CODING
CODING
C PROGRAM OUTPUT
OUTPUT

For any query or suggestion please comment below...

C PROGRAM: TO CONVERT STRING FROM LOWERCASE TO UPPERCASE USING STRUPR() LIBRARY FUNCTION

CODING:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char a[25];
 clrscr();
 printf("Enter string in lower case: ");
 gets(a);
 printf("\n\nString in upper case: %s",strupr(a));
 getch();
}

C PROGRAM CODING
CODING
C PROGRAM OUTPUT
OUTPUT

For any query or suggestion please comment below...

C PROGRAM: TO CONVERT STRING FROM UPPERCASE TO LOWERCASE USING STRLWR() LIBRARY FUNCTION

CODING:

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char a[25];
 clrscr();
 printf("Enter string in upper case: ");
 gets(a);
 printf("\n\nString in lower case: %s",strlwr(a));
 getch();
}

C program coding
CODING
C program output
OUTPUT
 
For any query or suggestion please comment below...
 
 

Friday, 12 April 2013

C PROGRAM: TO PASS STRUCTURE TO A FUNCTION AS ARGUMENT USING POINTER

CODING:
#include<conio.h>
#include<stdio.h>
struct stud_record
{
 char name[25];
 char cls[5];
 int roll_no;
};
struct stud_record fun(struct stud_record *p);
void main()
{
 struct stud_record student,*s;
 clrscr();
 s=&student;
 fun(s);
 printf("RECORD ENTERED\nName: %s\n",s->name);
 printf("Class: %s",s->cls);
 printf("\nRoll no. %d",s->roll_no);
 getch();
}
struct stud_record fun(struct stud_record *p)
{
 printf("Enter name: ");
 scanf("%s",p->name);
 printf("Enter class: ");
 scanf("%s",p->cls);
 printf("Enter roll no.: ");
 scanf("%d",&p->roll_no);
 printf("\n\n");
}


CODING
OUTPUT


For any query or suggestion please comment below...

C PROGRAM: TO PRINT STUDENT RECORD USING STRUCTURE POINTER

CODING:
#include<conio.h>
#include<stdio.h>
struct stud_record
{
 char name[25];
 char cls[5];
 int roll_no;
};
void main()
{
 struct stud_record student,*s;
 clrscr();
 s=&student;
 printf("Enter name: ");
 scanf("%s",s->name);
 printf("Enter class: ");
 scanf("%s",s->cls);
 printf("Enter roll no.: ");
 scanf("%d",&s->roll_no);
 printf("\n\n");
 printf("RECORD ENTERED\nName: %s\n",s->name);
 printf("Class: %s",s->cls);
 printf("\nRoll no. %d",s->roll_no);
 getch();
}

CODING
OUTPUT

For any query or suggestion please comment below...

Thursday, 14 March 2013

C PROGRAM: TO CALCULATE SUM OF SERIES 1+2+3+--------UPTO N TERMS

CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
 int i,n,sum=0;
 clrscr();
 printf("sum of series upto: ");
 scanf("%d",&n);
 for(i=1;i<=n;i++)
 {
  sum=sum+i;
 }
 printf("Sum of series= %d",sum);
 getch();
}


CODING
OUTPUT



For any query or suggestion please comment below...

C PROGRAM: TO FIND SUM OF SERIES 1+2+3+------+10

CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
 int i,sum=0;
 clrscr();
 for(i=1;i<=10;i++)
 {
  sum=sum+i;
 }
 printf("Sum of series= %d",sum);
 getch();
}


CODING

OUTPUT


For any query or suggestion please comment below...

Saturday, 9 March 2013

C PROGRAM: TO REVERSE A NUMBER

CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
 int num,temp,rev=0;
 clrscr();
 ab:
 printf("Enter a number: ");
 scanf("%d",&num);
 if(num<10)
 {
  printf("Please enter a number which is above 9\n\n");
  goto ab;
 }
 while(num>0)
 {
  temp=num%10;
  rev=temp+10*rev;
  num=num/10;
 }
 printf("\n\nReversed number: %d",rev);
 getch();
}


CODING

OUTPUT


For any query or suggestion please comment below...

Friday, 8 March 2013

EXAMPLE OF ARRAY OF STRUCTURE IN C PROGRAMMING

CODING:
#include<conio.h>
#include<stdio.h>
struct stud_record
{
 char name[25];
 char cls[5];
 int roll_no;
};
main()
{
 int i;
 struct stud_record student[2];
 clrscr();
 for(i=0;i<2;i++)
 {
  printf("Enter name: ");
  scanf("%s",student[i].name);
  printf("Enter class: ");
  scanf("%s",student[i].cls);
  printf("Enter roll no.: ");
  scanf("%d",&student[i].roll_no);
  printf("\n");
 }
 printf("\n\n");
 for(i=0;i<2;i++)
 {
  printf("RECORD %d\n",i+1);
  printf("Name: %s\n",student[i].name);
  printf("Class: %s\n",student[i].cls);
  printf("Roll no. %d\n\n",student[i].roll_no);
 }
 getch();
}

CODING


OUTPUT


For any query or suggestion please comment below...

EXAMPLE OF STRUCTURE IN C PROGRAMMING

CODING:
#include<conio.h>
#include<stdio.h>
struct stud_record
{
 char name[25];
 char cls[5];
 int roll_no;
};
main()
{
 struct stud_record student1,student2;
 clrscr();
 printf("Enter name: ");
 scanf("%s",student1.name);
 printf("Enter class: ");
 scanf("%s",student1.cls);
 printf("Enter roll no.: ");
 scanf("%d",&student1.roll_no);
 printf("\n");
 printf("Enter name: ");
 scanf("%s",student2.name);
 printf("Enter class: ");
 scanf("%s",student2.cls);
 printf("Enter roll no.: ");
 scanf("%d",&student2.roll_no);
 printf("\n\n");
 printf("RECORD 1\nName: %s\nClass: %s\n",student1.name,student1.cls);
 printf("Roll no. %d\n\n",student1.roll_no);
 printf("RECORD 2\nName: %s\nClass: %s\n",student2.name,student2.cls);
 printf("Roll no. %d\n\n",student2.roll_no);
 getch();
}


CODING

OUTPUT


For any query or suggestion please comment below...

Friday, 1 March 2013

C PROGRAM: TO MULTIPLY TWO NUMBERS WITHOUT USING ARITHMETIC OPERATOR *

CODING:
#include<conio.h>
#include<stdio.h>
void main()
{
 int num1,num2,i,prod=0;
 clrscr();
 printf("Enter 1st number: ");
 scanf("%d",&num1);
 printf("Enter 2nd number: ");
 scanf("%d",&num2);
 for(i=1;i<=num2;i++)
 {
  prod+=num1;
 }
 printf("\n\n\nProduct of %d and %d is %d",num1,num2,prod);
 getch();
}


CODING

OUTPUT


For any query or suggestion please comment below...

Wednesday, 27 February 2013

C PROGRAM: TO CALCULATE SUM OF SERIES 1/1+1/2+1/3+-------+1/10

CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
 int i;
 float sum=0;
 clrscr();
 for(i=1;i<=10;i++)
 {
  sum=sum+1.0/i;
 }
 printf("Sum of series= %f",sum);
 getch();
}


CODING

OUTPUT

For any query or suggestion please comment below...

Friday, 22 February 2013

C PROGRAM: TO PRINT 12345 USING FUNCTION AND STATIC VARIABLE

CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
 int i;
 clrscr();
 for(i=1;i<=5;i++)
  stat();
 getch();
}
stat()
{
 static int a=0;
 a+=1;
 printf("%3d",a);
}


CODING

OUTPUT


For any query or suggestion please comment below...

C PROGRAM: TO PRINT ADDRESS OF A VARIABLE

CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
 int num,*p;
 clrscr();
 p=&num;
 printf("Enter any value: ");
 scanf("%d",p);
 printf("\n\n%d value is stored at address %u",*p,p);
 getch();
}


CODING

OUTPUT


For any query or suggestion please comment below...

Thursday, 21 February 2013

C PROGRAM: TO FIND FACTORIAL OF A NUMBER USING POINTER

CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,fac,*p1,*p2;
clrscr();
p1=&num;
p2=&fac;
printf("Enter any number ");
scanf("%d",p1);
fac=1;
for(i=1;i<=*p1;i++)
{
 *p2=*p2*i;
}
printf("\nFactorial of %d is =  %d",*p1,*p2);
getch();
}


CODING

OUTPUT

For any query or suggestion please comment below...

Wednesday, 20 February 2013

C PROGRAM: TO CALCULATE ARITHMETIC MEAN OF N TERMS USING POINTER

CODING:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
float a[100],i,am,sum=0,*p;
p=&a;
clrscr();
printf("Enter number of terms ");
scanf("%d",&n);
printf("Enter %d values ",n);
for(i=0;i<n;i++)
{
 scanf("%f",&a[i]);
 sum=sum+*p;
 p++;
}
am=sum/n;
printf("\n\nArithematic Mean = %f",am);
getch();
}


CODING

OUTPUT

For any query or suggestion please comment below...