Contents
Variables, Constants and Keywords .................................................................................................................. 3
Basic Structure of C Program.............................................................................................................................. 3
Arithmetic Problems ......................................................................................................................................... 5
//-Adding two numbers 5 and 6 in C- ........................................................................................................... 5
//-Adding two numbers given by user in C- ................................................................................................ 5
Decision Making .................................................................................................................................................... 6
If statement: ....................................................................................................................................................... 6
If…else ................................................................................................................................................................... 7
If…elseif..else (Ladder if) ............................................................................................................................... 8
Nested if ............................................................................................................................................................ 10
switch statement ........................................................................................................................................... 11
Check even or odd ....................................................................................................................................................... 12
divisible by 5 or not .................................................................................................................................................... 12
positive or negative..................................................................................................................................................... 12
positive/negative/zero.............................................................................................................................................. 12
Check if a person is eligible to vote or not. ........................................................................................................ 12
Check greatest(greater) among 3(two) numbers .......................................................................................... 12
Loop Questions............................................................................................................................................... 13
//-Print "Hello boys" 5 times in C- ............................................................................................................. 13
//-Print "Hello boys" n times in C, prefixed by numbers-.................................................................... 13
//-Print natural numbers from 1 to 100- ................................................................................................... 13
//-Print natural numbers from 1 to 100,with sum-................................................................................. 14
//-Print natural numbers from 2,8,18,32.......upto 10th term .............................................................. 14
//-Print 1,2,3,4,6,7,8,9,11,12,13,14.. upto 100— ................................................................................... 14
//-Print 1,2,3,4,6,7,8,9,11,12,13,14.. upto 100— ................................................................................... 15
//-Print fibonaccii numbers from 1,1,2,3,5,8,13.. less than 100-- ..................................................... 15
//-Print factorial of given number .............................................................................................................. 15
//-Print sum of digits of given number....................................................................................................... 16
//-Print product of digits of given number ............................................................................................... 16
//-Print number of digits of given number ................................................................................................ 16
//-Print reverse of given number ................................................................................................................. 17
//-Check Palindrome ...................................................................................................................................... 17
//-Check Armstrong ........................................................................................................................................ 18
//Factorial of given number ......................................................................................................................... 18
//HCF or LCM of given number ................................................................................................................. 19
//Multiplication table of given number ..................................................................................................... 19
//-Check if given number is prime or not— ............................................................................................. 20
Array ....................................................................................................................................................................... 21
//Print sum of all elements of given array—............................................................................................ 21
//To arrange given array in ascending order.......................................................................................... 21
//sum of even elements and odd elements of given array— ............................................................... 22
//split even elements and odd elements of given array— ................................................................... 23
//sum of even indexed elements and odd elements of given array— ............................................... 24
//Add sum of two vectors ............................................................................................................................... 25
//-A program to input a matrix, and find sum of diagonal elements ................................................ 26
//-A program to input a matrix, transpose it and perform sum ........................................................ 27
//-Matrix Subtraction...................................................................................................................................... 28
//-Matrix multipilcation ................................................................................................................................. 29
//-Matrix multipilcation of 3x3 ................................................................................................................... 30
//-Input a string and count vowel,consonants, words, spaces and digits ........................................ 31
//-concatenate a string without using strcat ............................................................................................ 32
//checking if string is palindrome or not, without using strrev() ....................................................... 33
Functions ............................................................................................................................................................... 33
//-Print reverse of given number using function..................................................................................... 35
//-Generate prime numbers using function- ............................................................................................ 35
//-Swap values using pass by reference .................................................................................................... 36
Recursion ................................................................................................................................................................ 37
//-Factorial Using Recursion- ..................................................................................................................... 37
//-Sum of natural numbers upto nth term Using Recursion-............................................................... 37
//-Print nth Fibonaccii term Using Recursion- ..................................................................................... 38
//-HCF of given number Using Recursion-.............................................................................................. 38
Structure ................................................................................................................................................................. 39
//Read Records of 20 Students in structure & Sort Marks in Ascending Order ........................... 39
//Read Records of 20 Students in structure & Sort Marks in Ascending Order ........................... 40
//--Employee Record in Alphabetical Order by Name in Structure .................................................. 40
//-Employee Record in Descending Order by Age in Structure ......................................................... 42
// C program to draw few shapes ............................................................................................................... 43
Computer Programming
Computer Programming is a medium for us to communicate with computers, just like we
use Nepali or English to communicate with each other. Programming is the way for us to
deliver our instructions to the computer.
C is a programming language. It is one of the oldest and finest programming language. It
was developend by Dennis Ritchie in 1972.
C language is used to program a wide variety of systems. Some uses are: 1. Major parts
of windows and other os are written in C.
Characteristics of C
Portability
Faster and efficient
Supports structured programming
Extendibility
Flexible
Variables, Constants and Keywords
Basic Structure of C Program
Each c program starts with the following block sample:
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
//statements goes here
getch();
}
The typical C program may be virtually divided into following components
• declaration and initialization of variables. (Here variables used in the program are
mentioned and values are initialized if needed)
• Taking INPUT and storing them in variables (If required)
• Perform PROCESSING and storing result in variables.
• Displaying the OUTPUT in the way, the program has asked.
Fig: Steps of Execution
Arithmetic Problems
//-Adding two numbers 5 and 6 in C-
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int a,b,c; //declaration
a=5,b=6; //initialization
c=a+b; //processing
printf("Sum is %d",c); //displaying output
getch();
}
In a more generic approach, our program should be able to add any two numbers, not just
5 and 6, but any numbers provided by user on fly.
//-Adding two numbers given by user in C-
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int a,b,c; //declaration
printf("Enter first value");
scanf("%d",&a); //taking input for first number
printf("Enter second value");
scanf("%d",&b) //taking input for second number
c=a+b; //processing
printf("Sum is %d",c); //displaying output
getch();
}
• WAP to find the area and perimeter of a rectangle.
• WAP to calculate SI and Amount.
• WAP to find the area and perimeter of a circle
• WAP to convert Celsius into Fahrenheit.
• WAP to convert km into meters.
• WAP to convert seconds into hours, minutes and seconds.
Decision Making
When we have to do decisions in a program, we make the use of conditional
statements: For Eg:
– Check even or odd , divisible by 5 or not, positive or negative,
positive/negative/zero
– Check divisibility of a given number by any another number
– Check if a person is eligible to vote or not.
– Check greatest(greater) among 3(two) numbers
Conditional statements
If statement:
Syntax:
if(condition)
{
//statements
}
Example:
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int a;
printf("Enter age");
scanf("%d",&a);
if(a>=18)
{
printf("You are eligible to vote.")
}
printf("You are %d years old",a)
getch();
}
If…else
if-else statement in c
Structure:
Syntax:
if(condition)
{
//statements on true
}
else
{
//statements on false
}
Example:
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int a; //declaration
printf("Enter age");
scanf("%d",&a); //taking input for first number
if(a>=18)
{
printf("You are eligible to vote.")
}
else
{
printf("You are not eligible to vote");
}
printf("You are %d years old",a)
getch();
}
If…elseif..else (Ladder if)
Structure:
Syntax:
if(condition1)
{
//statements on regard of first condition to be true
}
else if (condition2)
{
//statements on regard of second condition to be true
}
else if(condition3)
{
//statements on regard of third condition to be true
}
...
...
else
{
//statements when all above conditions are false
}
Example:
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int a; //declaration
printf("Enter age");
scanf("%d",&a); //taking input for first
number
if(a<=18)
{
printf("You are too young to vote.")
}
else if(a>=18 && a<=120)
{
printf("You are not eligible to vote");
}
else if(a>120)
{
printf("Rare people exist beyond this age")
}
else
{
printf("You gave an invalid entry.")
}
printf("You are %d years old",a)
getch();
}
Nested if
It means another if statement inside one/more if statement.
Syntax:
if(condition1)
{
//statements on regard of condition1 to be true
if(condition2)
{
//statements on regard of condition1 and condition2 to be true
}
else
{
//statements on regard of condition1 to be true and condition2 to be false
}
}
else
{
//statements on regard of condition1 to be false
}
Example:
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int a; //declaration
printf("Enter your marks");
scanf("%d",&a);
if(a>=40)
{
printf("You passed");
if(a>=60){
printf("with score higher than 60");
}
else
{
printf("with score less than 60");
}
}
else
{
printf("You didnt pass");
}
printf("Your marks is %d",a);
getch();
}
switch statement
When you have same variable to be compared on all cases, we use switch
statement.
Syntax:
switch(a){
case cond_1:
//statements
break;
case cond_2:
//statements
break;
..
default:
//statements when all above are false
}
Example:
#include <stdio.h>
#include <conio.h>
void main(){
clrscr();
int a; //declaration
printf("Enter a day number");
scanf("%d",&a);
switch(a);
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Invalid entry");
}
}
Check even or odd
if(n%2==0){ }
else{}
divisible by 5 or not
if(n%5==0){}
else {}
positive or negative
if(n>=0){}
else{}
positive/negative/zero
if(n>0){ }
elseif(n<0) { }
else { }
Check if a person is eligible to vote or not.
if(n >= 18){}
else {}
Check greatest(greater) among 3(two) numbers
if(a>b && a>c){}
else if(b>c && b>c) {}
else {}
Loop Questions
When we have to perform some perform repeatedly, we use loop. Say, we need to print
“Hello boys” 5 times then, we write the following program.
//-Print "Hello boys" 5 times in C-
#include <stdio.h>
#include <conio.h>
void main(){
int i;
for(i=1;i<=5;i++){
printf("Hello boys\n")
}
getch();
}
//-Print "Hello boys" n times in C, prefixed by numbers-
#include <stdio.h>
#include <conio.h>
void main(){
int i,n;
printf("Upto which number you wish to print?")
scanf("%d",&n);
for(i=1;i<=n;i++){
printf("%d. Hello boys\n",n);
}
getch();
}
//-Print natural numbers from 1 to 100-
#include <stdio.h>
#include <conio.h>
void main(){
int i;
for(i=1;i<100;i++){
printf("%d\n",n);
}
getch();
}
//-Print natural numbers from 1 to 100,with sum-
#include <stdio.h>
#include <conio.h>
void main(){
int i,sum;
for(i=1;i<=100;i++){
sum=sum+i;
printf("%d\n",i);
}
printf("%d\n",sum);
getch();
}
//-Print natural numbers from 2,8,18,32.......upto 10th term
#include <stdio.h>
#include <conio.h>
void main(){
int i,sum;
for(i=1;i<=10;i++){
x=2*i*i;
printf("%d,",x);
}
getch();
}
//-Print 1,2,3,4,6,7,8,9,11,12,13,14.. upto 100—
#include <stdio.h>
#include <conio.h>
void main(){
int i,sum;
for(i=1;i<=10;i++){
if(i%5==0)
continue;
printf("%d,"i);
}
getch();
}
//-Print 1,2,3,4,6,7,8,9,11,12,13,14.. upto 100—
#include <stdio.h>
#include <conio.h>
void main(){
int i,sum;
for(i=1;i<=10;i++){
if(i%5==0)
continue;
printf("%d,"i);
}
getch();
}
//-Print fibonaccii numbers from 1,1,2,3,5,8,13.. less than 100--
#include <stdio.h>
#include <conio.h>
void main(){
int a=1;
int b=0;
intc;
do
{
c=a+b;
printf("%d,",c);
{
}while(a+b<=100);
getch();
}
//-Print factorial of given number
#include <stdio.h>
#include <conio.h>
void main(){
int i,f=1;
printf("Enter a number");
scanf("%d",&n);
for(i=1;i,<=n;i++){
f=f*i;
}
printf("factorial is %d,",f);
}
getch();
}
//-Print sum of digits of given number
#include <stdio.h>
#include <conio.h>
void main(){
int n,r,s=0;
printf("Enter a number");
scanf("%d",&n);
do{
r=n%10;
s=s+r;
n=n/10;
}
printf("Sum of digits is %d",s);
getch();
}
//-Print product of digits of given number
#include <stdio.h>
#include <conio.h>
void main(){
int n,r,p=1;
printf("Enter a number");
scanf("%d",&n);
do{
r=n%10;
p-p*r;
n=n/10;
}
printf("Product of digits is %d",p);
getch();
}
//-Print number of digits of given number
#include <stdio.h>
#include <conio.h>
void main(){
int n,r,s=0;
printf("Enter a number");
scanf("%d",&n);
do{
r=n%10;
s++;
n=n/10;
}
printf("Number of digits is %d",s);
getch();
}
//-Print reverse of given number
#include <stdio.h>
#include <conio.h>
void main(){
int n,r,s=0;
printf("Enter a number");
scanf("%d",&n);
do{
r=n%10;
s=s*10+r;
n=n/10;
}
printf("Reverse of given number is %d",s);
getch();
}
//-Check Palindrome
#include <stdio.h>
#include <conio.h>
void main(){
int n,r,s=0,x;
printf("Enter a number");
scanf("%d",&n);
x=n;
do{
r=n%10;
s=s*10+r;
n=n/10;
}
if(x==s)
{
printf("%d is palindrome",n);
}
else
{
printf("%d is not palindrome",n);
}
getch();
}
//-Check Armstrong
#include <stdio.h>
#include <conio.h>
void main(){
int n,r,s=0,x;
printf("Enter a number");
scanf("%d",&n);
x=n;
do{
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(x==s)
{
printf("%d is armstrong",n);
}
else
{
printf("%d is not armstrong",n);
}
getch();
}
//Factorial of given number
#include <stdio.h>
#include <conio.h>
void main(){
int n,f=1;
printf("Enter a number");
scanf("%d",&n);
for(i=1;i<=n;i++){
f=f*i;
}
printf("factorial is %d",f);
getch();
}
//HCF or LCM of given number
#include <stdio.h>
#include <conio.h>
void main(){
int a,b,p,q,c;
printf("Enter two number");
scanf("%d%d",&a,&b);
p=a,q=b;
do{
c=a%b;
a=b;
b=c;
}while(c != 0);
printf("HCF is %d",a);
printf("LCM is %d",p*q/a);
getch();
}
//Multiplication table of given number
#include <stdio.h>
#include <conio.h>
void main(){
int n,p,i;
printf("Enter a number");
scanf("%d",&n);
for(i=1;i<=n;i++){
p=n*i;
printf("%d x %d = %d",n,i,p);
}
getch();
}
//-Check if given number is prime or not—
#include <stdio.h>
#include <conio.h>
void main(){
int i,n;
printf("Enter a number");
scanf("%d",&n);
for(i=2;i<n;i++){
if(n%i==0)
break;
}
if(n==i){
printf("Prime");
}
else{
printf("Not prime");
}
getch();
}
Array
//Print sum of all elements of given array—
#include <stdio.h>
#include <conio.h>
void main(){
int n[10],s=0;
printf("Enter 10 number");
//taking inputs
for(i=0;i<10;i++)
{
scanf("%d",&n[i]);
}
//finding sum
for(i=0;i<10;i++)
{
s=s+a[i];
}
//printing sum
printf("Sum is %d",s);
getch();
}
//To arrange given array in ascending order
#include <stdio.h>
void main (){
int num[20];
int i, j, a, n;
printf("enter number of elements in an array");
scanf("%d", &n);
printf("Enter the elements");
for (i = 0; i < n; ++i)
scanf("%d", &num[i]);
for (i = 0; i < n; ++i){
for (j = i + 1; j < n; ++j){
if (num[i] > num[j]){
a = num[i];
num[i] = num[j];
num[j] = a;
}
}
}
printf("The numbers in ascending order is:");
for (i = 0; i < n; ++i){
printf("%d", num[i]);
}
}
//sum of even elements and odd elements of given array—
#include <stdio.h>
#include <conio.h>
void main(){
int n[10],se=0,so=0,i;
printf("Enter 10 number");
//taking inputs
for(i=0;i<10;i++)
{
scanf("%d",&n[i]);
}
//finding sum
for(i=0;i<10;i++)
{
if(n[i]%2 == 0)
{
se=se+n[i];
}
else
{
so=so+n[i];
}
}
//printing sum
printf("Sum of even elements is %d",se);
printf("Sum of odd elements is %d",so);
getch();
}
//split even elements and odd elements of given array—
#include <stdio.h>
#include <conio.h>
void main(){
int n[10],se[10],so[10],j=0,k=0,i;
printf("Enter 10 number");
//taking inputs
for(i=0;i<10;i++)
{
scanf("%d",&n[i]);
}
//finding sum
for(i=0;i<n;i++)
{
if(n[i]%2 == 0)
{
se[j]=n[i];
j++;
}
else
{
so[k]=n[i];
k++;
}
}
//printing sum
printf("Even elements are")
for(i=0;i<j;i++)
{
scanf("%d,",&se[i]);
}
printf("Odd elements are")
for(i=0;i<k;i++)
{
scanf("%d,",&so[i]);
}
getch();
}
//sum of even indexed elements and odd elements of given array—
#include <stdio.h>
#include <conio.h>
void main(){
int n[10],se=0,so=0,i;
printf("Enter 10 number");
//taking inputs
for(i=0;i<10;i++)
{
scanf("%d",&n[i]);
}
//finding sum
for(i=0;i<10;i++)
{
if(i%2 == 0)
{
se=se+n[i];
}
else
{
so=so+n[i];
}
}
//printing sum
printf("Sum of even indexed elements is %d",se);
printf("Sum of odd indexed elements is %d",so);
getch();
}
//Add sum of two vectors
#include <stdio.h>
#include <conio.h>
void main(){
int a[3],b[3],c[3];
printf("Enter vector A");
//taking inputs
for(i=0;i<3;i++)
{
scanf("%d",&a[i]);
}
printf("Enter vector B");
for(i=0;i<3;i++)
{
scanf("%d",&b[i]);
}
//finding sum
for(i=0;i<3;i++)
{
c[i]=a[i]+b[i];
}
//printing sum
printf("Sum is\n");
for(i=0;i<3;i++)
{
printf("%d\n",c[i]);
}
getch();
}
//-A program to input a matrix, and find sum of diagonal elements
#include<stdio.h>
void main()
{
int m[3][3],m2[3][3],sum=0;
printf("Enter matrix elements \n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&m[i][j]);
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(i==j)
{
sum=sum+m[i][j];
}
}
}
printf("Addition of principal diagonal elements is %d\n",sum);
}
//-A program to input a matrix, transpose it and perform sum
#include<stdio.h>
void main()
{
int m1[3][3],m2[3][3],sum[3][3];
printf("Enter first matrix elements \n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Transposed matrix is \n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
m2[i][j]=m1[j][i];
printf("%d\t",m2[i][j]);
}
printf("\n");
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
sum[i][j]=m1[i][j]+m2[i][j];
}
}
printf("Addition of matrix\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("%d\t",sum[i][j]);
}
printf("\n");
}
}
}
//-Matrix Subtraction
#include<stdio.h>
void main()
{
int m1[3][3],m2[3][3],sum[3][3];
printf("Enter first matrix elements \n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&m2[i][j]);
}
}
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
sum[i][j]=m1[i][j]+m2[i][j];
}
}
printf("Addition of matrix\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d\t",sum[i][j]);
}
printf("\n");
}
}
}
//-Matrix multipilcation
#include<stdio.h>
void main()
{
int r1,r2,c1,c2;
printf("Enter number of rows and columns for First Matrix:\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns for Second Matrix:\n");
scanf("%d",&r2,&c2);
if(c1!=r2)
{
printf("Matrices Can't be multiplied together");
}
int m1[r1][c1],m2[r2][c2];
printf("Enter first matrix elements \n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
int mul[r1][c2];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
mul[i][j]=0;
// Multiplying i’th row with j’th column
for(int k=0;k<c1;k++)
{
mul[i][j]+=m1[i][k]*m2[k][j];
}
}
}
printf("Multiplied matrix\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}}
//-Matrix multipilcation of 3x3
#include<stdio.h>
void main()
{
int m1[3][3],m2[3][3];
printf("Enter first matrix elements \n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter Second matrix elements\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&m2[i][j]);
}
}
int mul[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
mul[i][j]=0;
// Multiplying i’th row with j’th column
for(int k=0;k<3;k++)
{
mul[i][j]+=m1[i][k]*m2[k][j];
}
}
}
printf("Multiplied matrix\n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}}
//-Input a string and count vowel,consonants, words, spaces and digits
#include <stdio.h>
void main() {
char line[150];
int vowels, consonant, digit, space;
// initialize all variables to 0
vowels = consonant = digit = space = 0;
// get full line of string input
printf("Enter a line of string: ");
scanf("%[^\n]s",line);
// loop through each character of the string
for (int i = 0; line[i] != '\0'; ++i) {
// convert character to lowercase
line[i] = tolower(line[i]);
// check if the character is a vowel
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u') {
// increment value of vowels by 1
++vowels;
}
// if it is not a vowel and if it is an alphabet, it is a consonant
else if ((line[i] >= 'a' && line[i] <= 'z')) {
++consonant;
}
// check if the character is a digit
else if (line[i] >= '0' && line[i] <= '9') {
++digit;
}
// check if the character is an empty space
else if (line[i] == ' ') {
++space;
}
}
printf("Vowels: %d", vowels);
printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\nWhite spaces: %d", space);
printf("\nTotal number of words: %d", space+1);
}
//-concatenate a string without using strcat
#include<stdio.h>
void main(void)
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}
//checking if string is palindrome or not, without using strrev()
#include <stdio.h>
#include <string.h>
void main(){
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
Functions
#include <stdio.h>
int add(int a,int b); //having parameters and return types
void main(){
printf("Enter two numbers") ;
scanf("%d%d",&a,&b);
c=add(a,b);
printf("sum is %d",c);
}
int add(int a,int b) //having parameters and return types
{
int c=a+b;
return c;
}
//
void add(int a,int b); //having parameters and no return type
void main(){
printf("Enter two numbers") ;
scanf("%d%d",&a,&b);
add(a,b);
}
void add(int a,int b)
{
int c=a+b;
printf("sum is %d",c)
}
//--
int add(); //having no parameter but have return type
void main(){
int c;
c=add();
printf("sum is %d",c)
}
int add()
{
printf("Enter two numbers") ;
scanf("%d%d",&a,&b);
c=a+b;
return c;
}
//-
void add(); // have no parameter and no return type
void main(){
add();
}
void add()
{
printf("Enter two numbers") ;
scanf("%d%d",&a,&b);
c=a+b;
printf("sum is %d",c);
}
//-Print reverse of given number using function
#include <stdio.h>
#include <conio.h>
int reverse(int n){
int r,s=0;
do{
r=n%10;
s=s*10+r;
n=n/10;
}
}
void main(){
int n,s;
printf("Enter a number");
scanf("%d",&n);
s=reverse(n);
printf("Reverse of given number is %d",s);
getch();
}
//-Generate prime numbers using function-
#include <stdio.h>
#include <conio.h>
void main(){
int i,n,x;
printf("Enter a number upto which you want to generate prime number");
scanf("%d",&n);
for(i=2;i<n;i++){
x=isPrime(i);
if(x==1){
printf("%d,"i);
}
}
getch();
}
int isPrime(int n){
for(i=2;i<n;i++){
if(n%i==0)
break;
}
if(n==i){
return 1;
}
}
//-Swap values using pass by reference
#include <stdio.h>
void swap(int*, int*);
void main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
Recursion
//-Factorial Using Recursion-
#include <stdio.h>
#include <conio.h>
void main(){
int f,n;
printf("Enter a number ");
scanf("%d",&n);
f=facto(n);
printf("Factorial is %d,"f);
getch();
}
int facto(int n){
if(n==1)
return 1;;
else
return n*facto(n-1);
}
//-Sum of natural numbers upto nth term Using Recursion-
#include <stdio.h>
#include <conio.h>
void main(){
int f,n;
printf("Enter a number ");
scanf("%d",&n);
f=sum(n);
printf("Sum is %d,"f);
getch();
}
int sum(int n){
if(n==1)
return 1;;
else
return n+sum(n-1);
}
//-Print nth Fibonaccii term Using Recursion-
#include <stdio.h>
#include <conio.h>
void main(){
int f,n;
printf("Enter term ");
scanf("%d",&n);
f=fibo(n);
printf("%dth Fibonacci term is %d,"n,f);
getch();
}
int fibo(int n){
if(n==1|| n==2)
return 1;;
else
return fibo(n-1+fibo(n-2);
}
//-HCF of given number Using Recursion-
#include <stdio.h>
#include <conio.h>
void main(){
int h,a,b;
printf("Enter a number ");
scanf("%d%d",&a,&b);
h=hcf(a,b);
printf("HCF is %d,"h);
getch();
}
int hcf(int a, int b){
if(b != 0 )
return hcf(b,a%b);
else
return a;
}
Structure
//Read Records of 20 Students in structure & Sort Marks in Ascending Order
#include<stdio.h>
/* Declaration of structure */
struct student
{
char name[30];
int roll;
float marks;
};
void main()
{
/* Declaration of array of structure */
struct student s[20], temp;
int i,j,n;
printf("Enter n:\n");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("Enter name, roll and marks of student:\n");
scanf("%s%d%f",s[i].name, &s[i].roll, &s[i].marks);
}
for(i=0;i< n-1;i++)
{
if(s[i].marks>40)
{
printf("Name: %s\n", s[i].name);
printf("Roll: %d\n", s[i].roll);
printf("Marks: %f\n\n", s[i].marks);
}
}
}
//Read Records of 20 Students in structure & Sort Marks in Ascending Order
#include<stdio.h>
/* Declaration of structure */
struct student
{
char name[30];
int roll;
float marks;
};
void main()
{
/* Declaration of array of structure */
struct student s[20], temp;
int i,j,n;
printf("Enter n:\n");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("Enter name, roll and marks of student:\n");
scanf("%s%d%f",s[i].name, &s[i].roll, &s[i].marks);
}
for(i=0;i< n-1;i++)
{
for(j=i+1;j< n;j++)
{
if(s[i].marks>s[j].marks)
{
temp = s[i];
s[i] = s[j];
s[j] = temp;
}
}
}
printf("Sorted records are:\n");
for(i=0;i< n;i++)
{
printf("Name: %s\n", s[i].name);
printf("Roll: %d\n", s[i].roll);
printf("Marks: %0.2f\n\n", s[i].marks);
}
}
//--Employee Record in Alphabetical Order by Name in Structure
#include<stdio.h>
#include<string.h>
/* Declaration of structure */
typedef struct
{
char name[30];
int salary;
int age;
}employee;
void main()
{
/* Declaration of array of structure */
employee e[20], temp;
int i,j,n;
printf("Enter n:\n");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("Enter name, salary and age of employee:\n");
scanf("%s%d%d",e[i].name, &e[i].salary, &e[i].age);
}
for(i=0;i< n-1;i++)
{
for(j=i+1;j< n;j++)
{
if(strcmp(e[i].name, e[j].name) > 0)
{
temp = e[i];
e[i] = e[j];
e[j] = temp;
}
}
}
printf("Records in alphabetical order are:\n");
for(i=0;i< n;i++)
{
printf("Name: %s\n", e[i].name);
printf("Salary: %d\n", e[i].salary);
printf("Age: %d\n\n", e[i].age);
}
}
//-Employee Record in Descending Order by Age in Structure
#include<stdio.h>
* Declaration of structure */
typedef struct
{
char name[30];
int salary;
int age;
}employee;
void main()
{
/* Declaration of array of structure */
employee e[20], temp;
int i,j,n;
printf("Enter n:\n");
scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("Enter name, salary and age of employee:\n");
scanf("%s%d%d",e[i].name, &e[i].salary, &e[i].age);
}
for(i=0;i< n-1;i++)
{
for(j=i+1;j< n;j++)
{
if(e[i].age< e[j].age)
{
temp = e[i];
e[i] = e[j];
e[j] = temp;
}
}
}
printf("Sorted records are:\n");
for(i=0;i< n;i++)
{
printf("Name: %s\n", e[i].name);
printf("Salary: %d\n", e[i].salary);
printf("Age: %d\n\n", e[i].age);
}
}
Graphics
// C program to draw few shapes
#include <graphics.h>
// Driver code
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
//rectangle(left, top, right, bottom);
rectangle(10,20,30,50);
rectangle(50,50,60,60)
//circle(centerX,centerY,radius)
circle(40,40,20);
//ellipse(centerX,centerY,startAngle,endAngle,xradius,yradius);
ellipse(80,80,0,360,15,20);
//arc(centerX,centerY,startAngle,endAngle,radius);
arc(180,180,0,200,24);
//line(left, top, right, bottom)
line(50,50,60,60);
//setcolor(COLORNAME)
setcolor(RED);
//setbkcolor(COLORNAME)
setbkcolor(GREEN);
//outtextxy(x,y,"YourText")
outtextxy(getmaxx()/2,getmaxy(),"These are my drawings");
getch();
closegraph();
return 0;
}