Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts
Hello Here is C program to check If the year entered by user is century Year or not. First we check if the year is Century then check if it is leap or not. And for other check Leap year.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
main()
{
    int year;
    printf("Enter the Year you want to check:- ");
    scanf("%d",&year);
    if(year % 100 ==0)
    {
        if (year % 400 == 0)
        printf("Century Leap Year");
        else
        printf("Not a leap year");
    }
    else
    {
        if(year % 4 == 0)
        printf("Leap Year");
        else
        printf("Not a leap year");

    }
}

And I think you understand this code.
Hello Here is C Program to Find the Location (Place) of a Character That one Lies at Last.
Example: In Apple find P. Answer is 2. In Sagar Find a. Answer is 3. Else Not Found print no found.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<conio.h>
void main()
 {
   char a,name[10];
   int i,the=0;
   printf("Enter String:- ");
   gets(name);
   printf("Enter Char:- ");
   scanf("%c",&a);
   for(i=0;name[i]!='\0';i++)
   the=the+1;
   for(i=the;i>=0;i--)
   {
    if(a==name[i])
    {
      printf("%d",i);
      break;
      }
   }
   if(i<0)
   printf("Not Found Sorry");
   getche();
   }

And First we find the total character and run the loop from Back and find the the character if found break the loop. else Display not found.
Hello Here is a C program to Swipe a Two Variables With out the use of other verifiable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
void main()
    {
    int a,b;
    printf ("Enter the value of A:- ");
    scanf ("%d",&a);
    printf ("Enter the value of B:- ");
    scanf ("%d",&b);
    printf("%d is A and %d is B\n",a,b);
    a=a+b;
    b=a-b;
    a=a-b;
    printf("%d is A and %d is B\n",a,b);
    }

Let 4(a) 6(b)
4(a) + 6(b) = 10(a New)
10(a New) - 6(b) = 4(b New)
10(a New) - 4(b New) = 6(a New)

I expect you understand the code above.
Hello Here i a c Program for matrix multiplication of User defined size. Here we create different function to Read data add the data and display them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
52
#include<stdio.h>
void read(int,int,int [][20]);
void add(int,int,int [][20],int [][20],int [][20]);
void display(int,int,int [][20]);
void main()
    {
    int r,c,mat[20][20],sem[20][20],sum[20][20];
    printf("Enter the Value of Row and Column:- \n");
    scanf("%d %d",&r, &c);
    printf("Enter the First Matrix:- \n");
    read(r,c,mat);
    printf("Enter the Second Matrix:- \n");
    read(r,c,sem);
    add(r,c,mat,sem,sum);
    printf("The Sum matrix is:- \n");
    display(r,c,sum);
    }
void read(int x,int y,int ma[20][20])
    {
    int i,j;
    for (i=0;i<x;i++)
        {
        for(j=0;j<y;j++)
            {
            scanf("%d",&ma[i][j]);
            }
        }
    }
void add(int x, int y,int right[20][20],int left[20][20],int ans[20][20])
    {
    int i,j;
    for (i=0;i<x;i++)
        {
        for(j=0;j<y;j++)
            {
            ans[i][j]=right[i][j]+left[i][j];
            }
        }
    }
void display(int x,int y, int ma[20][20])
    {
    int i,j;
    for (i=0;i<x;i++)
        {
        for(j=0;j<y;j++)
            {
            printf("%d\t",ma[i][j]);
            }
        printf("\n");
        }
    }

The Process is Quite simple in add function we pass 3 matrix so that can store the sum in one of the matrix and display it.
Hello Write some things here
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
void read(int, int []);
void display(int, int []);
void sort(int, int []);
void main()
    {
    int num, data[50];
    printf("Enter Total no of value to be sorted in Accessending Order:- ");
    scanf("%d",&num);
    read(num, data);
    sort(num, data);
    printf("After SUcessful Arangement in Accessending Order:- \n");
    display(num,data);
    }
void read(int x, int thedata[50])
    {
    int i;
    printf("Enter the values to be inserted in the array:-\n");
    for(i=0;i<x;i++)
        {
        scanf("%d",&thedata[i]);
        }
    }
void sort(int x, int thedata[50])
    {
    int i,j,tmp;
    for(i=0;i<x-1;i++)
        {
        for (j=0;j<x-1;j++)
            {
            if(thedata[j]>thedata[j+1])
                {
                tmp=thedata[j];
                thedata[j]=thedata[j+1];
                thedata[j+1]=tmp;
                }
            }
        }
    }
void display(int x, int thedata[50])
    {
    int i;
    for(i=0;i<x;i++)
        {
        printf("%d\t",thedata[i]);
        }
    }

Here we Use a function to read another to sort and another to display. As passing array is call by reference we need not to return individual value.
Hello Read a String from Users and Print It in Multi line using new line after every words. Read a String "my name is sagar Devkota" and Print it as:-
My
Name
Is
Sagar
Devkota

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<stdio.h>
void nline(char []);
void main()
    {
    char asent[100];
    int i;
    printf("Enter a line of string:- \n");
    gets(asent);
    nline(asent);
    puts(asent);

    }
void nline(char thesent[100])
    {
    int i;
    for (i=0;thesent[i]!='\0';i++)
        {
        if (thesent[0]>='a' && thesent[0]<='z')
            thesent[0]=thesent[0]-32;
        if(thesent[i]==' ')
            {
                thesent[i]='\n';
                if(thesent[i+1]>='a' && thesent[i+1]<='z')
                    thesent[i+1]=thesent[i+1]-32;
            }
        }
    }

Here we Check the case of First and make it capital if it was small. Then check every space and replace it with Newline "\n". and Make Capital all after that space.
Hello Here is a program to count the UPPERCASE lowercase and Total Characters in a String.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>
#define MAX 100
int ucase(char asent[]);
int lcase(char asent[]);
int total(char asent[]);
void main()
    {
    int u,l,t;
    char asent[MAX];
    printf("Enter a Sentence:-\n");
    gets(asent);
    u=ucase(asent);
    l=lcase(asent);
    t=total(asent);
    printf("In the Sentence ");
    puts(asent);
    printf("Total Letters are %d. Among them %d are UPPERCASE and %d are lowercase",t,u,l);
    }
int ucase(char thesent[MAX])
    {
    int i,count=0;
    for (i=0;thesent[i]!='\0';i++)
        {
        if(thesent[i]>='A' && thesent[i]<='Z')
        count++;
        }
    return (count);
    }
int lcase(char thesent[MAX])
    {
    int i,count=0;
    for (i=0;thesent[i]!='\0';i++)
        {
        if(thesent[i]>='a' && thesent[i]<='z')
        count++;
        }
    return (count);
    }
int total(char thesent[MAX])
    {
    int i,count=0;
    for (i=0;thesent[i]!='\0';i++)
        {
        count++;
        }
    return (count);
    }

We pass the string and Check their case and increment if they are with in the boundary.
Hello Here is A program to Print the Prime Number up to That Number which is inserted by users. Here we are using function. If you are not using function yet You can read the number in second function and process.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
void prime(int);
void main()
    {
    int num;
    printf("Enter a Number up to which the prime numbers to be printed:- ");
    scanf("%d",&num);
    prime(num);
    }
void prime(int a)
    {
    int i,j;
    printf("1\t2\t");
    for (i=3;i<=a;i++)
        {
        for (j=2;j<i;j++)
            {
            if ((i%j)==0)
            break;
            }
        if(i==j)
        printf("%d\t",i);
        }
    }

Here we ask users for the max number up to which we need to print out the prime number and from there. we Pass that value to The function and Display the value.
Hello this is a program to convert a Line string to multyline by inserting newline in every space and Capatalize the first word of all line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
void main()
    {
    char asent[100];
    int i;
    printf("Enter a line of string:- \n");
    gets(asent);
    puts(asent);
    for (i=0;asent[i]!='\0';i++)
        {
        if (asent[0]>='a' && asent[0]<='z')
            asent[0]=asent[0]-32;
        if(asent[i]==' ')
            {
                asent[i]='\n';
                if(asent[i+1]>='a' && asent[i+1]<='z')
                    asent[i+1]=asent[i+1]-32;
            }
        }
    puts(asent);

    }

Write a program to Read the string "My name is sagar devkota" and convert it in the following form using loop:-
My
Name
Is
Sagar
Devkota

Here in the above program we check the Case of first word and make it capital if needed and check every space and replace it with newline and check the case of next word and change its case to Uppercase and display at last.
Hello Here is a program to convert A string inserted from users to lower case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
void main()
{
char cname[100];
int i;
printf("Enter the name of your collage:-\n");
gets(cname);
for (i=0;cname[i]!='\0';i++)
    {
    if(cname[i]>='A' && cname[i]<='Z')
        cname[i]=cname[i]+32;
    }
puts(cname);
}

Here the program checks every alphabet if they are Upper case it will change all of them into lowercase else they will be as it is.
Hello Here is a C program for matrix multiplication of  size 3*3. This is the easiest process. From this code concept you can make multiplication of any size of matrix too. Just need to add some tmp locations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <stdio.h>
#include <stdlib.h>
//Program for Multiplication of matrix 3*3 Time and Update
int main()
{
    int a[3][3],b[3][3],c[3][3];
    int i,j,k,tmp1=0,tmp2=0,tmp3=0;
    printf("Enter value of matrix A \n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            scanf("%d",&a[i][j]);
        printf("\n");
    }
    printf("Enter value of matrix B \n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            scanf("%d",&b[i][j]);
        printf("\n");
    }
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            tmp1=a[i][j]*b[j][0]+tmp1;
            tmp2=a[i][j]*b[j][1]+tmp2;
            tmp3=a[i][j]*b[j][2]+tmp3;
        }
        c[i][0]=tmp1;
        c[i][1]=tmp2;
        c[i][2]=tmp3;

    }
    printf("https://timeandupdate.com \n");
    printf("The Multiplication is :-\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
            printf("%d\t",c[i][j]);
        printf("\n");
    }
    return 0;
}

Here is the Output:
I did not check all but looks its correct.


The above program is compiled and successfully run on Code::Block 13.12.
This is easy program but you just need to focus. If you cant understand how this program flow don't forget to comment we will be back to help you.
Hello Here is A c program to arrange numerical data in array in ascending order (1 3 8..). Input N numbers means you need to ask users how much numbers to be inserted. Here we are using max 10 Numbers if you need more you can from Changing the value a[10]. what ever you need. And you should change it in Called function too.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
#include<conio.h>
void asce(int[],int);
void main()
 {
    int a[10],n,i;
      printf("Enter total no of array to be inserted in array:- ");
      scanf("%d",&n);
      printf("Enter value to be inserted in array");
      for(i=0;i<n;i++)
         scanf("%d",&a[i]);
      asce(a,n);
      printf("After arrranging in ascending order:- \n");
      for(i=0;i<n;i++)
         printf("%d\t",a[i]);
      getche();
   }
void asce(int a[10],int y)
 {
    int i,j,tmp;
      for (i=0;i<y;i++)
      {
       for(j=0;j<y-1;j++)
          {
             if(a[j]>a[j+1])
                {
                   tmp=a[j+1];
                   a[j+1]=a[j];
                   a[j]=tmp;
                  }
            }
      }
   }

The main concept is the nested for loop. There are 100s ways to perform a program. The return type of called function is void as value is passed by reference.
Hello Here is a c program to find out the largest and the smallest numbers from 3 numbers reading those numbers from users.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#include<conio.h>
int gre(int,int,int);
int sma(int,int,int);
void main()
 {
    int a,b,c,g,s;
      printf("Enter 3 numbers:- \n");
      scanf("%d %d %d", &a, &b, &c);
      g=gre(a,b,c);
      s=sma(a,b,c);
      printf("%d is Greatest and %d is Smallest among %d, %d and %d",g,s,a,b,c);
      getche();
   }
int gre(int x,int y, int z)
 {
    int g;
      if(x>y)
       {
          if(x>z)
             g=x;
            else
             g=z;
         }
      else
       {
           if(y>z)
             g=y;
            else
             g=z;
         }
      return (g);
   }
int sma(int x,int y, int z)
 {
    int s;
      if(x<y)
       {
          if(x<z)
             s=x;
            else
             s=z;
         }
      else
       {
           if(y<z)
             s=y;
            else
             s=z;
         }
      return (s);
   }

Here we create two functions for finding smallest and greatest.
Hello Here is a program for Reading a number from user and print the factorial of that number using recursive function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<stdio.h>
#include<conio.h>
int factf(int);
void main()
 {
    int a, fact;
      printf("enter a number to find its factorial:- ");
      scanf("%d",&a);
      fact=factf(a);
      printf("Factorial is %d",fact);
      getche();
   }
int factf(int x)
 {
    int y=1;
    if(x !=0)
       y=x*factf(x-1);
      return(y);
   }

Recursive function is that function that falls it self. We should set certain conditions if not the program will be of infinite loop.

The function that we used in previous article is of no use as we can write those piece of words in the main functions insist of calling the other function. those functions always perform the same task. It will be very useful if we can provide some more instruction to them and the work accordingly.f t

Here is the general format of the function that can get value and perform the task.
return_type function name(dataype argument1, datatype argument2)
{
local veriable deceleration
statememts;
return;
}

It will be clear if showing some example insist of formate so here goes a sample example. we will describe it later.

#include<stdio.h>
int add(int, int, int);
int main()
{
int a,b,c,sum;
printf("Enter the three numbers:- ");
scanf("%d %d %d",&a, &b, &c);
sum = add(a,b,c);
printf("Finally the sum is %d",sum);
return 0;
}
int add(int x, int y, int z)
{
int ans=x+y+z;
return(ans);
}
Here goes Output
Enter the the three numbers:- 1 2 3
Finally the sum is 6

Now We will define the program.
Int add(int, int, int)
As mentioned earlier we should Declare that the function add will be used in the program. we should define the data type it can receive.
Then add(a,b,c) Here the variables a b and c are passed to the functions add and a,b and c are actual arguments.
Then int add(int x, int y, int z) Here the variables value passed are stored in x,y and z. and are known as formal arguments.
return(ans); will return the value to the the function it was called.
sum will now get the value and finally printed out.

After calling a function the calling function wait till the function run out of statements to be excited. If you need any help on programs or any confusion please let us know form the comment box below.
A function in C is a block of statements that perform task of some kind. Every c program have collections of functions. You can use a function to perform certain task that are usually need to be done so that for every time you need to preform that task you don't need to rewrite the code insist call the function to do that task. Its something like hiring  some person to do some special task for you.
Here goes a example program:

#include<stdio.h>
void text();/* we need to declare that we are using this function in program */
int main()
{
text()/* calling function text */
printf("The message peinted above is from function test.");
return 0;
}
void text
{
printf("How is your day\?\n");
}

Output:
How is your day?
The message peinted above is from function test.

In this program above we define two functions main() and text()
we use the function test in three location. The first is to inform the program that we are using text function. Its like informing to friends before visiting them.
Second time inside the main function to call the function from main function. so now the programs go the the function text and perform the task and come back and continue.
At last we create a function. that's all.

A function can be called any numbers of time as needed. we can create any numbers of functions as needed. we can call any function(except main) from from any function. But another function cannot defined inside another function.
Basically there are two types of functions:
Library Functions: pritnf() scanf() etc and
User defined functions text() message()
A functions can be named any but should no be the keyword.
Loops will help you to do some tasks that need to be do a statement repeating until the time you set or till some conditions.
There are three method in c for where you control loop in c.
Loops are the most for C programming Be prepared for all loops. and work hard on it.

The while loop

The while loop repeats a statement until the test at the top proves false. the while has the form:
while (expression)
{
Statements;
}
For example
int x=3;
while (x>0)
{
printf("x=%d\n",x);
x--;
}
output:
x=3
x=2
x=1

Because the while loop can accept expressions, not conditions.

The do while loop

This is vary similar to the while loop excepted that the test occurs at the end of the loop body. This guarantees that the loop is executed at leas once before continuing. Such a step is frequently used where data is to be read. the rest then verifies the data, and loops back to read again if it was unaccepted The do while loop has the form:
do
{
statement;
}
while(expression);
example
int x=3;
do
{
printf("x=%d\n",x--);
}
while(x>0);
output
x=3
x=2
x=1

The for loop

The for loop works where the number of iteration of the loop is known before the loop variable is entered. The head of loop consists of three parts separated by semicolons.
  • The  first is rune before the loop is entered. This is usually the initialization of the loop variable.
  • The second is a test, the loop is excited when this returns false.
  • The third is a statement to be run every time the loop body is complicated.This is usually an increment for the loop counter.
The C for statement has the following form:
for(expression1; expression2; expression3)
{
statement;
}
expression1 initialises; expression2 it the terminate test; expression3 is the modifier;
Simple example:
int x;
main()
{
for(x=3;x>0;x--)
{
printf("x=%d\n",x);
}
Output:
x=3
x=2
x=1

in c programming we need to control some decisions as if this comes true the program should do this else the program should do this. Today we will learn about all these.

If Statement

if statement is used when we want some part of program should be operated only if some conditinn is true. General form of if statement is if (Condition)
Do this;

if (a==b)
printf("a and b are equal");

In above program a and b are equal is displayed only when a are equal to b.

If else statement

if(a==b)
printf("a and b are equal");
else
printf("a and b are not equal");

If else if statement

if(a>80)
printf("Distionction");
else if (a>60 && a<80)
printf("First division");
else if(a<60 && a>40);
printf("Passed");
else
printf("Failed");

Nested if else

if()
{
if()
do this
else
do this
}
else if ()
{
if()
{
if()
do this
else
do this
}
else
do this
}
else
do this

The above is simple structure.

Next we will go to loop control structure do comment if you need help in some programs in your Lab assignment questions or class questions.
Here is a simple program of c. Try not to learn what is that just understand and looks how program goes on. For learning go through program and come back to understand. as your mind will make some ideas from first view.

1. /* Simple Basic Program */
2. #include<stdio.h>
3. #include<conio.h>
4. main()
5. {
6. printf("Hello Sansar!");
7. getch();
8. }

The first line is a comment you can write whatever you want inside /* and */ it wont affect in program.
The Second 2 lines are header files they call library functions to work in the program. stdio.h is For Standard Input/Output library. You must have that header to use printf() function.
conio.h is called to use getch() function in program.
Line no 4 define main function. The function main will be called first when program starts. all the commands on main function are covered inside { and } braces.
line no 6 printf("Hello Sansar!"); will print in the screen. printf is a library function to print out. every statement on c program should ends with ;.
getch() function hold the screen till some character are inserted from keyboard. however modern compiler did not need this while programming.

How a program should be written?

  • Program should be in same order that programmer want it to be executed.
  • tabs and spaces can be added as needed for better looks and easy for debugging.
  • Remember all statements are in small case letter. C is case sensitive.
  • C is called free-form language. as there is no any rule for position where statement should be written.
  • Every C statement must end with ;. ; is a terminator for all statement.


This is basic of all so next we will go to if else else if condition. Comment if need any helps out there.