Skip to main content

Posts

Showing posts with the label Basic

Get your hands dirty

The only way to learn a new programming language is by writing programs in it. The first program to write is the same for all languages: Print the words hello, world! In C, the program to print `` Hello, world! '' is #include <stdio.h> main() { printf("Hello, world!\n"); } Just how to run this program depends on the system you are using.

Basics of C

What Is C? C is a highly flexible and adaptable language. Since its creation in 1970, it's been used for a wide variety of programs including firmware for micro-controllers, operating systems, applications, and graphics programming. When was C developed? C was developed at Bell Laboratories in 1972 by Dennis Ritchie along with Ken Thompson. Many of its principles and ideas were taken from the earlier language B and B's earlier ancestors BCPL and CPL. Who is the inventor of C? Dennis Ritchie invented C, the computer-programming language that underlies Microsoft Windows, the Unix operating system and much of the other software running on computers around the world. Mr. Ritchie was a longtime research scientist at Bell Labs, originally AT&T's research division.

Accept Array elements

#include<stdio.h> void main() { int arr[10], i, j, k, size; printf("\nEnter array size : "); scanf("%d", &size); printf("\nAccept Numbers : \n"); for (i = 0; i < size; i++) { printf("Data [%d]: ",i+1); scanf("%d", &arr[i]); } printf("\nThe Array is : "); for (i = 0; i < size; i++) printf("%d ", arr[i]); getch(); } Output   Print   Download Code Output Enter array size : 5 Accept Numbers : Data [1]: 2 Data [2]: 6 Data [3]: 7 Data [4]: 1 Data [5]: 2 The Array is : 2 6 7 1 2

Print Pattern 5 

#include <stdio.h> void main() { int i, j, k; for(i=5;i>=1;i--) { for(k=5;k>=i;k--) { printf(" "); } for(j=1;j<i;j++) { printf("*"); } printf("\n"); } getch(); } Output   Print   Download Code Output **** *** ** *

Print Pattern 4 

#include <stdio.h> void main() { int i, j, k; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) printf("*"); printf("\n"); } getch(); } Output   Print   Download Code Output ***** **** *** ** *

Print Pattern 3 

#include <stdio.h> void main() { int i, j, k; for(i=5;i>=1;i--) { for(j=1;j<i;j++) { printf(" "); } for(k=5;k>=i;k--) { printf("*"); } printf("\n"); } getch(); } Output   Print   Download Code Output * ** *** **** *****

Print Pattern 2 

#include <stdio.h> void main() { int i,j; for(i=1;i<=5;i++) { for(j=1;j<=i;j++) { printf("*"); } printf("\n"); } getch(); } Output   Print   Download Code Output * ** *** **** *****

Print Pattern 1 

#include<stdio.h> int main() { int num,r,c; printf("Enter number of rows/columns: "); scanf("%d",&num); for(r=1; r<=num; r++) { for(c=1; c<=num; c++) printf("* "); printf("\n"); } getch(); return 0; } Output   Print   Download Code Output Enter number of rows/columns: 5 * * * * * * * * * * * * * * * * * * * * * * * * *

List all the Prime Numbers

#include<stdio.h> void main() { int n,i,j,flag; printf("\n\nEnter the limit\t:"); scanf("%d",&n); printf("\nThe prime numbers within the given limit:\n"); for(i=1;i<=n;i++) { flag=0; for(j=2;j<=i/2;j++) { if(i%j==0) { flag=1; break; } } if(flag==0) printf("%d\t",i); } } Output   Print   Download Code Output Enter the limit :60 The prime numbers within the given limit: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59

Generate the Fibonacci Series

#include<stdio.h> void main() { int t,i,m=0,n=1,f=1; printf("\n\nEnter the number of terms:"); scanf("%d",&t); printf("\n The Fibonacci Series of %d terms:\n",t); for(i=1;i<=t;i++) { printf("%d\t",f); f=m+n; m=n; n=f; } } Output   Print   Download Code Output Enter the number of terms:10 The Fibonacci Series of 10 terms: 1 1 2 3 5 8 13 21 34 55

Check whether an alphabet is Vowel or not

#include <stdio.h> void main() { char in; printf("Enter Character: "); scanf("%c", &in); if(in>=65&&in<=90 || in>=97&& in<=122) { if(in<97) in=in+32; if(in=='a'||in=='e'||in=='i'||in=='o'||in=='u') printf("vowel"); else printf("consonent"); } else printf("Not an valid Alphabet"); } Output   Print   Download Code Output Enter Character: A vowel

Print the ASCII values of all the English Alphabets

#include<stdio.h> void main() { char chl='a'; char chu='A'; int i; printf("\n==========================="); for(i=1;i<=26;i++,chu++,chl++) printf("\n|%c | %d \t|| %c| %d |",chl,chl, chu, chu); printf("\n==========================="); } Output   Print   Download Code Output =========================== |a | 97 || A| 65 | |b | 98 || B| 66 | |c | 99 || C| 67 | |d | 100 || D| 68 | |e | 101 || E| 69 | |f | 102 || F| 70 | |g | 103 || G| 71 | |h | 104 || H| 72 | |i | 105 || I| 73 | |j | 106 || J| 74 | |k | 107 || K| 75 | |l | 108 || L| 76 | |m | 109 || M| 77 | |n | 110 || N| 78 | |o | 111 || O| 79 | |p | 112 || P| 80 | |q | 113 || Q| 81 | |r | 114 || R| 82 | |s | 115 || S| 83 | |t | 116 || T| 84 | |u | 117 || U| 85 | |v | 118 || V| 86 | |w | 119

Find Area and Circumference of a Circle

#include<stdio.h> #define PI 3.142 void main() { float r,a,c; printf("\nEnter the radius of circle:\t"); scanf("%f",&r); a=PI*r*r; printf("\nThe area of the circle:\t%f",a); c=2*PI*r; printf("\nThe circumference of the circle: %f",c); } Output   Print   Download Code Output Enter the radius of circle: 2 The area of the circle: 12.568000 The circumference of the circle: 12.568000 Enter the radius of circle: 1 The area of the circle: 3.142000 The circumference of the circle: 6.284000

Print the Truth Table

#include<stdio.h> int OR(int,int); int AND(int,int); int XOR(int,int); int NOT(int); void main() { int a,b,ch,o; printf("\n-:MENU:-\n1.AND\n2.OR\n3.NOT\n4.XOR\n0.Exit\n"); do{ printf("\nEnter Choice:"); scanf("%d",&ch); switch(ch) { case 1: printf ("\nTruth Table for AND\nA B \tO"); for(a=0;a<=1;a++) { for(b=0;b<=1;b++) { o=AND(a,b); printf("\n%d %d \t%d",a,b,o); } } break; case 2: printf ("\nTruth Table for OR\nA B \tO"); for(a=0;a<=1;a++) { for(b=0;b<=1;b++) { o=OR(a,b); printf("\n%d %d \t%d",a,b,o); } } break; case 3: printf ("\nTruth Table for NOT\

Simple Interest

#include<stdio.h> void main() { float p; float ir,t; float in; printf("Principle amount : "); scanf("%f",&p); printf("\nInterest : "); scanf("%f",&ir); printf("\nTime (in year) : "); scanf("%f",&t); in=p*ir*t/100; printf("\nYou will get interest : Rs. %.2f",in); } Output   Print   Download Code Output Principle amount : 5000 Interest : 10 Time (in year) : 3 You will get interest :Rs. 1500.00