-->

Sunday, December 21, 2014

Program to find sum of even and odd numbers (using one for loop) in C

Instead of using two for loops as in the previous program, only one for loop can be used. Let us see how this can be achieved.

sum= 0;
/* To add all numbers */
for(i=1; i<=n; i++)
{
sum = sum+i;
}

It is easy to say that we are computing the sum of all the numbers from 1 to n. To add only even numbers or to add numbers, the code can be changed as shown below:

esum=0;                          /* To store sum of even numbers */
osum = 0;                        /* To store sum of odd numbers */

/* To generate the integer from 1 to n */

for(i=1; i<=n; i++)
{
if(i % 2 == 0)                     /* If even */
esum = esum + i;               /* Add to even sum */
else
osum = osum + i;

}

Thus, after the control comes out of the for-loop, the variable esum contains the sum of even numbers and the variable osum contains the sum of odd numbers. The result can be printed using the following statements:

printf("Sum of even numbers = %d\n",esum);
printf("Sum of odd numbers = %d\n", osum);

The complete program is shown below:

Example : Program to add even numbers and odd numbers.
Program
#include<stdio.h>
main( )
{
int n, i, esum, osum;
printf("Enter the value of n:\n");
scanf("%d",&n);
/* Generate and print number from 1 to n */
printf("Integer from 1 to %d are \n",n);
for(i=1; i<=n;i++)
printf("%d",i);
printf("\n");
esum=0;
osum=0;

/* find sum of even and odd numbers */
for ( i=1; i<=n; i++)
{
if((i %2) == 0)
esum +=i;     /* Add even numbers */
else
osum += i;    /* Add odd numbers */

}
printf(" Sum of even numbers = %d\n",esum);
printf("Sum of odd numbers = %d\n",osum);
}

TRACING

Execution starts from here

Non-executable statement
Input
Enter the value of n:
6
Output 1
Integer from 1 to 6
1 2 3 4 5 6

Computations
e_sum = 0
o_sum = 0

i=1 2 3 4 5 6
e_sum = 0+2+4+6
                         = 12
o_sum= 0+1+3+5
                         = 9
Output 2

Sum of even numbers =12
Sum of odd numbers = 9

Saturday, December 20, 2014

How to insert data into multiple table in ASP.NET

I have two table, such as complaint table and status table. Now, i want to add data in both table through same SqlCommand class instance. Now, first to prepare the Connection using SqlConnection class, after that design the query for first table after inserting the data, we can add data into other table.



You can check the complete.

Source Code

<table style="width:100%;">
    <tr>
        <td class="style3">
            Complaint against To</td>
    </tr>
    <tr>
        <td class="style1">
            Name :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pname" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
                ControlToValidate="pname" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Business&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pbusiness" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 
                ControlToValidate="pbusiness" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Address&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="padd" runat="server" Height="61px" Width="248px" 
                TextMode="MultiLine" ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
                ControlToValidate="padd" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Phone Number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pnumber" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" 
                ControlToValidate="pnumber" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Faulty Proff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pfproff" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" 
                ControlToValidate="pfproff" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Faulty Proff Image&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:FileUpload ID="pproffimage" runat="server" />
        </td>
    </tr>
    <tr>
        <td class="style1">
            Review Against&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="preview" runat="server" Height="55px" Width="248px" 
                TextMode="MultiLine" ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" 
                ControlToValidate="preview" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Fir Number (if any)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pfir" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" 
                ControlToValidate="pfir" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
</table>
<p>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
</p>
<p>
    <asp:Label ID="Label1" runat="server"></asp:Label>
</p>

<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" 
    ValidationGroup="cr" />

Design View Of Source code

Complaint form

Complete Business Logic Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class UserControl_complaint : System.Web.UI.UserControl
{
    string savefile = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string un=System.Guid.NewGuid ().ToString ();

        if (pproffimage.HasFile)
        {
            pproffimage.SaveAs(Server.MapPath("~/images/" + pproffimage.FileName));
            savefile = "~/images/" + pproffimage.FileName;

        }
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "insert into complaint(Party_Name,Business,Address,Faulty_proff,Image,Review,Fir_Numer,Consumer_Id,date,uniquet) values(@cuoname,@cuofname,@cuodob,@cuoadd,@cuoimage,@cuophone,@cuoemail,@cuoph,@cuodate,@cuoun)";
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@cuoname",pname.Text);
        cmd.Parameters.AddWithValue("@cuofname",pbusiness.Text);
        cmd.Parameters.AddWithValue("@cuodob", padd.Text);
        cmd.Parameters.AddWithValue("@cuoadd",pfproff .Text);
        cmd.Parameters.AddWithValue("@cuoimage", savefile);
        cmd.Parameters.AddWithValue("@cuophone",preview.Text);
        cmd.Parameters.AddWithValue("@cuoemail", pfir .Text);
        cmd.Parameters.AddWithValue("@cuoph",Session["cons_id"].ToString());
        cmd.Parameters.AddWithValue("@cuodate", DateTime.Now.ToString ());
         cmd.Parameters.AddWithValue("@cuoun", un);
        int a = cmd.ExecuteNonQuery();
        if (a > 0)
        {
            Label1.Text = "Data Inserted Sucessfully";
            Label1.ForeColor = System.Drawing.Color.Green;
        }
        cmd.Parameters.Clear();

        cmd.CommandText = "insert into status(status,Complaint_Id,consumer_id) values(@stname,@comid,@cunsid)";
        cmd.Parameters.AddWithValue("@stname", "pending");
        cmd.Parameters.AddWithValue("@comid",un);
        cmd.Parameters.AddWithValue("@cunsid", Session["cons_id"].ToString());
        cmd.ExecuteNonQuery();

    }
}

Friday, December 19, 2014

How to run Applet in Netbean IDE

We are very familiar with java programming, when we come to applet programming then we need some resources like browsers, AppletViewer etc to run the applet code. When i run the applet code in command prompt using following command:
appletviewer filename.html
 Applet viewer was not appear in windows 8.1. Then lastly i was decide that i shall install Netbean IDE for applet code.

import java.awt.*;
 import java.applet.*;

public class design extends Applet
 {
 public void paint(Graphics g)
{
g.drawString("Hello World",50,100);
}
}

Now, copy this code and following some instructions which is given in the youtube video

Monday, December 15, 2014

Prevention from SQL Injection attack in ASP.NET

In myPrevious article, we have already learn about SQL injection attack. We saw that if we use Text Box for retrieving data from the database then other queries also perform with the same database. So, Microsoft provide, Parameterized query for DML and DQL statements. Like
Replace this statement with the parameterized query

Direct Interface with TextBox (SQL Injection Attack Possible)

cmd.CommandText = "Select * from [TableName] where name='"+TextBox1.Text+"'";

Resolve this problem by the parameterized query

cmd.CommandText = "Select * from [TableName] where name=@name1";
cmd.Parameter.AddWithValue("@name1",TextBox1.Text);

Now that video give more clearance :   

Saturday, December 13, 2014

Program to add the numbers which are divisible by 5 in C

Let us write a program to print all the numbers between N1 and N2 which are divisible by 5. Then, compute the sum of those numbers. The numbers between N1 and N2 can be added using the following statement:

sum =0;
for(i=n1; i<=n2; i++)       /* i start from n1 and goes up to n2 */
{
sum =sum+i;                    /* Can be written as sum +=i */
}

But, it is required to add those which are divisible by 5 only. This can be achieved by taking only those values of i which are divisible by 5 and the program can be modified as follows:

sum =0;
for(i=n1; i<=n2; i++)
{
if(i%5 == 0)
{
sum +=i;
}
}

The complete algorithm are shown below:

Algorithm : DIVBY5.

[This algorithm prints the numbers divisible by 5 between n1 and n2]
Step 1: [Enter lower & upper limits]
Read : n1, n2

Step 2: [Initialize]
sum =0

Step 3: [Generate and add the number if divisible by 5]
for i=n1 to n2 in step 1
if(i%5 ==0)
sum = sum+i
[End of if]
[End of for]

Step 4: [Output the result]
Write : sum

Step 5: [Finished]
Exit.

The complete program to find the sum of all those numbers divisible by 5 is shown below:
Example : Program to add the numbers which are divisible by 5.
PROGRAM

#include<stdio.h>
main( )
{
int n1, n2, i, sum;
printf("Enter range N1 and N2 (N1 < N2): \n");
scanf("%d%d",&n1,&n2);
sum=0;
for(i=n1;i<=n2;i++)
{
if((i%5)==0)
{
printf("%d",i);
sum +=i;
}
}
printf("Total sum =%d\n",sum);
}

TRACING

Execution starts from here
Non-executable statement
Input
Enter the N1 and N2 (N1<N2):
4 20
Computations
sum=0
i= 5 10 15 20
Output
5 10 15 20
sum = 0+5+10+15+20

Total sum = 50

Friday, December 12, 2014

Program to find sum of squares of first N natural numbers in C

Let us design the algorithm and write the program to find the sum of the following series:
12+22+32+42+....................................................N2
We know the program segment to add all the numbers from 1 to N. The program segment is:
sum=0;
for(i=1; i<=n; i++)
{

sum = sum+i;

}
But, to add 12 , 22 , 32 , 42 , ...............N2    it is necessary to replace the statement:
sum = sum+i;

by the statement:

sum = sum+i*i;

The algorithm and flowchart to find the sum of squares of all natural numbers are as follows:

Algorithm : SERIESSUM

Step 1: [Input the number of terms]
Read: n
Step 2: [Initialization]
sum =0
Step 3: [Find the sum of all terms]
for i=1 to n in steps of 1 do
sum = sum + i*i

[End of for i]

Step 4: [Output the sum]
Write : sum
Step 5: [Finished]
Exit.

The complete program to find the sum of the given series is shown below:
Example : Program to add the series  12+22+32+42+....................................................N2

PROGRAM

#include<stdio.h>
main( )
{
int n, i, sum;
printf("Enter the number of terms \n");
scanf("%d",&n);
sum =0;
for(i=1; i<=n; i++)
{
sum = sum+(i*i);
}
printf("Sum of squares of numbers = %d",sum);
}

TRACING

Execution starts from here:
Non-executable statement
Input
Enter the number of terms
5
Computations
sum=0
              i= 1 2 3 4 5
sum =0+12+22+32+42+52
Output
Sum of squares of numbers =55

Thursday, December 11, 2014

Program to simulate calculator using switch in C

The input is the expression of the form (a op b) where a and b are the operands and op is an operator. For example 10+20. Based on the operator, the operation is performed and the result is displayed. The complete algorithm along with the flowchart is shown below:

Algorithm: CALCULATOR

Step 1: [Read expression of from (a+b)]
Read: a, op, b
Step 2: [perform the required operation]
switch(op)

case "+" : res = a+b
case "-" : res = a-b
case "*" : res= a*b
case "/" : if (b==0)
Write : "Divide by 0"
Exit
else

res = a/b
[End of if]
default : Write: ' Invalid operator'
Exit
[End of switch]
Step 3: [Output the result]
Write: res
Step 4: [Finished]
Exit.


The equivalent C program to perform various operations such as addition, subtraction, multiplication and division is shown below:

Program

#include<stdio.h>
#include<process.h>
main( )
{
int a, b, res;
char op; /* can be +, - , *,, or / */
printf("Enter the expression");
scanf("%d%c%d",&a, &op, &b);
switch(op)
{
case '+' :
res = a+b;
break;
case '-':
res= a-b;
break;
case '*':
res=a*b;
break;
case '/' :
if(b!=0)
res=a/b;
else
{
printf("Div by 0\n");
exit(0);
}
default:
printf("Invalid operator \n");
}
/* end of switch statement */
printf("%d %c %d = %d\n",a,op,b,res);  /* Display the result */
}

© Copyright 2013 Computer Programming | All Right Reserved