Skip to main content

Posts

Showing posts from September, 2018
Write a program to calculate to sum of an array of 5 elements. Before going through program, you should know what is array. Array is a collection of homogenous elements, consist a common property and share a common name. Syntax; datatype variable name [size]; PROGRAM: - #include<stdio.h> void main (); { int a[5], i; Int sum=0 ; clrscr ( ); for( i=0; i<5; i++ ) { Printf (“\n enter the number =”); Scanf(“%d”, & a[i] ); } for( i=0; i<5; i++); { Sum = sum + a[i] ; } Printf(“\n sum= %d”, sum); getch(); } DRY RUN: -
Write a program to print the factorial of a number. As we know that factorial of a number means the sum of number less then given number but always greater and equal to 1. Mathematically let’s take a number b then. Let’s go through program #include<stdio.h> void main () { Int a, fact. =1; Clrscr(); Printf (“\n enter a number you want to calculate=”); Scanf (“%d”, & a); while (a! =0) { fact. = fact. * a; a--; } Printf (“\n fact. Of number = %d”, fact.); getch(); } DRY RUN: - Enter a number you want to calculate = 3 (say) a! =3 so, fact. = 1* 3; a= 3-1 =2; a! =2 so, fact. = 3*2; a= 2-1=1; a! =1 so, fact. = 6*1; a= 1-1=0; a! =0 which is not possible so operation will stop here and output will be 6.
Write a program to print the first ‘n’ terms of Fibonacci series. Today we are writing a program about Fibonacci series. Fibonacci simply is a series of number in which third number is the sum of preceding numbers.     Now let’s start the program. #include<stdio.h> void main (); { Int a=0, b=1, sum, n, counter=0; clrscr (); printf (“\n how many terms you want to calculate= “); scanf (“%d”, &n); printf (“%d \t”, a); printf (“%d \t”, b); while (counter < (n-2) ) { sum = a+b ; printf ( “ %d \t”, sum); a=b; b=sum; counter = counter +1 ; } getch(); } OUTPUT: -
Write a program to input two number from the user and then swap the value of these two variables by using third variable. Here swap simply means interchanging the given data with each other by using third variable. In the given program I’m going to input data in two variables and with the help of third variable I’m going to swap the data between two. Let’s go through program. #include<stdio.h> void main(); { int a, b, c;   clrscr ();   printf (“\n enter the first number”);    scanf (“%d”, &a);     printf (“\n enter the second number”);      scanf (“%d”, &b);        c=b ;          b=a ;           a=c ;            printf (“\n a=%d”, a );              printf (“...
Write a program in C to input three numbers from the user and print the maximum number using if else statement. Before going through the program, we should know about if else statement. If else statement is condition control statement which comes under control statement. It helps in the flow of execution of program. Let’s us look through the syntax of if else statement. If (condition) { Statement 1; Else if (condition) Statement 2; Else Statement 3; } Program: - #include<stdio.h> Void main() { Int a, b, c; Clrscr (); Printf(“\n enter the first number=”); Scanf(“%d”, & a); Printf (“\n enter the second number=”); Scanf (“%d”,&b); Printf(“\n enter the third number=”); Scanf( “%d”,& c); If( (a>b) && (b>c) ) Printf ( “%d is a greatest number “, a); Else if ( (b>c) && (b>a) ) Print ( “ %d is a greatest number “, b); Else Printf ( “%d is a greatest number “,c); getch(); } ...

Introduction to C Programming

What is c programming? Before knowing about c programming, one should know about the c language. C was developed in 1960’s (somewhere we can find 1972) by Dinnis Ritchie . It was growth of two earlier language i.e. combined programming Language and basic combined programming    Language. C is a middle level language because in c we can do programming up to high level as well as up to low level. FORMAT OF C PROGRAM #include<header file name> Main() { All variable declaration; All I/O elements; } 1)        What is #include? - It is a pre-processor directive. 2)        What is Header file?             - It contains description about library functions. There are many number of library functions in a single c program. For eg; pow(), <math.h> etc 3)         what is main() ? ...